public async Task <bool> SendHeartbeatAsync(DistributedLockSettings settings) { Check.NotNull(settings, nameof(settings)); if (settings is NullLockSettings) { return(await NullLockManager.SendHeartbeatAsync(settings).ConfigureAwait(false)); } try { using var scope = _serviceScopeFactory.CreateScope(); return(await SendHeartbeatAsync(settings.ResourceName, settings.UniqueId, scope.ServiceProvider) .ConfigureAwait(false)); } catch (Exception ex) { _logger.LogDebug( CoreEventIds.FailedToSendDistributedLockHeartbeat, ex, "Failed to send heartbeat for lock {lockName} ({lockUniqueId}). See inner exception for details.", settings.ResourceName, settings.UniqueId); return(false); } }
/// <summary> /// Ensures that the lock is still valid, otherwise tries to re-acquire it. /// </summary> /// <param name="cancellationToken"> /// A <see cref="CancellationToken" /> to observe while waiting for the task to complete. /// </param> /// <returns> /// A <see cref="Task" /> representing the asynchronous operation. /// </returns> public async Task RenewAsync(CancellationToken cancellationToken = default) { if (Status == DistributedLockStatus.Released) { throw new InvalidOperationException("This lock was explicitly released and cannot be renewed."); } await CheckIsStillLockedAsync().ConfigureAwait(false); if (Status == DistributedLockStatus.Acquired) { await _lockManager.SendHeartbeatAsync(_settings).ConfigureAwait(false); } else { await _lockManager.AcquireAsync(_settings, cancellationToken).ConfigureAwait(false); Status = DistributedLockStatus.Acquired; } }
public async Task <bool> SendHeartbeatAsync(DistributedLockSettings settings) { Check.NotNull(settings, nameof(settings)); if (settings is NullLockSettings) { return(await NullLockManager.SendHeartbeatAsync(settings).ConfigureAwait(false)); } try { using var scope = _serviceScopeFactory.CreateScope(); return(await SendHeartbeatAsync( settings.ResourceName, settings.UniqueId, scope.ServiceProvider) .ConfigureAwait(false)); } catch (Exception ex) { _logger.LogFailedToSendLockHeartbeat(settings, ex); return(false); } }