-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
71 lines (54 loc) · 1.66 KB
/
Copy pathProgram.cs
File metadata and controls
71 lines (54 loc) · 1.66 KB
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
using SquidStd.Abstractions.Attributes;
using SquidStd.Core.Interfaces.Events;
using SquidStd.Core.Interfaces.Jobs;
using SquidStd.Core.Interfaces.Scheduling;
using SquidStd.Generators.Events;
using SquidStd.Services.Core.Extensions;
using SquidStd.Services.Core.Services.Bootstrap;
var bootstrap = SquidStdBootstrap.Create(
new()
{
ConfigName = "squidstd",
RootDirectory = AppContext.BaseDirectory
}
);
// The cron scheduler and timer wheel are opt-in.
bootstrap.ConfigureServices(
container => container
.RegisterSchedulerServices()
.RegisterGeneratedEventListeners()
);
await bootstrap.StartAsync();
#region step-1
var eventBus = bootstrap.Resolve<IEventBus>();
await eventBus.PublishAsync(new PingEvent("hello"), CancellationToken.None);
#endregion
#region step-2
var jobs = bootstrap.Resolve<IJobSystem>();
await jobs.ScheduleAsync(() => Console.WriteLine("job ran on a worker thread"));
#endregion
#region step-3
var cron = bootstrap.Resolve<ICronScheduler>();
cron.Schedule(
"heartbeat",
"*/5 * * * *",
_ =>
{
Console.WriteLine("cron tick");
return Task.CompletedTask;
}
);
#endregion
await bootstrap.StopAsync();
/// <summary>A sample event published on the bus.</summary>
public sealed record PingEvent(string Message) : IEvent;
/// <summary>Handles <see cref="PingEvent" />.</summary>
[RegisterEventListener]
public sealed class PingListener : IEventListener<PingEvent>
{
public Task HandleAsync(PingEvent eventData, CancellationToken cancellationToken)
{
Console.WriteLine($"received: {eventData.Message}");
return Task.CompletedTask;
}
}