private async Task DistributedHold() { try { if (Interlocked.CompareExchange(ref distributedHoldTimerLock, 1, 0) == 0) { foreach (var lockKV in LockDict) { if (LockDict.TryGetValue(lockKV.Key, out var lockId)) { var holdResult = await grainFactory.GetGrain <IWeightHoldLock>(lockKV.Key).Hold(lockId, lockHoldingSeconds); if (!holdResult && NodeRunnerDict.TryGetValue(lockKV.Key, out var consumerRunners)) { consumerRunners.ForEach(runner => runner.Close()); NodeRunnerDict.TryRemove(lockKV.Key, out var _); LockDict.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); } }
private async Task DistributedStart() { try { if (Interlocked.CompareExchange(ref distributedMonitorTimeLock, 1, 0) == 0) { foreach (var node in rabbitEventBusOptions.Nodes) { if (!NodeRunnerDict.TryGetValue(node, out var consumerRunners)) { int weight = NodeRunnerDict.Count > 0 ? 100 : 99; var(isOk, lockId, expectMillisecondDelay) = await grainFactory.GetGrain <IWeightHoldLock>(node).Lock(weight, lockHoldingSeconds); if (!isOk && expectMillisecondDelay > 0) { await Task.Delay(expectMillisecondDelay + 100); (isOk, lockId, expectMillisecondDelay) = await grainFactory.GetGrain <IWeightHoldLock>(node).Lock(weight, lockHoldingSeconds); if (isOk) { await Task.Delay(10 * 1000); } } if (isOk) { await Start(node, rabbitEventBusOptions.Nodes); LockDict.TryAdd(node, lockId); break; } } } Interlocked.Exchange(ref distributedMonitorTimeLock, 0); } } catch (Exception exception) { logger.LogError(exception.InnerException ?? exception, nameof(DistributedStart)); Interlocked.Exchange(ref distributedMonitorTimeLock, 0); } }