Removed redundant thread synchronization code from modules which now use distributed locks.

This commit is contained in:
Daniel Stolt
2015-09-06 21:41:30 +02:00
parent 86b9f35454
commit 49c772bf25
3 changed files with 132 additions and 148 deletions

View File

@@ -1,6 +1,5 @@
using System;
using System.Linq;
using System.Threading;
using Orchard.AuditTrail.Models;
using Orchard.ContentManagement;
using Orchard.Environment.Extensions;
@@ -13,7 +12,6 @@ using Orchard.Tasks.Locking.Services;
namespace Orchard.AuditTrail.Services {
[OrchardFeature("Orchard.AuditTrail.Trimming")]
public class AuditTrailTrimmingBackgroundTask : Component, IBackgroundTask {
private static readonly object _sweepLock = new object();
private readonly ISiteService _siteService;
private readonly IClock _clock;
private readonly IAuditTrailManager _auditTrailManager;
@@ -36,10 +34,9 @@ namespace Orchard.AuditTrail.Services {
}
public void Sweep() {
if (Monitor.TryEnter(_sweepLock)) {
try {
Logger.Debug("Beginning sweep.");
try {
// Only allow this task to run on one farm node at a time.
IDistributedLock @lock;
if (_distributedLockService.TryAcquireLock(GetType().FullName, TimeSpan.FromHours(1), out @lock)) {
@@ -55,16 +52,16 @@ namespace Orchard.AuditTrail.Services {
Settings.LastRunUtc = _clock.UtcNow;
}
}
else
Logger.Debug("Distributed lock could not be acquired; going back to sleep.");
}
catch (Exception ex) {
Logger.Error(ex, "Error during sweep.");
}
finally {
Monitor.Exit(_sweepLock);
Logger.Debug("Ending sweep.");
}
}
}
private bool GetIsTimeToTrim() {
var lastRun = Settings.LastRunUtc ?? DateTime.MinValue;

View File

@@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Microsoft.WindowsAzure.MediaServices.Client;
using Orchard.Azure.MediaServices.Helpers;
using Orchard.Azure.MediaServices.Models;
@@ -18,8 +17,6 @@ using Orchard.Tasks.Locking.Services;
namespace Orchard.Azure.MediaServices.Services.Jobs {
public class JobProcessor : Component, IBackgroundTask {
private static readonly object _sweepLock = new object();
private readonly IWamsClient _wamsClient;
private readonly IAssetManager _assetManager;
private readonly IJobManager _jobManager;
@@ -41,10 +38,9 @@ namespace Orchard.Azure.MediaServices.Services.Jobs {
}
public void Sweep() {
if (Monitor.TryEnter(_sweepLock)) {
try {
Logger.Debug("Beginning sweep.");
try {
if (!_orchardServices.WorkContext.CurrentSite.As<CloudMediaSettingsPart>().IsValid()) {
Logger.Debug("Settings are invalid; going back to sleep.");
return;
@@ -153,16 +149,16 @@ namespace Orchard.Azure.MediaServices.Services.Jobs {
}
}
}
else
Logger.Debug("Distributed lock could not be acquired; going back to sleep.");
}
catch (Exception ex) {
Logger.Error(ex, "Error during sweep.");
}
finally {
Monitor.Exit(_sweepLock);
Logger.Debug("Ending sweep.");
}
}
}
private static string GetAggregateErrorMessage(IEnumerable<Tuple<IJob, ITask, ErrorDetail>> tuples) {
var sb = new StringBuilder();

View File

@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Newtonsoft.Json.Linq;
using Orchard.Environment;
using Orchard.Events;
@@ -13,7 +12,6 @@ namespace Orchard.JobsQueue.Services {
public class JobsQueueProcessor : IJobsQueueProcessor {
private readonly Work<IJobsQueueManager> _jobsQueueManager;
private readonly Work<IEventBus> _eventBus;
private readonly ReaderWriterLockSlim _rwl = new ReaderWriterLockSlim();
private readonly IDistributedLockService _distributedLockService;
public JobsQueueProcessor(
@@ -28,10 +26,8 @@ namespace Orchard.JobsQueue.Services {
}
public ILogger Logger { get; set; }
public void ProcessQueue() {
// prevent two threads on the same machine to process the message queue
if (_rwl.TryEnterWriteLock(0)) {
try {
IDistributedLock @lock;
if (_distributedLockService.TryAcquireLock(GetType().FullName, TimeSpan.FromMinutes(5), out @lock)) {
using (@lock) {
@@ -45,11 +41,6 @@ namespace Orchard.JobsQueue.Services {
}
}
}
finally {
_rwl.ExitWriteLock();
}
}
}
private void ProcessMessage(QueuedJobRecord job) {