Exemplo n.º 1
0
 public void SetUp()
 {
     blobSettings = new LeaseBlockBlobSettings
     {
         ConnectionString = "UseDevelopmentStorage=true",
         ContainerName = "test" + Guid.NewGuid().ToString("N"),
         BlobPath = "lease.blob",
         ReAquirePreviousTestLease = false,
         RetryCount = 2,
         RetryInterval = TimeSpan.FromMilliseconds(250)
     };
     maximumStopDurationEstimateSeconds = 10;
     var storageAccount = CloudStorageAccount.Parse("UseDevelopmentStorage=true");
     var client = storageAccount.CreateCloudBlobClient();
     container = client.GetContainerReference(blobSettings.ContainerName);
     container.CreateIfNotExist();
     leaseBlob = container.GetBlobReference(blobSettings.BlobPath);
     leaseBlob.UploadByteArray(new byte[0]);
 }
Exemplo n.º 2
0
 public static void ReleaseLease(LeaseBlockBlobSettings blobSettings, string leaseId)
 {
     cancellationTokenSource.Cancel();
     leasingTask.Wait();
     var blob = GetBlobReference(blobSettings);
     try
     {
         blob.ReleaseLease(AccessCondition.GenerateLeaseCondition(leaseId), leaseRequestTimeout);
     }
     catch
     {
     }
     finally
     {
         BreakLease(blob);
     }
 }
Exemplo n.º 3
0
 private static CloudBlob GetBlobReference(LeaseBlockBlobSettings blobSettings)
 {
     var storageAccount = blobSettings.ConnectionString == null 
         ? new CloudStorageAccount(new StorageCredentialsAccountAndKey(blobSettings.BlobStorage, blobSettings.BlobStorageKey), blobSettings.UseHttps) 
         : CloudStorageAccount.Parse(blobSettings.ConnectionString);
     var client = storageAccount.CreateCloudBlobClient();
     var container = client.GetContainerReference(blobSettings.ContainerName);
     return container.GetBlobReference(blobSettings.BlobPath);
 }
Exemplo n.º 4
0
        public static string AquireLease(LeaseBlockBlobSettings blobSettings, int maximumStopDurationEstimateSeconds)
        {
            var blob = GetBlobReference(blobSettings);

            if (blobSettings.ReAquirePreviousTestLease)
                BreakTestLeaseIfExists(blob);

            var retryCount = blobSettings.RetryCount;
            var leaseId = blob.TryAcquireLease(maximumStopDurationEstimateSeconds);
            while (leaseId == null && retryCount > 0)
            {
                Thread.Sleep(blobSettings.RetryInterval);
                leaseId = blob.TryAcquireLease(maximumStopDurationEstimateSeconds);
                retryCount--;
            }
            return leaseId;
        }