-
Notifications
You must be signed in to change notification settings - Fork 289
Expand file tree
/
Copy pathProgram.cs
More file actions
110 lines (96 loc) · 3.51 KB
/
Program.cs
File metadata and controls
110 lines (96 loc) · 3.51 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.Threading;
using System.Threading.Tasks;
using Amazon;
using Amazon.Runtime;
using Greetings.Ports.Commands;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Paramore.Brighter;
using Paramore.Brighter.Extensions.DependencyInjection;
using Paramore.Brighter.MessageScheduler.Quartz;
using Paramore.Brighter.MessagingGateway.AWSSQS;
using Paramore.Brighter.MessagingGateway.AWSSQS.V4;
using Quartz;
using Serilog;
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.Enrich.FromLogContext()
.WriteTo.Console()
.CreateLogger();
var host = new HostBuilder()
.ConfigureServices((hostContext, services) =>
{
services
.AddSingleton<QuartzBrighterJob>()
.AddQuartz(opt =>
{
opt.SchedulerId = "QuartzBrighter";
opt.SchedulerName = "QuartzBrighter";
opt.UseSimpleTypeLoader();
opt.UseInMemoryStore();
})
.AddQuartzHostedService(opt =>
{
opt.WaitForJobsToComplete = true;
});
var awsConnection = new AWSMessagingGatewayConnection(new BasicAWSCredentials("test", "test"), RegionEndpoint.USEast1,
cfg =>
{
var serviceURL = "http://localhost:4566/";
if (!string.IsNullOrWhiteSpace(serviceURL))
{
cfg.ServiceURL = serviceURL;
}
});
var producerRegistry = new SnsProducerRegistryFactory(
awsConnection,
[
new SnsPublication
{
Topic = new RoutingKey(typeof(GreetingEvent).FullName.ToValidSNSTopicName()),
RequestType = typeof(GreetingEvent)
}
]
).Create();
services.AddBrighter()
.AddProducers((configure) =>
{
configure.ProducerRegistry = producerRegistry;
})
.UseScheduler(provider =>
{
var factory = provider.GetRequiredService<ISchedulerFactory>();
return new QuartzSchedulerFactory(
factory.GetScheduler().GetAwaiter().GetResult());
})
.AutoFromAssemblies([typeof(GreetingEvent).Assembly]);
services.AddHostedService<RunCommandProcessor>();
}
)
.UseConsoleLifetime()
.UseSerilog()
.Build();
Console.CancelKeyPress += (_, _) => host.StopAsync().Wait();
await host.RunAsync();
internal sealed class RunCommandProcessor(IAmACommandProcessor commandProcessor, ILogger<RunCommandProcessor> logger)
: BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
long loop = 0;
while (!stoppingToken.IsCancellationRequested)
{
loop++;
logger.LogInformation("Scheduling message #{Loop}", loop);
commandProcessor.Post(TimeSpan.FromSeconds(10), new GreetingEvent($"Scheduler message Ian #{loop}"));
if (loop % 100 != 0)
{
continue;
}
logger.LogInformation("Pausing for breath...");
await Task.Delay(4000, stoppingToken);
}
}
}