Ensure CleanupService executes on one node at a time.
This commit is contained in:
parent
8c6a01a363
commit
369ebb6db3
|
|
@ -3,6 +3,7 @@ using System.Threading;
|
|||
using System.Threading.Tasks;
|
||||
using Elsa.Retention.Jobs;
|
||||
using Elsa.Retention.Options;
|
||||
using Elsa.Services;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
|
@ -16,12 +17,14 @@ namespace Elsa.Retention.HostedServices
|
|||
public class CleanupService : BackgroundService
|
||||
{
|
||||
private readonly IServiceScopeFactory _serviceScopeFactory;
|
||||
private readonly IDistributedLockProvider _distributedLockProvider;
|
||||
private readonly ILogger<CleanupService> _logger;
|
||||
private readonly TimeSpan _interval;
|
||||
|
||||
public CleanupService(IOptions<CleanupOptions> options, IServiceScopeFactory serviceScopeFactory, ILogger<CleanupService> logger)
|
||||
public CleanupService(IOptions<CleanupOptions> options, IServiceScopeFactory serviceScopeFactory, IDistributedLockProvider distributedLockProvider, ILogger<CleanupService> logger)
|
||||
{
|
||||
_serviceScopeFactory = serviceScopeFactory;
|
||||
_distributedLockProvider = distributedLockProvider;
|
||||
_logger = logger;
|
||||
_interval = options.Value.SweepInterval.ToTimeSpan();
|
||||
}
|
||||
|
|
@ -34,7 +37,8 @@ namespace Elsa.Retention.HostedServices
|
|||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.Delay(_interval, stoppingToken);
|
||||
|
||||
await using var handle = await _distributedLockProvider.AcquireLockAsync(nameof(CleanupService), cancellationToken: stoppingToken);
|
||||
|
||||
try
|
||||
{
|
||||
await job.ExecuteAsync(stoppingToken);
|
||||
|
|
|
|||
Loading…
Reference in a new issue