Blame view

Quartz.AspNetCore/ServiceCollectionExtensions.cs 3.48 KB
霍尔 authored
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
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Quartz.AspNetCore.Logging;
using Quartz.Impl;
using Quartz.Logging;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;

namespace Quartz.AspNetCore
{
	public static class ServiceCollectionExtensions
	{
		public static IServiceCollection AddQuartz(this IServiceCollection services,
			Action<QuartzOtpionsBuilder> optionsAction = null)
		{
			var builder = new QuartzOtpionsBuilder { Services = services };
			builder.Properties = new NameValueCollection
			{
				{ "quartz.jobStore.useProperties", "true" }
			};
			optionsAction?.Invoke(builder);

			var serverSched = new StdSchedulerFactory(builder.Properties).GetScheduler().Result;
			builder.Services.AddSingleton(serverSched);
			builder.Services.AddTransient<ISchedulerFactory, StdSchedulerFactory>();
			//builder.Services.AddTransient<ISchedulerFactory, StdSchedulerFactory>();
			builder.Services.AddTransient<LoggingProvider>();
			return services;
		}

		public static QuartzOtpionsBuilder UseSchedulerName(this QuartzOtpionsBuilder builder, string name)
		{
			builder.Properties.Set("schedName", name);
			return builder;
		}

		public static QuartzOtpionsBuilder UseMemoryStore(this QuartzOtpionsBuilder builder)
		{
			builder.Properties.Set("quartz.jobStore.type", "Quartz.Simpl.RAMJobStore, Quartz");
			builder.Properties.Remove("quartz.jobStore.useProperties");
			builder.Properties.Remove("quartz.jobStore.driverDelegateType");
			builder.Properties.Remove("quartz.jobStore.dataSource");
			builder.Properties.Remove("quartz.jobStore.tablePrefix");
			builder.Properties.Remove("quartz.dataSource.myDs.provider");
			builder.Properties.Remove("quartz.dataSource.myDs.connectionString");
			return builder;
		}

		public static QuartzOtpionsBuilder UseSqlServer(this QuartzOtpionsBuilder builder, string connectString, string serializerType = "binary", string tablePrefix = "QRTZ_")
		{
			builder.Properties.Set("quartz.jobStore.type", "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz");
			builder.Properties.Set("quartz.jobStore.driverDelegateType", "Quartz.Impl.AdoJobStore.StdAdoDelegate, Quartz");
			builder.Properties.Set("quartz.jobStore.dataSource", "myDs");
			builder.Properties.Set("quartz.dataSource.myDs.provider", "SqlServer");
			builder.Properties.Set("quartz.jobStore.tablePrefix", tablePrefix);
			builder.Properties.Set("quartz.serializer.type", serializerType);
			builder.Properties.Set("quartz.dataSource.myDs.connectionString", connectString);
			return builder;
		}

        public static QuartzOtpionsBuilder UseProperties(this QuartzOtpionsBuilder builder, bool useProperties)
        {
            if(useProperties)
                builder.Properties.Set("quartz.jobStore.useProperties", "true");
            else
                builder.Properties.Set("quartz.jobStore.useProperties", "false");
            return builder;
        }

        public static IServiceProvider UseQuartz(this IServiceProvider provider)
		{
			LogProvider.SetCurrentLogProvider(provider.GetRequiredService<LoggingProvider>());
			var sched = provider.GetRequiredService<IScheduler>();
			sched.Start();
			return provider;
		}

		public static IApplicationBuilder UseQuartz(this IApplicationBuilder builder)
		{
			LogProvider.SetCurrentLogProvider(builder.ApplicationServices.GetRequiredService<LoggingProvider>());
			var sched = builder.ApplicationServices.GetRequiredService<IScheduler>();
			sched.Start();
			return builder;
		}
	}
}