Exemplo n.º 1
0
 public AutoRenewLease(CloudBlob blob)
 {
     this.blob = blob;
     blob.Container.CreateIfNotExist();
     try
     {
         blob.UploadByteArray(new byte[0], new BlobRequestOptions { AccessCondition = AccessCondition.IfNoneMatch("*") });
     }
     catch (StorageClientException e)
     {
         if (e.ErrorCode != StorageErrorCode.BlobAlreadyExists
             && e.StatusCode != HttpStatusCode.PreconditionFailed) // 412 from trying to modify a blob that's leased
         {
             throw;
         }
     }
     leaseId = blob.TryAcquireLease();
     if (HasLease)
     {
         renewalThread = new Thread(() =>
         {
             Thread.Sleep(TimeSpan.FromSeconds(40));
             blob.RenewLease(leaseId);
         });
         renewalThread.Start();
     }
 }
Exemplo n.º 2
0
        public AutoRenewLease(ILoggerFactory loggerFactory, LoggerLevel logLevel, CloudBlob blob, int renewLeaseSeconds = 40, int leaseLengthSeconds = 90)
        {
            _logger = loggerFactory.Create(GetType(), logLevel);
            var autoRenewLease = this;
            _blob = blob;
            blob.Container.CreateIfNotExist();
            try
            {
                blob.UploadByteArray(new byte[0], new BlobRequestOptions { AccessCondition = AccessCondition.IfNoneMatch("*")});
            }
            catch (StorageClientException ex)
            {
                if (ex.ErrorCode != StorageErrorCode.BlobAlreadyExists)
                {
                    if (ex.StatusCode != HttpStatusCode.PreconditionFailed)
                        throw;
                }
            }
            LeaseId = blob.TryAcquireLease(leaseLengthSeconds);
            if (!HasLease)
                return;
            _cancellationTokenSource = new CancellationTokenSource();
            _resetEvent = new ManualResetEvent(false);
            Task.Factory.StartNew(() =>
            {
                try
                {
                    while (true)
                    {
                        _resetEvent.WaitOne(TimeSpan.FromSeconds(renewLeaseSeconds));
                        if (_cancellationTokenSource.IsCancellationRequested)
                            break;

                        blob.RenewLease(autoRenewLease.LeaseId);
                    }
                }
                catch (Exception e)
                {
                    LeaseId = null; // Release the lease
                    _logger.Error("Error renewing blob lease", e);
                }
            }, _cancellationTokenSource.Token);
        }
Exemplo n.º 3
0
 private static void AutoRenewLease(CloudBlob blob, string leaseId, int maximumStopDurationEstimateSeconds, CancellationToken cancellationToken)
 {
     try
     {
         const int sleepSeconds = 10;
         var totalSleepSeconds = 0;
         while (true)
         {
             for (var i = 0; i < sleepSeconds; i++ )
             {
                 Thread.Sleep(TimeSpan.FromSeconds(1));
                 if (cancellationToken.IsCancellationRequested)
                     break;
             }
             totalSleepSeconds += sleepSeconds;
             if (totalSleepSeconds >= maximumStopDurationEstimateSeconds)
                 break;
             blob.RenewLease(AccessCondition.GenerateLeaseCondition(leaseId), leaseRequestTimeout);
         }
     }
     catch
     {
     }
     finally
     {
         try
         {
             BreakLease(blob);
         }
         catch
         {
         }
     }
 }