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 { namespace Orchard.TaskLease.Services {
/// <summary> /// <summary>
/// Provides a database driven implementation of <see cref="ILock" /> /// Provides a database driven implementation of <see cref="IDistributedLock" />
/// </summary> /// </summary>
[OrchardSuppressDependency("Orchard.Tasks.Locking.DefaultLock")] [OrchardSuppressDependency("Orchard.Tasks.Locking.DefaultLock")]
public class DatabaseLock : ILock { public class DatabaseLock : IDistributedLock {
private readonly ILifetimeScope _lifetimeScope; private readonly ILifetimeScope _lifetimeScope;
private readonly IClock _clock; private readonly IClock _clock;
private string _name; private string _name;

View File

@@ -1,5 +1,4 @@
using System; using System;
using Orchard.TaskLease.Models;
namespace Orchard.TaskLease.Services { 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, /// 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. /// for a specific amount of time. Optionnally a State can be saved along with the lease.
/// </summary> /// </summary>
[Obsolete("Use Orchard.Tasks.Locking.ILockService instead.")] [Obsolete("Use Orchard.Tasks.Locking.IDistributedLockService instead.")]
public interface ITaskLeaseService : IDependency { public interface ITaskLeaseService : IDependency {
/// <summary> /// <summary>

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -4,24 +4,24 @@ namespace Orchard.Tasks.Locking {
/// <summary> /// <summary>
/// Provides distributed locking functionality. /// Provides distributed locking functionality.
/// </summary> /// </summary>
public interface ILockService : IDependency { public interface IDistributedLockService : IDependency {
/// <summary> /// <summary>
/// Tries to acquire a lock on the specified name. /// Tries to acquire a lock on the specified name.
/// </summary> /// </summary>
/// <param name="name">The name to use for the lock.</param> /// <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="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> /// <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> /// <param name="lock">The acquired lock.</param>
ILock TryAcquireLock(string name, TimeSpan maxLifetime, TimeSpan timeout); /// <returns>Returns true if a lock was successfully acquired, false otherwise.</returns>
bool TryAcquireLock(string name, TimeSpan maxLifetime, TimeSpan timeout, out IDistributedLock @lock);
/// <summary> /// <summary>
/// Acquires a lock with the specified parameters. /// Acquires a lock with the specified parameters.
/// </summary> /// </summary>
/// <param name="name">The name to use for the lock.</param> /// <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="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> /// <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> /// <returns>Returns a lock if one was successfully acquired, null otherwise.</returns>
/// <exception cref="TimeoutException">Thrown if the lock couldn't be acquired.</exception> IDistributedLock AcquireLock(string name, TimeSpan maxLifetime, TimeSpan timeout);
ILock AcquireLock(string name, TimeSpan maxLifetime, TimeSpan timeout);
} }
} }