public async Task <TaskSeriesCommandResult> ExecuteAsync(CancellationToken cancellationToken) { TimeSpan delay; try { _trace.Verbose(string.Format(CultureInfo.InvariantCulture, "Renewing Singleton lock ({0})", _lockId), source: TraceSource.Execution); AccessCondition condition = new AccessCondition { LeaseId = _leaseId }; await _leaseBlob.RenewLeaseAsync(condition, null, null, cancellationToken); // The next execution should occur after a normal delay. delay = _speedupStrategy.GetNextDelay(executionSucceeded: true); } catch (StorageException exception) { if (exception.IsServerSideError()) { // The next execution should occur more quickly (try to renew the lease before it expires). delay = _speedupStrategy.GetNextDelay(executionSucceeded: false); } else { // If we've lost the lease or cannot restablish it, we want to fail any // in progress function execution throw; } } return(new TaskSeriesCommandResult(wait: Task.Delay(delay))); }
public async Task <TaskSeriesCommandResult> ExecuteAsync(CancellationToken cancellationToken) { TimeSpan delay; try { AccessCondition condition = new AccessCondition { LeaseId = _leaseId }; DateTimeOffset requestStart = DateTimeOffset.UtcNow; await _leaseBlob.RenewLeaseAsync(condition, null, null, cancellationToken); _lastRenewal = DateTime.UtcNow; _lastRenewalLatency = _lastRenewal - requestStart; // The next execution should occur after a normal delay. delay = _speedupStrategy.GetNextDelay(executionSucceeded: true); } catch (StorageException exception) { if (exception.IsServerSideError()) { // The next execution should occur more quickly (try to renew the lease before it expires). delay = _speedupStrategy.GetNextDelay(executionSucceeded: false); string msg = string.Format(CultureInfo.InvariantCulture, "Singleton lock renewal failed for blob '{0}' with error code {1}. Retry renewal in {2} milliseconds.", _lockId, FormatErrorCode(exception), delay.TotalMilliseconds); _trace.Warning(msg, source: TraceSource.Execution); _logger?.LogWarning(msg); } else { // Log the details we've been accumulating to help with debugging this scenario int leasePeriodMilliseconds = (int)_leasePeriod.TotalMilliseconds; string lastRenewalFormatted = _lastRenewal.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ", CultureInfo.InvariantCulture); int millisecondsSinceLastSuccess = (int)(DateTime.UtcNow - _lastRenewal).TotalMilliseconds; int lastRenewalMilliseconds = (int)_lastRenewalLatency.TotalMilliseconds; string msg = string.Format(CultureInfo.InvariantCulture, "Singleton lock renewal failed for blob '{0}' with error code {1}. The last successful renewal completed at {2} ({3} milliseconds ago) with a duration of {4} milliseconds. The lease period was {5} milliseconds.", _lockId, FormatErrorCode(exception), lastRenewalFormatted, millisecondsSinceLastSuccess, lastRenewalMilliseconds, leasePeriodMilliseconds); _trace.Error(msg); _logger?.LogError(msg); // If we've lost the lease or cannot re-establish it, we want to fail any // in progress function execution throw; } } return(new TaskSeriesCommandResult(wait: Task.Delay(delay))); }