-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathProgram.cs
62 lines (48 loc) · 1.81 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#region copyright
// Autofac Quartz integration
// https://github.com/alphacloud/Autofac.Extras.Quartz
// Licensed under MIT license.
// Copyright (c) 2014-2022 Alphacloud.Net
#endregion
// ReSharper disable MethodSupportsCancellation
namespace ConsoleScheduler;
using Autofac;
using Serilog;
using SimpleService.Configuration;
using SimpleService.Jobs;
static class Program
{
static async Task Main()
{
Bootstrap.InitializeLogger();
var log = Log.ForContext(typeof(Program));
IContainer? container = null;
Console.WriteLine("This sample demonstrates how to integrate Quartz and Autofac.");
log.Information("Starting...");
try
{
container = Bootstrap.ConfigureContainer(new ContainerBuilder()).Build();
var job = JobBuilder.Create<HeartbeatJob>().WithIdentity("Heartbeat", "Maintenance").Build();
var trigger = TriggerBuilder.Create()
.WithIdentity("Heartbeat", "Maintenance")
.StartNow()
.WithSchedule(SimpleScheduleBuilder.RepeatSecondlyForever(2)).Build();
var cts = new CancellationTokenSource();
var scheduler = container.Resolve<IScheduler>();
await scheduler.ScheduleJob(job, trigger, cts.Token).ConfigureAwait(true);
await scheduler.Start().ConfigureAwait(true);
Console.WriteLine("======================");
Console.WriteLine("Press Enter to exit...");
Console.WriteLine("======================");
Console.ReadLine();
cts.Cancel();
await scheduler.Shutdown().ConfigureAwait(true);
}
catch (Exception ex)
{
log.Fatal(ex, "Unhandled exception caught");
}
container?.Dispose();
Log.CloseAndFlush();
}
}