-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
SchedulerServiceCollectionExtensions.cs
112 lines (96 loc) · 4.6 KB
/
SchedulerServiceCollectionExtensions.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
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
111
112
using System;
using System.Threading.Tasks;
using CronScheduler.Extensions.Internal;
using CronScheduler.Extensions.Scheduler;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
/// An Extension method to register <see cref="SchedulerHostedService"/>.
/// https://github.com/aspnet/Hosting/blob/a3dd609ae667adcb6eb062125d76f9a76a82f7b4/src/Microsoft.Extensions.Hosting.Abstractions/ServiceCollectionHostedServiceExtensions.cs#L17.
/// </summary>
public static class SchedulerServiceCollectionExtensions
{
/// <summary>
/// Adds <see cref="SchedulerHostedService"/> service without global error handler <see cref="UnobservedTaskExceptionEventArgs"/>.
/// Manually register jobs.
/// </summary>
/// <param name="services"></param>
/// <returns></returns>
public static IServiceCollection AddScheduler(this IServiceCollection services)
{
CreateInstance(services);
return services;
}
/// <summary>
/// Adds <see cref="SchedulerHostedService"/> service with global error handler <see cref="UnobservedTaskExceptionEventArgs"/>.
/// Manually register jobs.
/// </summary>
/// <param name="services"></param>
/// <param name="unobservedTaskExceptionHandler"></param>
/// <returns></returns>
public static IServiceCollection AddScheduler(
this IServiceCollection services,
EventHandler<UnobservedTaskExceptionEventArgs> unobservedTaskExceptionHandler)
{
CreateInstance(services, unobservedTaskExceptionHandler);
return services;
}
/// <summary>
/// Adds <see cref="SchedulerHostedService"/> service with ability to register all of the cron job inside the context with
/// global error handler <see cref="UnobservedTaskExceptionEventArgs"/>.
/// </summary>
/// <param name="services"></param>
/// <param name="config"></param>
/// <returns></returns>
public static IServiceCollection AddScheduler(this IServiceCollection services, Action<SchedulerBuilder> config)
{
var builder = new SchedulerBuilder(services);
config(builder);
CreateInstance(builder.Services, builder.UnobservedTaskExceptionHandler);
return builder.Services;
}
/// <summary>
/// Adds <see cref="IScheduledJob"/> job to DI without support for <see cref="UnobservedTaskExceptionEventArgs"/> delegate.
/// </summary>
/// <typeparam name="TJob">The type of the schedule job.</typeparam>
/// <typeparam name="TJobOptions">The options to be using for the job.</typeparam>
/// <param name="services">The DI services.</param>
/// <param name="configure"></param>
/// <param name="sectionName">The section name of the configuration for the job.</param>
/// <param name="jobName">The name for the schedule job.</param>
/// <returns></returns>
public static IServiceCollection AddSchedulerJob<TJob, TJobOptions>(
this IServiceCollection services,
Action<TJobOptions>? configure = default,
string sectionName = "SchedulerJobs",
string? jobName = default)
where TJob : class, IScheduledJob
where TJobOptions : SchedulerOptions, new()
{
var builder = new SchedulerBuilder(services);
// add job with configuration settings
builder.AddJob<TJob, TJobOptions>(configure, sectionName, jobName);
CreateInstance(builder.Services);
return builder.Services;
}
private static void CreateInstance(
IServiceCollection services,
EventHandler<UnobservedTaskExceptionEventArgs>? unobservedTaskExceptionHandler = default)
{
services.TryAddSingleton<ISchedulerRegistration, SchedulerRegistration>();
// should prevent from double registrations.
services.TryAddEnumerable(ServiceDescriptor.Singleton<IHostedService, SchedulerHostedService>(sp =>
{
var registry = sp.GetRequiredService<ISchedulerRegistration>();
var instance = new SchedulerHostedService(registry);
if (unobservedTaskExceptionHandler != null)
{
instance.UnobservedTaskException += unobservedTaskExceptionHandler;
}
return instance;
}));
}
}
}