| | | 1 | | using JetBrains.Annotations; |
| | | 2 | | using Medallion.Threading; |
| | | 3 | | using Microsoft.Extensions.Logging; |
| | | 4 | | |
| | | 5 | | namespace Elsa.Workflows.Runtime.Distributed; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Decorator class that adds distributed locking to the Workflow Definitions Reloader. |
| | | 9 | | /// </summary> |
| | | 10 | | [UsedImplicitly] |
| | 7 | 11 | | public class DistributedWorkflowDefinitionsReloader( |
| | 7 | 12 | | IWorkflowDefinitionsReloader inner, |
| | 7 | 13 | | IDistributedLockProvider distributedLockProvider, |
| | 7 | 14 | | ILogger<DistributedWorkflowDefinitionsReloader> logger) : IWorkflowDefinitionsReloader |
| | | 15 | | { |
| | | 16 | | private const string LockKey = "WorkflowDefinitionsReloader"; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// This ensures that only one instance of the application can reload workflow definitions at a time, preventing pot |
| | | 20 | | /// </summary> |
| | | 21 | | public async Task ReloadWorkflowDefinitionsAsync(CancellationToken cancellationToken = default) |
| | | 22 | | { |
| | 6 | 23 | | await using var distributedLock = await distributedLockProvider.TryAcquireLockAsync( |
| | 6 | 24 | | LockKey, |
| | 6 | 25 | | TimeSpan.Zero, |
| | 6 | 26 | | cancellationToken); |
| | | 27 | | |
| | 6 | 28 | | if (distributedLock == null) |
| | | 29 | | { |
| | 0 | 30 | | logger.LogInformation("Could not acquire lock for workflow definitions reload. Another instance is already r |
| | 0 | 31 | | return; |
| | | 32 | | } |
| | | 33 | | |
| | 6 | 34 | | await inner.ReloadWorkflowDefinitionsAsync(cancellationToken); |
| | 6 | 35 | | } |
| | | 36 | | } |