diff --git a/src/Orchard.Tests/Tasks/DistributedLockServiceTests.cs b/src/Orchard.Tests/Tasks/DistributedLockServiceTests.cs
index 092585fe6..5b0074c27 100644
--- a/src/Orchard.Tests/Tasks/DistributedLockServiceTests.cs
+++ b/src/Orchard.Tests/Tasks/DistributedLockServiceTests.cs
@@ -82,10 +82,19 @@ namespace Orchard.Tests.Tasks {
_distributedLockService.ReleaseLock(@lock);
_session.Refresh(lockRecord);
Assert.That(lockRecord.Count, Is.EqualTo(1));
+ }
+ [Test]
+ public void ReleasingLockAndCountReachesZeroDeletesLock()
+ {
+ DistributedLock @lock;
+ _distributedLockService.TryAcquireLockForMachine(LockName, TimeSpan.FromSeconds(60), null, out @lock);
+
+ var lockId = Int32.Parse(@lock.Id);
_distributedLockService.ReleaseLock(@lock);
- _session.Refresh(lockRecord);
- Assert.That(lockRecord.Count, Is.EqualTo(0));
+ var lockRecord = _distributedLockRepository.Get(lockId);
+
+ Assert.That(lockRecord, Is.Null);
}
[Test]
diff --git a/src/Orchard/Tasks/Locking/Services/DistributedLockService.cs b/src/Orchard/Tasks/Locking/Services/DistributedLockService.cs
index 0639f1521..ed0fb21e0 100644
--- a/src/Orchard/Tasks/Locking/Services/DistributedLockService.cs
+++ b/src/Orchard/Tasks/Locking/Services/DistributedLockService.cs
@@ -52,12 +52,15 @@ namespace Orchard.Tasks.Locking.Services {
var record = repository.Get(lockId);
if (record == null)
- throw new ObjectDisposedException("@lock", "No lock record could be found for the specified lock to be released.");
+ throw new OrchardException(T("No lock record could be found for the specified lock to be released."));
if (record.Count <= 0)
- throw new ObjectDisposedException("@lock", "The specified lock has already been released.");
+ throw new OrchardException(T("The specified lock has already been released."));
record.Count--;
+
+ if(record.Count == 0)
+ repository.Delete(record);
}
catch (Exception ex) {
if (ex.IsFatal()) throw;
@@ -70,10 +73,7 @@ namespace Orchard.Tasks.Locking.Services {
private bool TryAcquireLock(string name, TimeSpan? maxValidFor, TimeSpan? timeout, string machineName, int? threadId, out DistributedLock @lock) {
@lock = AcquireLock(name, maxValidFor, machineName, threadId, timeout ?? TimeSpan.Zero);
- if (@lock != null)
- return true;
-
- return false;
+ return @lock != null;
}
private DistributedLock AcquireLock(string name, TimeSpan? maxValidFor, TimeSpan? timeout, string machineName, int? threadId) {
@@ -171,15 +171,15 @@ namespace Orchard.Tasks.Locking.Services {
///
/// Executes the specified function until it returns true, for the specified amount of time, or indefinitely if no timeout was given.
///
- /// The function to repeatedly execute until it returns true.
+ /// The operation to repeatedly execute until it returns true.
/// The amount of time to retry executing the function. If null is specified, the specified function is executed indefinitely until it returns true.
/// Returns true if the specified function returned true within the specified timeout, false otherwise.
- private bool Poll(Func pollFunc, TimeSpan? timeout) {
+ private bool Poll(Func operation, TimeSpan? timeout) {
var waitedTime = TimeSpan.Zero;
var waitTime = TimeSpan.FromMilliseconds(timeout.GetValueOrDefault().TotalMilliseconds / 10);
bool acquired;
- while (!(acquired = pollFunc()) && (timeout == null || waitedTime < timeout.Value)) {
+ while (!(acquired = operation()) && (timeout == null || waitedTime < timeout.Value)) {
Task.Delay(waitTime).ContinueWith(t => {
waitedTime += waitTime;
}).Wait();