private async Task DistributedHold() { try { if (logger.IsEnabled(LogLevel.Information)) { logger.LogInformation("EventBus Background Service is holding."); } if (Interlocked.CompareExchange(ref distributedHoldTimerLock, 1, 0) == 0) { foreach (var lockKV in Runners) { if (Runners.TryGetValue(lockKV.Key, out var lockId)) { var holdResult = await grainFactory.GetGrain <IWeightHoldLock>(lockKV.Key).Hold(lockId, lockHoldingSeconds); if (!holdResult) { if (ConsumerRunners.TryRemove(lockKV.Key, out var runner)) { runner.Close(); } Runners.TryRemove(lockKV.Key, out var _); } } } Interlocked.Exchange(ref distributedHoldTimerLock, 0); } } catch (Exception exception) { logger.LogError(exception.InnerException ?? exception, nameof(DistributedHold)); Interlocked.Exchange(ref distributedHoldTimerLock, 0); } }