Port https://github.com/dotnet/core-setup/pull/168 to CLI.
This commit is contained in:
parent
4f1d9c4e23
commit
80332c8fc3
2 changed files with 36 additions and 7 deletions
|
@ -15,6 +15,8 @@ namespace Microsoft.DotNet.Cli.Build
|
|||
|
||||
private static string Channel { get; set; }
|
||||
|
||||
private static string CommitHash { get; set; }
|
||||
|
||||
private static string CliNuGetVersion { get; set; }
|
||||
|
||||
private static string SharedFrameworkNugetVersion { get; set; }
|
||||
|
@ -28,6 +30,7 @@ namespace Microsoft.DotNet.Cli.Build
|
|||
CliNuGetVersion = c.BuildContext.Get<BuildVersion>("BuildVersion").NuGetVersion;
|
||||
SharedFrameworkNugetVersion = CliDependencyVersions.SharedFrameworkVersion;
|
||||
Channel = c.BuildContext.Get<string>("Channel");
|
||||
CommitHash = c.BuildContext.Get<string>("CommitHash");
|
||||
|
||||
return c.Success();
|
||||
}
|
||||
|
@ -49,7 +52,7 @@ namespace Microsoft.DotNet.Cli.Build
|
|||
if (CheckIfAllBuildsHavePublished())
|
||||
{
|
||||
string targetContainer = $"{AzurePublisher.Product.Sdk}/{Channel}/";
|
||||
string targetVersionFile = $"{targetContainer}{CliNuGetVersion}";
|
||||
string targetVersionFile = $"{targetContainer}{CommitHash}";
|
||||
string semaphoreBlob = $"{targetContainer}/publishSemaphore";
|
||||
AzurePublisherTool.CreateBlobIfNotExists(semaphoreBlob);
|
||||
string leaseId = AzurePublisherTool.AcquireLeaseOnBlob(semaphoreBlob);
|
||||
|
@ -64,7 +67,7 @@ namespace Microsoft.DotNet.Cli.Build
|
|||
}
|
||||
else
|
||||
{
|
||||
Regex versionFileRegex = new Regex(@"(?<version>\d\.\d\.\d)-(?<release>.*)?");
|
||||
Regex versionFileRegex = new Regex(@"(?<CommitHash>[\w\d]{40})");
|
||||
|
||||
// Delete old version files
|
||||
AzurePublisherTool.ListBlobs(targetContainer)
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue