/// <summary>
        /// Acquire the singleton lock. If the lock cannot be acquired within the configured timeout,
        /// an exception will be thrown.
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task AcquireAsync(CancellationToken cancellationToken)
        {
            AcquireStartTime = DateTime.UtcNow;

            _lockHandle = await _singletonManager.LockAsync(_lockId, _functionInstanceId, _attribute, cancellationToken);

            AcquireEndTime = DateTime.UtcNow;
            IsHeld         = true;
        }
Exemple #2
0
        public async virtual Task ReleaseLockAsync(RenewableLockHandle handle, CancellationToken cancellationToken)
        {
            if (handle.LeaseRenewalTimer != null)
            {
                await handle.LeaseRenewalTimer.StopAsync(cancellationToken);
            }

            await _lockManager.ReleaseLockAsync(handle.InnerLock, cancellationToken);

            string msg = string.Format(CultureInfo.InvariantCulture, "Singleton lock released ({0})", handle.InnerLock.LockId);

            _logger?.LogDebug(msg);
        }
        public async virtual Task <RenewableLockHandle> LockAsync(string lockId, string functionInstanceId, SingletonAttribute attribute, CancellationToken cancellationToken)
        {
            RenewableLockHandle lockHandle = await TryLockAsync(lockId, functionInstanceId, attribute, cancellationToken);

            if (lockHandle == null)
            {
                TimeSpan acquisitionTimeout = attribute.LockAcquisitionTimeout != null
                    ? TimeSpan.FromSeconds(attribute.LockAcquisitionTimeout.Value) :
                                              _config.LockAcquisitionTimeout;

                throw new TimeoutException(string.Format("Unable to acquire singleton lock blob lease for blob '{0}' (timeout of {1} exceeded).", lockId, acquisitionTimeout.ToString("g")));
            }

            return(lockHandle);
        }