Minor cleanup and formatting.

This commit is contained in:
Sipke Schoorstra 2015-09-15 23:02:58 +01:00
parent c6a87f5228
commit 3896a8f921
4 changed files with 41 additions and 47 deletions

View File

@ -1,7 +1,4 @@
using System; using Microsoft.WindowsAzure.ServiceRuntime;
using System.Collections.Generic;
using System.Linq;
using Microsoft.WindowsAzure.ServiceRuntime;
using Orchard.Environment; using Orchard.Environment;
namespace Orchard.Azure.Services.TaskLease { namespace Orchard.Azure.Services.TaskLease {

View File

@ -1,7 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Orchard.Data.Conventions; using Orchard.Data.Conventions;
namespace Orchard.TaskLease.Models namespace Orchard.TaskLease.Models

View File

@ -27,7 +27,7 @@ namespace Orchard.TaskLease.Services {
} }
public string Acquire(string taskName, DateTime expiredUtc) { public string Acquire(string taskName, DateTime expiredUtc) {
var machineName = _applicationEnvironment.GetEnvironmentIdentifier(); var environmentIdentifier = _applicationEnvironment.GetEnvironmentIdentifier();
// retrieve current lease for the specified task // retrieve current lease for the specified task
var taskLease = _repository.Get(x => x.TaskName == taskName); var taskLease = _repository.Get(x => x.TaskName == taskName);
@ -36,7 +36,7 @@ namespace Orchard.TaskLease.Services {
if (taskLease == null) { if (taskLease == null) {
taskLease = new TaskLeaseRecord { taskLease = new TaskLeaseRecord {
TaskName = taskName, TaskName = taskName,
MachineName = machineName, MachineName = environmentIdentifier,
State = String.Empty, State = String.Empty,
UpdatedUtc = _clock.UtcNow, UpdatedUtc = _clock.UtcNow,
ExpiredUtc = expiredUtc ExpiredUtc = expiredUtc
@ -49,12 +49,12 @@ namespace Orchard.TaskLease.Services {
} }
// lease can't be aquired only if for a different machine and it has not expired // lease can't be aquired only if for a different machine and it has not expired
if (taskLease.MachineName != machineName && taskLease.ExpiredUtc >= _clock.UtcNow) { if (taskLease.MachineName != environmentIdentifier && taskLease.ExpiredUtc >= _clock.UtcNow) {
return null; return null;
} }
// otherwise update information // otherwise update information
taskLease.MachineName = machineName; taskLease.MachineName = environmentIdentifier;
taskLease.UpdatedUtc = _clock.UtcNow; taskLease.UpdatedUtc = _clock.UtcNow;
taskLease.ExpiredUtc = expiredUtc; taskLease.ExpiredUtc = expiredUtc;
@ -64,10 +64,10 @@ namespace Orchard.TaskLease.Services {
} }
public void Update(string taskName, string state) { public void Update(string taskName, string state) {
var machineName = _applicationEnvironment.GetEnvironmentIdentifier(); var environmentIdentifier = _applicationEnvironment.GetEnvironmentIdentifier();
// retrieve current lease for the specified task // retrieve current lease for the specified task
var taskLease = _repository.Get(x => x.TaskName == taskName && x.MachineName == machineName); var taskLease = _repository.Get(x => x.TaskName == taskName && x.MachineName == environmentIdentifier);
if (taskLease == null) { if (taskLease == null) {
return; return;
@ -78,10 +78,10 @@ namespace Orchard.TaskLease.Services {
} }
public void Update(string taskName, string state, DateTime expiredUtc) { public void Update(string taskName, string state, DateTime expiredUtc) {
var machineName = _applicationEnvironment.GetEnvironmentIdentifier(); var environmentIdentifier = _applicationEnvironment.GetEnvironmentIdentifier();
// retrieve current lease for the specified task // retrieve current lease for the specified task
var taskLease = _repository.Get(x => x.TaskName == taskName && x.MachineName == machineName); var taskLease = _repository.Get(x => x.TaskName == taskName && x.MachineName == environmentIdentifier);
if (taskLease == null) { if (taskLease == null) {
return; return;

View File

@ -44,14 +44,14 @@ namespace Orchard.Tasks.Locking.Services {
if (dLock != null) { if (dLock != null) {
Logger.Debug("Successfully acquired lock '{0}'.", name); Logger.Debug("Successfully acquired lock '{0}'.", name);
return true; return true;
} }
Logger.Warning("Failed to acquire lock '{0}' within the specified timeout ({1}).", name, timeout); Logger.Warning("Failed to acquire lock '{0}' within the specified timeout ({1}).", name, timeout);
} }
catch (Exception ex) { catch (Exception ex) {
Logger.Error(ex, "Error while trying to acquire lock '{0}'.", name); Logger.Error(ex, "Error while trying to acquire lock '{0}'.", name);
// TODO: Is it correct to not throw here? Should we instead ONLY swallow TimeoutException? // TODO: Is it correct to not throw here? Should we instead ONLY swallow TimeoutException?
} }
dLock = null; dLock = null;
return false; return false;
@ -62,12 +62,12 @@ namespace Orchard.Tasks.Locking.Services {
DistributedLock result = AcquireLockInternal(name, maxValidFor, timeout, throwOnTimeout: true); DistributedLock result = AcquireLockInternal(name, maxValidFor, timeout, throwOnTimeout: true);
Logger.Debug("Successfully acquired lock '{0}'.", name); Logger.Debug("Successfully acquired lock '{0}'.", name);
return result; return result;
} }
catch (Exception ex) { catch (Exception ex) {
Logger.Error(ex, "Error while trying to acquire lock '{0}'.", name); Logger.Error(ex, "Error while trying to acquire lock '{0}'.", name);
throw; throw;
} }
} }
private DistributedLock AcquireLockInternal(string name, TimeSpan? maxValidFor, TimeSpan? timeout, bool throwOnTimeout) { private DistributedLock AcquireLockInternal(string name, TimeSpan? maxValidFor, TimeSpan? timeout, bool throwOnTimeout) {
var internalName = GetInternalLockName(name); var internalName = GetInternalLockName(name);
@ -81,7 +81,7 @@ namespace Orchard.Tasks.Locking.Services {
throw new TimeoutException(String.Format("Failed to acquire lock '{0}' within the specified timeout ({1}).", internalName, timeout)); throw new TimeoutException(String.Format("Failed to acquire lock '{0}' within the specified timeout ({1}).", internalName, timeout));
return null; return null;
} }
Logger.Debug("Successfully entered local monitor for lock '{0}'.", internalName); Logger.Debug("Successfully entered local monitor for lock '{0}'.", internalName);
@ -106,14 +106,14 @@ namespace Orchard.Tasks.Locking.Services {
dLock = new DistributedLock(name, internalName, releaseLockAction: () => { dLock = new DistributedLock(name, internalName, releaseLockAction: () => {
Monitor.Exit(monitorObj); Monitor.Exit(monitorObj);
DeleteDistributedLockRecord(internalName); DeleteDistributedLockRecord(internalName);
}); });
_locks.Add(monitorObj, dLock); _locks.Add(monitorObj, dLock);
return true; return true;
} }
return false; return false;
}); });
if (!success) { if (!success) {
Logger.Debug("Record for lock '{0}' could not be created for current machine within the specified timeout ({1}).", internalName, timeout); Logger.Debug("Record for lock '{0}' could not be created for current machine within the specified timeout ({1}).", internalName, timeout);
@ -121,22 +121,22 @@ namespace Orchard.Tasks.Locking.Services {
if (throwOnTimeout) if (throwOnTimeout)
throw new TimeoutException(String.Format("Failed to acquire lock '{0}' within the specified timeout ({1}).", internalName, timeout)); throw new TimeoutException(String.Format("Failed to acquire lock '{0}' within the specified timeout ({1}).", internalName, timeout));
return null; return null;
} }
} }
return dLock; return dLock;
} }
catch (Exception ex) { catch (Exception ex) {
Monitor.Exit(monitorObj); Monitor.Exit(monitorObj);
Logger.Error(ex, "An error occurred while trying to acquire lock '{0}'.", internalName); Logger.Error(ex, "An error occurred while trying to acquire lock '{0}'.", internalName);
throw; throw;
} }
} }
private bool EnsureDistributedLockRecord(string internalName, TimeSpan? maxValidFor) { private bool EnsureDistributedLockRecord(string internalName, TimeSpan? maxValidFor) {
var localMachineName = _applicationEnvironment.GetEnvironmentIdentifier(); var environmentIdentifier = _applicationEnvironment.GetEnvironmentIdentifier();
var hasLockRecord = false; var hasLockRecord = false;
ExecuteOnSeparateTransaction(repository => { ExecuteOnSeparateTransaction(repository => {
@ -148,22 +148,22 @@ namespace Orchard.Tasks.Locking.Services {
repository.Create(new DistributedLockRecord { repository.Create(new DistributedLockRecord {
Name = internalName, Name = internalName,
MachineName = localMachineName, MachineName = environmentIdentifier,
CreatedUtc = _clock.UtcNow, CreatedUtc = _clock.UtcNow,
ValidUntilUtc = maxValidFor.HasValue ? _clock.UtcNow + maxValidFor.Value : default(DateTime?) ValidUntilUtc = maxValidFor.HasValue ? _clock.UtcNow + maxValidFor.Value : default(DateTime?)
}); });
hasLockRecord = true; hasLockRecord = true;
} }
else if (record.MachineName == localMachineName) { else if (record.MachineName == environmentIdentifier) {
// Existing lock was for correct machine name => lock record exists. // Existing lock was for correct machine name => lock record exists.
Logger.Debug("Found a valid record for lock '{0}' and current local machine name '{1}'.", internalName, localMachineName); Logger.Debug("Found a valid record for lock '{0}' and current local machine name '{1}'.", internalName, environmentIdentifier);
hasLockRecord = true; hasLockRecord = true;
} }
}); });
return hasLockRecord; return hasLockRecord;
} }
private void DeleteDistributedLockRecord(string internalName) { private void DeleteDistributedLockRecord(string internalName) {
try { try {
@ -177,7 +177,7 @@ namespace Orchard.Tasks.Locking.Services {
} }
catch (Exception ex) { catch (Exception ex) {
if (ex.IsFatal()) if (ex.IsFatal())
throw; throw;
Logger.Warning(ex, "An error occurred while deleting record for lock '{0}'.", internalName); Logger.Warning(ex, "An error occurred while deleting record for lock '{0}'.", internalName);
} }
} }
@ -198,13 +198,13 @@ namespace Orchard.Tasks.Locking.Services {
if (action == null) if (action == null)
throw new ArgumentNullException(); throw new ArgumentNullException();
using (var childLifetimeScope = _lifetimeScope.BeginLifetimeScope()) { using (var childLifetimeScope = _lifetimeScope.BeginLifetimeScope()) {
var repository = childLifetimeScope.Resolve<IRepository<DistributedLockRecord>>(); var repository = childLifetimeScope.Resolve<IRepository<DistributedLockRecord>>();
var transactionManager = childLifetimeScope.Resolve<ITransactionManager>(); var transactionManager = childLifetimeScope.Resolve<ITransactionManager>();
transactionManager.RequireNew(IsolationLevel.ReadCommitted); transactionManager.RequireNew(IsolationLevel.ReadCommitted);
action(repository); action(repository);
}
} }
}
private string GetInternalLockName(string name) { private string GetInternalLockName(string name) {
// Prefix the requested lock name by a constant and the tenant name. // Prefix the requested lock name by a constant and the tenant name.