wankeda
2025-04-07 4da42c12efde190b208f18b7dd51d3e89986837d
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.Extensions.DependencyInjection;
using Quartz.Spi;
using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
 
namespace WIDESEA_Core.QuartzJob
{
    /// <summary>
    /// 任务调度 启动服务
    /// </summary>
    public static class JobSetup
    {
        public static void AddJobSetup(this IServiceCollection services, Action<IServiceCollection> configureJobs = null)
        {
            if (services == null) throw new ArgumentNullException(nameof(services));
 
            // 将JobFactory注入到服务中
            services.AddSingleton<IJobFactory, JobFactory>();
            // 将SchedulerCenterServer注入到服务中
            services.AddSingleton<ISchedulerCenter, SchedulerCenterServer>();
 
            // 任务注入
            var baseType = typeof(IJob);
            // 获取当前应用程序域的相对搜索路径
            var path = AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory;
            // 获取指定路径下的所有程序集
            var referencedAssemblies = System.IO.Directory.GetFiles(path, "WIDESEA_Tasks.dll").Select(Assembly.LoadFrom).ToArray();
            // 获取所有定义的类型
            var types = referencedAssemblies
                .SelectMany(a => a.DefinedTypes)
                .Select(type => type.AsType())
                // 筛选出继承自IJob的类型
                .Where(x => x != baseType && baseType.IsAssignableFrom(x)).ToArray();
            // 获取所有实现了IJob的类型
            var implementTypes = types.Where(x => x.IsClass).ToArray();
            // 将所有实现了IJob的类型注入到服务中
            foreach (var implementType in implementTypes)
            {
                services.AddTransient(implementType);
            }
 
            // 添加定时启动配置
            services.AddTransient<StartupJob>();
            services.AddSingleton(new JobSchedule(
                jobType: typeof(StartupJob),
                cronExpression: "0 0 8 ? * *")); // 每天上午8点执行
        }
    }
 
    /// <summary>
    /// 启动任务
    /// </summary>
    public class StartupJob : IJob
    {
        private readonly ISchedulerCenter _schedulerCenter;
 
        public StartupJob(ISchedulerCenter schedulerCenter)
        {
            _schedulerCenter = schedulerCenter;
        }
 
        public async Task Execute(IJobExecutionContext context)
        {
            // 在这里启动所有需要定时执行的任务
            await _schedulerCenter.StartScheduleAsync();
        }
    }
 
    /// <summary>
    /// 任务调度配置
    /// </summary>
    public class JobSchedule
    {
        public Type JobType { get; }
        public string CronExpression { get; }
 
        public JobSchedule(Type jobType, string cronExpression)
        {
            JobType = jobType;
            CronExpression = cronExpression;
        }
    }
}