📄
src/Api/CleanupJob.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System; using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Quartz; using Slopper.Domain; namespace Slopper.Api; [DisallowConcurrentExecution] public sealed class CleanupJob(ILogger<CleanupJob> logger, Cleaner cleaner) : IJob { public static JobKey Key { get; } = new(nameof(CleanupJob)); public async Task Execute(IJobExecutionContext context) { logger.LogDebug("Running cleanup job"); try { await cleaner.Cleanup(context.CancellationToken); } catch (Exception ex) when (!context.CancellationToken.IsCancellationRequested) { throw new JobExecutionException(ex, refireImmediately: false); } } } public static class CleanupJobExtensions { extension(IServiceCollectionQuartzConfigurator quartz) { public IServiceCollectionQuartzConfigurator AddCleanupJob() => quartz .AddJob<CleanupJob>(CleanupJob.Key, options => options.StoreDurably()) .AddTrigger(options => options.ForJob(CleanupJob.Key).WithCronSchedule("0 0 * * * ?")); } }