mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-05-04 04:57:50 +08:00
Minor cleanup and formatting.
This commit is contained in:
parent
c6a87f5228
commit
3896a8f921
@ -1,7 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.WindowsAzure.ServiceRuntime;
|
||||
using Microsoft.WindowsAzure.ServiceRuntime;
|
||||
using Orchard.Environment;
|
||||
|
||||
namespace Orchard.Azure.Services.TaskLease {
|
||||
|
@ -1,7 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using Orchard.Data.Conventions;
|
||||
|
||||
namespace Orchard.TaskLease.Models
|
||||
|
@ -27,7 +27,7 @@ namespace Orchard.TaskLease.Services {
|
||||
}
|
||||
|
||||
public string Acquire(string taskName, DateTime expiredUtc) {
|
||||
var machineName = _applicationEnvironment.GetEnvironmentIdentifier();
|
||||
var environmentIdentifier = _applicationEnvironment.GetEnvironmentIdentifier();
|
||||
|
||||
// retrieve current lease for the specified task
|
||||
var taskLease = _repository.Get(x => x.TaskName == taskName);
|
||||
@ -36,7 +36,7 @@ namespace Orchard.TaskLease.Services {
|
||||
if (taskLease == null) {
|
||||
taskLease = new TaskLeaseRecord {
|
||||
TaskName = taskName,
|
||||
MachineName = machineName,
|
||||
MachineName = environmentIdentifier,
|
||||
State = String.Empty,
|
||||
UpdatedUtc = _clock.UtcNow,
|
||||
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
|
||||
if (taskLease.MachineName != machineName && taskLease.ExpiredUtc >= _clock.UtcNow) {
|
||||
if (taskLease.MachineName != environmentIdentifier && taskLease.ExpiredUtc >= _clock.UtcNow) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// otherwise update information
|
||||
taskLease.MachineName = machineName;
|
||||
taskLease.MachineName = environmentIdentifier;
|
||||
taskLease.UpdatedUtc = _clock.UtcNow;
|
||||
taskLease.ExpiredUtc = expiredUtc;
|
||||
|
||||
@ -64,10 +64,10 @@ namespace Orchard.TaskLease.Services {
|
||||
}
|
||||
|
||||
public void Update(string taskName, string state) {
|
||||
var machineName = _applicationEnvironment.GetEnvironmentIdentifier();
|
||||
var environmentIdentifier = _applicationEnvironment.GetEnvironmentIdentifier();
|
||||
|
||||
// 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) {
|
||||
return;
|
||||
@ -78,10 +78,10 @@ namespace Orchard.TaskLease.Services {
|
||||
}
|
||||
|
||||
public void Update(string taskName, string state, DateTime expiredUtc) {
|
||||
var machineName = _applicationEnvironment.GetEnvironmentIdentifier();
|
||||
var environmentIdentifier = _applicationEnvironment.GetEnvironmentIdentifier();
|
||||
|
||||
// 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) {
|
||||
return;
|
||||
|
@ -44,30 +44,30 @@ namespace Orchard.Tasks.Locking.Services {
|
||||
if (dLock != null) {
|
||||
Logger.Debug("Successfully acquired lock '{0}'.", name);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Logger.Warning("Failed to acquire lock '{0}' within the specified timeout ({1}).", name, timeout);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
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?
|
||||
}
|
||||
}
|
||||
|
||||
dLock = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public IDistributedLock AcquireLock(string name, TimeSpan? maxValidFor, TimeSpan? timeout) {
|
||||
try {
|
||||
DistributedLock result = AcquireLockInternal(name, maxValidFor, timeout, throwOnTimeout: true);
|
||||
Logger.Debug("Successfully acquired lock '{0}'.", name);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
Logger.Error(ex, "Error while trying to acquire lock '{0}'.", name);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private DistributedLock AcquireLockInternal(string name, TimeSpan? maxValidFor, TimeSpan? timeout, bool throwOnTimeout) {
|
||||
var internalName = GetInternalLockName(name);
|
||||
@ -76,12 +76,12 @@ namespace Orchard.Tasks.Locking.Services {
|
||||
|
||||
if (!Monitor.TryEnter(monitorObj, monitorTimeout)) {
|
||||
Logger.Debug("Could not enter local monitor for lock '{0}' within the specified timeout ({1}).", internalName, timeout);
|
||||
|
||||
|
||||
if (throwOnTimeout)
|
||||
throw new TimeoutException(String.Format("Failed to acquire lock '{0}' within the specified timeout ({1}).", internalName, timeout));
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Logger.Debug("Successfully entered local monitor for lock '{0}'.", internalName);
|
||||
|
||||
@ -106,37 +106,37 @@ namespace Orchard.Tasks.Locking.Services {
|
||||
dLock = new DistributedLock(name, internalName, releaseLockAction: () => {
|
||||
Monitor.Exit(monitorObj);
|
||||
DeleteDistributedLockRecord(internalName);
|
||||
});
|
||||
});
|
||||
|
||||
_locks.Add(monitorObj, dLock);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
});
|
||||
|
||||
if (!success) {
|
||||
Logger.Debug("Record for lock '{0}' could not be created for current machine within the specified timeout ({1}).", internalName, timeout);
|
||||
|
||||
|
||||
if (throwOnTimeout)
|
||||
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) {
|
||||
Monitor.Exit(monitorObj);
|
||||
|
||||
Logger.Error(ex, "An error occurred while trying to acquire lock '{0}'.", internalName);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool EnsureDistributedLockRecord(string internalName, TimeSpan? maxValidFor) {
|
||||
var localMachineName = _applicationEnvironment.GetEnvironmentIdentifier();
|
||||
var environmentIdentifier = _applicationEnvironment.GetEnvironmentIdentifier();
|
||||
var hasLockRecord = false;
|
||||
|
||||
ExecuteOnSeparateTransaction(repository => {
|
||||
@ -145,25 +145,25 @@ namespace Orchard.Tasks.Locking.Services {
|
||||
if (record == null) {
|
||||
// No record existed, so we're good to create a new one.
|
||||
Logger.Debug("No valid record was found for lock '{0}'; creating a new record.", internalName);
|
||||
|
||||
|
||||
repository.Create(new DistributedLockRecord {
|
||||
Name = internalName,
|
||||
MachineName = localMachineName,
|
||||
MachineName = environmentIdentifier,
|
||||
CreatedUtc = _clock.UtcNow,
|
||||
ValidUntilUtc = maxValidFor.HasValue ? _clock.UtcNow + maxValidFor.Value : default(DateTime?)
|
||||
});
|
||||
|
||||
hasLockRecord = true;
|
||||
}
|
||||
else if (record.MachineName == localMachineName) {
|
||||
else if (record.MachineName == environmentIdentifier) {
|
||||
// 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;
|
||||
}
|
||||
});
|
||||
|
||||
return hasLockRecord;
|
||||
}
|
||||
}
|
||||
|
||||
private void DeleteDistributedLockRecord(string internalName) {
|
||||
try {
|
||||
@ -177,9 +177,9 @@ namespace Orchard.Tasks.Locking.Services {
|
||||
}
|
||||
catch (Exception ex) {
|
||||
if (ex.IsFatal())
|
||||
throw;
|
||||
throw;
|
||||
Logger.Warning(ex, "An error occurred while deleting record for lock '{0}'.", internalName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool RepeatUntilTimeout(TimeSpan? timeout, TimeSpan repeatInterval, Func<bool> action) {
|
||||
@ -198,14 +198,14 @@ namespace Orchard.Tasks.Locking.Services {
|
||||
if (action == null)
|
||||
throw new ArgumentNullException();
|
||||
|
||||
using (var childLifetimeScope = _lifetimeScope.BeginLifetimeScope()) {
|
||||
var repository = childLifetimeScope.Resolve<IRepository<DistributedLockRecord>>();
|
||||
var transactionManager = childLifetimeScope.Resolve<ITransactionManager>();
|
||||
transactionManager.RequireNew(IsolationLevel.ReadCommitted);
|
||||
action(repository);
|
||||
}
|
||||
using (var childLifetimeScope = _lifetimeScope.BeginLifetimeScope()) {
|
||||
var repository = childLifetimeScope.Resolve<IRepository<DistributedLockRecord>>();
|
||||
var transactionManager = childLifetimeScope.Resolve<ITransactionManager>();
|
||||
transactionManager.RequireNew(IsolationLevel.ReadCommitted);
|
||||
action(repository);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private string GetInternalLockName(string name) {
|
||||
// Prefix the requested lock name by a constant and the tenant name.
|
||||
return String.Format("DistributedLock:{0}:{1}", _shellSettings.Name, name);
|
||||
|
Loading…
Reference in New Issue
Block a user