/// <summary> /// Tries to renew the lock lease. /// </summary> /// <param name="lockLease">The lock lease.</param> /// <returns>True if operation is successful, otherwise false.</returns> public static async Task <bool> TryRenewAsync(this ILockLease lockLease) { try { await lockLease.RenewAsync(); return(true); } catch (Exception) { return(false); } }
/// <inheritdoc /> public async Task ReleaseLock(ILockLease @lock) { var lockFileBlob = BlobContainer.GetBlobClient($"locks/{@lock.Name}.lock"); var lockFileLease = lockFileBlob.GetBlobLeaseClient(@lock.LeaseId); await lockFileLease.ReleaseAsync(); // the following code that tries to delete had side-effects. // Not deleting is not problem whatsoever but // there are multiple zero byte files that are never deleted on storage. //try { // var response = await lockFileBlob.DeleteIfExistsAsync(); //} catch {; } }
/// <summary> /// Tries to renew the lock lease. /// </summary> /// <param name="lockLease">The lock lease.</param> /// <param name="cancellationToken">Propagates notification that operations should be canceled.</param> /// <param name="intervalInSeconds">Specifies the duration in seconds to wait for a renew attempt.</param> /// <returns>The task that represents the asynchronous operation result for renewing the lease.</returns> public static async Task TryRenewUntilCancelled(this ILockLease lockLease, CancellationToken cancellationToken, int intervalInSeconds = 10) { while (!cancellationToken.IsCancellationRequested) { // Immediately try to renew the lease. var renewed = await lockLease.TryRenewAsync(); if (!renewed) { return; } // Wait for an amount of time before trying again to renew the lease. await Task.Delay(TimeSpan.FromSeconds(intervalInSeconds), cancellationToken); } }
/// <summary> /// Successful <see cref="LockLeaseResult"/> factory. /// </summary> /// <param name="lock">The lock</param> /// <returns></returns> public static LockLeaseResult Success(ILockLease @lock) => new LockLeaseResult(@lock, true);
private LockLeaseResult(ILockLease @lock, bool ok) { Lock = @lock; Ok = ok; }
/// <inheritdoc /> public async Task ReleaseLock(ILockLease @lock) { var lockFileBlob = BlobContainer.GetBlobClient($"locks/{@lock.Name}.lock"); var lockFileLease = lockFileBlob.GetBlobLeaseClient(@lock.LeaseId); await lockFileLease.ReleaseAsync(); }
/// <inheritdoc/> public Task ReleaseLock(ILockLease @lock) => Task.CompletedTask;
/// <inheritdoc /> public Task ReleaseLock(ILockLease @lock) { _signal.Release(); _logger.LogInformation("Item with lease id {0} released the lock.", @lock.LeaseId); return(Task.CompletedTask); }
/// <inheritdoc/> public async Task ReleaseLock(ILockLease @lock) { var query = @"DELETE FROM [work].[Lock] WHERE ([Name] = {0} AND [Id] < {1}) OR [ExpirationDate] < GetDate();"; await _dbContext.Database.ExecuteSqlRawAsync(query, @lock.Name, Base64Id.Parse(@lock.LeaseId).Id); }
/// <inheritdoc/> public async Task ReleaseLock(ILockLease @lock) => await _dbContext.Database.ExecuteSqlRawAsync(_queryDescriptor.ReleaseLock, @lock.Name, Base64Id.Parse(@lock.LeaseId).Id);