This commit is contained in:
Eric Erhardt 2016-06-21 19:18:48 -05:00
parent 4f1d9c4e23
commit 80332c8fc3
2 changed files with 36 additions and 7 deletions

View file

@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
@ -102,12 +104,36 @@ namespace Microsoft.DotNet.Cli.Build
return blobFiles.Results.Select(bf => bf.Uri.PathAndQuery.Replace($"/{s_dotnetBlobContainerName}/", string.Empty));
}
public string AcquireLeaseOnBlob(string blob)
public string AcquireLeaseOnBlob(
string blob,
TimeSpan? maxWaitDefault = null,
TimeSpan? delayDefault = null)
{
CloudBlockBlob cloudBlob = _blobContainer.GetBlockBlobReference(blob);
Task<string> task = cloudBlob.AcquireLeaseAsync(TimeSpan.FromMinutes(1), null);
task.Wait();
return task.Result;
TimeSpan maxWait = maxWaitDefault ?? TimeSpan.FromSeconds(120);
TimeSpan delay = delayDefault ?? TimeSpan.FromMilliseconds(500);
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
// This will throw an exception with HTTP code 409 when we cannot acquire the lease
// But we should block until we can get this lease, with a timeout (maxWaitSeconds)
while (stopWatch.ElapsedMilliseconds < maxWait.TotalMilliseconds)
{
try
{
CloudBlockBlob cloudBlob = _blobContainer.GetBlockBlobReference(blob);
Task<string> task = cloudBlob.AcquireLeaseAsync(TimeSpan.FromMinutes(1), null);
task.Wait();
return task.Result;
}
catch (Exception e)
{
Console.WriteLine($"Retrying lease acquisition on {blob}, {e.Message}");
Thread.Sleep(delay);
}
}
throw new Exception($"Unable to acquire lease on {blob}");
}
public void ReleaseLeaseOnBlob(string blob, string leaseId)