Refactor renamed ILock to IDistributedLock.

Also refactored the IDistributedLockService to be more in line with general .NET patterns by return a boolean from the TryAcquire method.
This commit is contained in:
Sipke Schoorstra
2015-08-10 12:47:53 +01:00
parent f363ac2b5f
commit 5813647574
8 changed files with 27 additions and 29 deletions

View File

@@ -13,10 +13,10 @@ using Orchard.Validation;
namespace Orchard.TaskLease.Services {
/// <summary>
/// Provides a database driven implementation of <see cref="ILock" />
/// Provides a database driven implementation of <see cref="IDistributedLock" />
/// </summary>
[OrchardSuppressDependency("Orchard.Tasks.Locking.DefaultLock")]
public class DatabaseLock : ILock {
public class DatabaseLock : IDistributedLock {
private readonly ILifetimeScope _lifetimeScope;
private readonly IClock _clock;
private string _name;

View File

@@ -1,5 +1,4 @@
using System;
using Orchard.TaskLease.Models;
namespace Orchard.TaskLease.Services {
@@ -7,7 +6,7 @@ namespace Orchard.TaskLease.Services {
/// Describes a service to save and acquire task leases. A task lease can't be acquired by two different machines,
/// for a specific amount of time. Optionnally a State can be saved along with the lease.
/// </summary>
[Obsolete("Use Orchard.Tasks.Locking.ILockService instead.")]
[Obsolete("Use Orchard.Tasks.Locking.IDistributedLockService instead.")]
public interface ITaskLeaseService : IDependency {
/// <summary>

View File

@@ -9,7 +9,7 @@ namespace Orchard.TaskLease.Services {
/// <summary>
/// Provides a database driven implementation of <see cref="ITaskLeaseService" />
/// </summary>
[Obsolete("Use Orchard.Tasks.Locking.ILockService instead.")]
[Obsolete("Use Orchard.Tasks.Locking.DistributedLockService instead.")]
public class TaskLeaseService : ITaskLeaseService {
private readonly IRepository<TaskLeaseRecord> _repository;

View File

@@ -398,9 +398,9 @@
<Compile Include="Settings\CurrentSiteWorkContext.cs" />
<Compile Include="Settings\ResourceDebugMode.cs" />
<Compile Include="Tasks\Locking\DefaultLock.cs" />
<Compile Include="Tasks\Locking\ILockService.cs" />
<Compile Include="Tasks\Locking\ILock.cs" />
<Compile Include="Tasks\Locking\LockService.cs" />
<Compile Include="Tasks\Locking\IDistributedLockService.cs" />
<Compile Include="Tasks\Locking\IDistributedLock.cs" />
<Compile Include="Tasks\Locking\DistributedLockService.cs" />
<Compile Include="Themes\CurrentThemeWorkContext.cs" />
<Compile Include="Themes\ThemeManager.cs" />
<Compile Include="Time\CurrentTimeZoneWorkContext.cs" />

View File

@@ -1,7 +1,7 @@
using System;
namespace Orchard.Tasks.Locking {
public class DefaultLock : ILock {
public class DefaultLock : IDistributedLock {
public bool TryAcquire(string name, TimeSpan maxLifetime) {
return true;

View File

@@ -4,11 +4,11 @@ using Orchard.Environment;
using Orchard.Logging;
namespace Orchard.Tasks.Locking {
public class LockService : ILockService {
private readonly Work<ILock> _lock;
public class DistributedLockService : IDistributedLockService {
private readonly Work<IDistributedLock> _lock;
private readonly IMachineNameProvider _machineNameProvider;
public LockService(Work<ILock> @lock, IMachineNameProvider machineNameProvider) {
public DistributedLockService(Work<IDistributedLock> @lock, IMachineNameProvider machineNameProvider) {
_lock = @lock;
_machineNameProvider = machineNameProvider;
Logger = NullLogger.Instance;
@@ -16,10 +16,10 @@ namespace Orchard.Tasks.Locking {
public ILogger Logger { get; set; }
public ILock TryAcquireLock(string name, TimeSpan maxLifetime, TimeSpan timeout) {
public bool TryAcquireLock(string name, TimeSpan maxLifetime, TimeSpan timeout, out IDistributedLock @lock) {
var waitedTime = TimeSpan.Zero;
var waitTime = TimeSpan.FromMilliseconds(timeout.TotalMilliseconds / 10);
var @lock = _lock.Value;
@lock = _lock.Value;
bool acquired;
while (!(acquired = @lock.TryAcquire(name, maxLifetime)) && waitedTime < timeout) {
@@ -32,17 +32,16 @@ namespace Orchard.Tasks.Locking {
if (acquired) {
Logger.Debug(String.Format("Successfully acquired a lock named {0} on machine {1}.", name, machineName));
return @lock;
return true;
}
Logger.Debug(String.Format("Failed to acquire a lock named {0} on machine {1}.", name, machineName));
return null;
return false;
}
public ILock AcquireLock(string name, TimeSpan maxLifetime, TimeSpan timeout) {
var lockResult = TryAcquireLock(name, maxLifetime, timeout);
if (lockResult != null) return lockResult;
throw new TimeoutException(String.Format("No lock for \"{0}\" could not be acquired within {1} milliseconds.", name, timeout.TotalMilliseconds));
public IDistributedLock AcquireLock(string name, TimeSpan maxLifetime, TimeSpan timeout) {
IDistributedLock lockResult;
return TryAcquireLock(name, maxLifetime, timeout, out lockResult) ? lockResult : null;
}
}
}

View File

@@ -4,7 +4,7 @@ namespace Orchard.Tasks.Locking {
/// <summary>
/// Provides a lock on a provided name.
/// </summary>
public interface ILock : ITransientDependency, IDisposable {
public interface IDistributedLock : ITransientDependency, IDisposable {
/// <summary>
/// Tries to acquire a lock on the specified name.
/// </summary>

View File

@@ -4,24 +4,24 @@ namespace Orchard.Tasks.Locking {
/// <summary>
/// Provides distributed locking functionality.
/// </summary>
public interface ILockService : IDependency {
public interface IDistributedLockService : IDependency {
/// <summary>
/// Tries to acquire a lock on the specified name.
/// </summary>
/// <param name="name">The name to use for the lock.</param>
/// <param name="maxLifetime">The maximum amount of time the lock is allowed. This is a safety net in case the caller fails to release the lock.</param>
/// <param name="timeout">The amount of time to wait for the lock to be acquired before timing out</param>
/// <returns>Returns a lock if one was successfully acquired, null otherwise.</returns>
ILock TryAcquireLock(string name, TimeSpan maxLifetime, TimeSpan timeout);
/// <param name="timeout">The amount of time to wait for the lock to be acquired before timing out.</param>
/// <param name="lock">The acquired lock.</param>
/// <returns>Returns true if a lock was successfully acquired, false otherwise.</returns>
bool TryAcquireLock(string name, TimeSpan maxLifetime, TimeSpan timeout, out IDistributedLock @lock);
/// <summary>
/// Acquires a lock with the specified parameters.
/// </summary>
/// <param name="name">The name to use for the lock.</param>
/// <param name="maxLifetime">The maximum amount of time the lock is allowed. This is a safety net in case the caller fails to release the lock.</param>
/// <param name="timeout">The amount of time to wait for the lock to be acquired before timing out</param>
/// <returns>Returns a lock if one was successfully acquired.</returns>
/// <exception cref="TimeoutException">Thrown if the lock couldn't be acquired.</exception>
ILock AcquireLock(string name, TimeSpan maxLifetime, TimeSpan timeout);
/// <param name="timeout">The amount of time to wait for the lock to be acquired before timing out.</param>
/// <returns>Returns a lock if one was successfully acquired, null otherwise.</returns>
IDistributedLock AcquireLock(string name, TimeSpan maxLifetime, TimeSpan timeout);
}
}