Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

always return true/false from Obtain() #29

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ obj/
csx/
Profiles/
*.ncrunch*
packages/
packages/
.vs/
35 changes: 23 additions & 12 deletions AzureDirectory/AzureLock.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;

namespace Lucene.Net.Store.Azure
{
Expand Down Expand Up @@ -44,7 +44,7 @@ override public bool IsLocked()
}
catch (StorageException webErr)
{
if (_handleWebException(blob, webErr))
if (HandleWebException(blob, webErr))
return IsLocked();
}
_leaseid = null;
Expand All @@ -61,27 +61,38 @@ public override bool Obtain()
{
_leaseid = blob.AcquireLease(TimeSpan.FromSeconds(60), _leaseid);
Debug.Print("AzureLock:Obtain({0}): AcquireLease : {1}", _lockFile, _leaseid);

// keep the lease alive by renewing every 30 seconds
long interval = (long)TimeSpan.FromSeconds(30).TotalMilliseconds;
_renewTimer = new Timer((obj) =>
_renewTimer = new Timer((obj) =>
{
try
{
AzureLock al = (AzureLock)obj;
al.Renew();
}
catch (Exception err) { Debug.Print(err.ToString()); }
catch (Exception err) { Debug.Print(err.ToString()); }
}, this, interval, interval);
}
return !String.IsNullOrEmpty(_leaseid);
return !string.IsNullOrEmpty(_leaseid);
}
catch (StorageException webErr)
catch (StorageException ex)
{
if (_handleWebException(blob, webErr))
if (HandleWebException(blob, ex))
{
return Obtain();
}
else
{
Debug.Print("AzureLock:Obtain unexpected exception {0}", ex.ToString());
return false;
}
}
catch (Exception ex)
{
Debug.Print("AzureLock:Obtain unexpected exception {0}", ex.ToString());
return false;
}
return false;
}

private Timer _renewTimer;
Expand Down Expand Up @@ -132,7 +143,7 @@ public override System.String ToString()
return String.Format("AzureLock@{0}.{1}", _lockFile, _leaseid);
}

private bool _handleWebException(ICloudBlob blob, StorageException err)
private bool HandleWebException(ICloudBlob blob, StorageException err)
{
if (err.RequestInformation.HttpStatusCode == 404 || err.RequestInformation.HttpStatusCode == 409)
{
Expand Down