-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
44 lines (35 loc) · 1.51 KB
/
Program.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
using Tingle.PeriodicTasks;
var builder = WebApplication.CreateBuilder(args);
// register IDistributedLockProvider
var path = builder.Configuration.GetValue<string?>("DistributedLocking:FilePath")
?? Path.Combine(builder.Environment.ContentRootPath, "distributed-locks");
builder.Services.AddSingleton<Medallion.Threading.IDistributedLockProvider>(provider =>
{
return new Medallion.Threading.FileSystem.FileDistributedSynchronizationProvider(Directory.CreateDirectory(path));
});
builder.Services.AddPeriodicTasks(builder =>
{
builder.AddTask<CookingTask>(o => o.Schedule = "0 */10 * * * *");
builder.AddTask<WashingTask>(o => o.Schedule = "0 */15 * * * *");
builder.AddAspNetCore();
});
var app = builder.Build();
app.MapGet("/", () => "Hello World!");
app.MapGroup("/periodic-tasks").MapPeriodicTasks();
await app.RunAsync();
class CookingTask(ILogger<CookingTask> logger) : IPeriodicTask
{
public async Task ExecuteAsync(PeriodicTaskExecutionContext context, CancellationToken cancellationToken = default)
{
logger.LogInformation("The food is ready to eat");
await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
}
}
class WashingTask(ILogger<WashingTask> logger) : IPeriodicTask
{
public async Task ExecuteAsync(PeriodicTaskExecutionContext context, CancellationToken cancellationToken = default)
{
logger.LogInformation("I will do the cleaning later. Have you eaten yet?");
await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
}
}