Exemplo n.º 1
0
            public async Task <TaskSeriesCommandResult> ExecuteAsync(CancellationToken cancellationToken)
            {
                // Exceptions wil propagate
                bool executionSucceeded = await _lockManager.RenewAsync(_lock, cancellationToken);

                TimeSpan delay = _speedupStrategy.GetNextDelay(executionSucceeded: true);

                return(new TaskSeriesCommandResult(wait: Task.Delay(delay)));
            }
    public async Task <TaskSeriesCommandResult> ExecuteAsync(CancellationToken cancellationToken)
    {
        // Exceptions will propagate
        bool renewalSucceeded = await _lockManager.RenewAsync(_lock, cancellationToken);

        TimeSpan delay = _delayStrategy.GetNextDelay(renewalSucceeded);

        return(new TaskSeriesCommandResult(Task.Delay(delay)));
    }
        private async Task AcquireOrRenewLeaseAsync()
        {
            if (_hostId == null)
            {
                _hostId = await _hostIdProvider.GetHostIdAsync(CancellationToken.None);
            }

            string lockName = GetBlobName(_hostId);

            DateTime requestStart = DateTime.UtcNow;

            if (HasLease)
            {
                try
                {
                    await _lockManager.RenewAsync(LockHandle, CancellationToken.None);

                    _lastRenewal        = DateTime.UtcNow;
                    _lastRenewalLatency = _lastRenewal - requestStart;
                }
                catch
                {
                    // The lease was 'stolen'. Log details for debugging.
                    string lastRenewalFormatted         = _lastRenewal.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ", CultureInfo.InvariantCulture);
                    int    millisecondsSinceLastSuccess = (int)(DateTime.UtcNow - _lastRenewal).TotalMilliseconds;
                    int    lastRenewalMilliseconds      = (int)_lastRenewalLatency.TotalMilliseconds;
                    ProcessLeaseError($"Another host has acquired the lease. The last successful renewal completed at {lastRenewalFormatted} ({millisecondsSinceLastSuccess} milliseconds ago) with a duration of {lastRenewalMilliseconds} milliseconds.");
                }
            }
            else
            {
                string proposedLeaseId = _websiteInstanceId;
                LockHandle = await _lockManager.TryLockAsync(null, lockName, _websiteInstanceId, proposedLeaseId, _leaseTimeout, CancellationToken.None);

                if (LockHandle == null)
                {
                    // We didn't have the lease and failed to acquire it. Common if somebody else already has it.
                    // This is normal and does not warrant any logging.
                    return;
                }

                _lastRenewal        = DateTime.UtcNow;
                _lastRenewalLatency = _lastRenewal - requestStart;

                string message = $"Host lock lease acquired by instance ID '{_websiteInstanceId}'.";
                _logger.LogInformation(message);

                // We've successfully acquired the lease, change the timer to use our renewal interval
                SetTimerInterval(_renewalInterval);
            }
        }