-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSchedulerService.cs
74 lines (63 loc) · 2.88 KB
/
SchedulerService.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace Scheduler
{
public class SchedulerService(IServiceProvider serviceProvider) : BackgroundService
{
private static readonly Queue<ScheduledJob> PendingJobs = new();
private static readonly object PendingJobsLock = new();
/// <summary>
/// Schedules a job to be executed asynchronously.
/// </summary>
/// <typeparam name="TJob">The type of the job to be scheduled.</typeparam>
/// <param name="job">The job to be scheduled.</param>
/// <remarks>
/// Dynamically scheduled jobs will neither resume nor persist when the application restarts.
/// </remarks>
public static void ScheduleJobAsync<TJob>(TJob job) where TJob : ScheduledJob
{
lock (PendingJobsLock) PendingJobs.Enqueue(job);
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await Task.Yield();
await Execute(stoppingToken);
}
private async Task Execute(CancellationToken stoppingToken)
{
using var scope = serviceProvider.CreateScope();
var scopedServiceProvider = scope.ServiceProvider;
while (!stoppingToken.IsCancellationRequested)
{
var noPendingTasks = false;
ScheduledJob? job = null;
lock (PendingJobsLock)
{
PendingJobs.TryDequeue(out var pendingJob);
if (pendingJob != null) job = pendingJob;
else noPendingTasks = true;
}
if (noPendingTasks || job == null) await Task.Delay(1000, stoppingToken);
else _ = ScheduleJob(scopedServiceProvider, job);
}
}
private static async Task ScheduleJob(IServiceProvider serviceProvider, ScheduledJob job)
{
while (!job.CancellationToken.IsCancellationRequested)
{
var nextExecutionTime = job.GetNextExecutionSchedule();
var universalNextExecutionTime = nextExecutionTime.ToUniversalTime();
var now = DateTime.UtcNow;
var nextTick = universalNextExecutionTime < now ? TimeSpan.Zero : universalNextExecutionTime - now;
await Task.Delay(nextTick, job.CancellationToken);
if (job.CancellationToken.IsCancellationRequested) continue;
await job.Execute(serviceProvider);
job.PreviousExecutionDateTime = nextExecutionTime;
}
}
}
public static class PrepareSchedulerExtension
{
public static void StartScheduler(this IServiceCollection services) => services.AddHostedService<SchedulerService>();
}
}