mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
Refactored minor internal affairs.
This commit is contained in:
@@ -69,7 +69,7 @@ namespace Orchard.Tasks.Locking.Services {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private bool TryAcquireLock(string name, TimeSpan? maxValidFor, TimeSpan? timeout, string machineName, int? threadId, out DistributedLock @lock) {
|
private bool TryAcquireLock(string name, TimeSpan? maxValidFor, TimeSpan? timeout, string machineName, int? threadId, out DistributedLock @lock) {
|
||||||
@lock = AcquireLockInternal(name, maxValidFor, machineName, threadId, timeout ?? TimeSpan.Zero);
|
@lock = AcquireLock(name, maxValidFor, machineName, threadId, timeout ?? TimeSpan.Zero);
|
||||||
if (@lock != null)
|
if (@lock != null)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
@@ -77,23 +77,21 @@ namespace Orchard.Tasks.Locking.Services {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private DistributedLock AcquireLock(string name, TimeSpan? maxValidFor, TimeSpan? timeout, string machineName, int? threadId) {
|
private DistributedLock AcquireLock(string name, TimeSpan? maxValidFor, TimeSpan? timeout, string machineName, int? threadId) {
|
||||||
var @lock = AcquireLockInternal(name, maxValidFor, machineName, threadId, timeout);
|
var @lock = AcquireLock(name, maxValidFor, machineName, threadId, timeout);
|
||||||
if (@lock != null)
|
if (@lock != null)
|
||||||
return @lock;
|
return @lock;
|
||||||
|
|
||||||
throw new TimeoutException(String.Format("Failed to acquire a lock named '{0}' within the specified timeout ('{1}').", name, timeout));
|
throw new TimeoutException(String.Format("Failed to acquire a lock named '{0}' within the specified timeout ('{1}').", name, timeout));
|
||||||
}
|
}
|
||||||
|
|
||||||
private DistributedLock AcquireLockInternal(string name, TimeSpan? maxValidFor, string machineName, int? threadId, TimeSpan? timeout = null) {
|
private DistributedLock AcquireLock(string name, TimeSpan? maxValidFor, string machineName, int? threadId, TimeSpan? timeout = null) {
|
||||||
try {
|
try {
|
||||||
DistributedLockRecord record = null;
|
DistributedLock @lock = null;
|
||||||
var acquired = Poll(() => (record = AcquireLockRecord(name, maxValidFor, machineName, threadId)) != null, timeout);
|
var acquired = Poll(() => (@lock = AcquireLockInternal(name, maxValidFor, machineName, threadId)) != null, timeout);
|
||||||
|
|
||||||
if (acquired) {
|
if (acquired) {
|
||||||
Logger.Debug("Successfully acquired a lock named '{0}'.", name);
|
Logger.Debug("Successfully acquired a lock named '{0}'.", name);
|
||||||
return threadId != null
|
return @lock;
|
||||||
? DistributedLock.ForThread(this, name, machineName, threadId.Value, record.Id.ToString())
|
|
||||||
: DistributedLock.ForMachine(this, name, machineName, record.Id.ToString());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex) {
|
catch (Exception ex) {
|
||||||
@@ -109,7 +107,7 @@ namespace Orchard.Tasks.Locking.Services {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private DistributedLockRecord AcquireLockRecord(string name, TimeSpan? maxValidFor, string machineName, int? threadId) {
|
private DistributedLock AcquireLockInternal(string name, TimeSpan? maxValidFor, string machineName, int? threadId) {
|
||||||
var childLifetimeScope = CreateChildLifetimeScope(name);
|
var childLifetimeScope = CreateChildLifetimeScope(name);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -157,7 +155,9 @@ namespace Orchard.Tasks.Locking.Services {
|
|||||||
if (!canAcquireLock)
|
if (!canAcquireLock)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
return record;
|
return threadId != null
|
||||||
|
? DistributedLock.ForThread(this, name, machineName, threadId.Value, record.Id.ToString())
|
||||||
|
: DistributedLock.ForMachine(this, name, machineName, record.Id.ToString());
|
||||||
}
|
}
|
||||||
catch (Exception ex) {
|
catch (Exception ex) {
|
||||||
Logger.Error(ex, "An error occurred while trying to acquire a lock.");
|
Logger.Error(ex, "An error occurred while trying to acquire a lock.");
|
||||||
|
|||||||
Reference in New Issue
Block a user