Admin
2025-12-02 9e42f0dafa019f5ecf6b0ff425ecb966b002171e
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Specialized;
using System.Reflection;
using System.Threading.Tasks;
using WIDESEA.Core.Utilities;
using WIDESEA.Services.IServices;
 
namespace WIDESEA.Job
{
    public class SchedulerCenterServer
    {
        private Task<IScheduler> _scheduler;
        private StdSchedulerFactory _jobFactory;
 
 
        public SchedulerCenterServer(StdSchedulerFactory jobFactory)
        {
            _jobFactory = jobFactory;
            _scheduler = GetSchedulerAsync();
        }
        private Task<IScheduler> GetSchedulerAsync()
        {
            if (_scheduler != null)
                return this._scheduler;
            else
            {
                // 从Factory中获取Scheduler实例
                NameValueCollection collection = new NameValueCollection
                {
                    { "quartz.serializer.type", "binary" },
                };
                StdSchedulerFactory factory = new StdSchedulerFactory(collection);
                return _scheduler = factory.GetScheduler();
            }
        }
        #region 将任务计划添加到调度中心
        /// <summary>
        /// 添加一个任务
        /// </summary>
        /// <param name="job"></param>
        /// <returns></returns>
        public async Task<WebResponseContent> AddScheduleJobAsync(string jobClassName)
        {
            WebResponseContent content = new WebResponseContent();
            string jobName = jobClassName;
            string jobGroup = "jobGroup";
            string jobAssembName = "WIDESEA.Job";
            string className = jobClassName;
            try
            {
                JobKey jobKey = new JobKey(jobName, jobGroup);
                if (!await this._scheduler.Result.CheckExists(jobKey))
                {
                    Assembly assembly = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + $"{jobAssembName}.dll");
 
                    Type jobType = assembly.GetType(jobAssembName + "." + className);
 
                    //判断任务调度是否已经开启
                    if (!this._scheduler.Result.IsStarted)
                    {
                        await StartScheduleAsync();
                    }
 
                    //传入反射出来的执行程序集
                    IJobDetail jobDetail = new JobDetailImpl(jobName, jobGroup, jobType);
                    ITrigger trigger = CreateSimpleTrigger(jobName, jobGroup);
 
 
                    // 告诉Quartz使用我们的触发器来安排作业
                    await _scheduler.Result.ScheduleJob(jobDetail, trigger);
 
                }
                await Console.Out.WriteLineAsync($"【{jobName}】任务计划添加成功");
                return content = content.OK(message: $"【{jobName}】添加成功");
 
            }
            catch (Exception ex)
            {
                Console.Out.WriteLine($"任务计划异常:【{ex.Message}】");
                //this._scheduler.Dispose();
                this._scheduler = null;
                return content = content.Error(message: $"任务计划异常:【{ex.Message}】");
            }
        }
        #endregion
        #region 检测任务是否存在
        /// <summary>
        /// 检测任务是否存在
        /// </summary>
        /// <param name="job"></param>
        /// <returns></returns>
        public async Task<bool> IsExistScheduleJobAsync(string jobName, string jobGroup)
        {
            JobKey jobKey = new JobKey(jobName, jobGroup);
            if (await _scheduler.Result.CheckExists(jobKey))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion
 
        #region 开启任务调度
        /// <summary>
        /// 开启任务调度
        /// </summary>
        /// <returns></returns>
        public async Task<WebResponseContent> StartScheduleAsync()
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                if (!this._scheduler.Result.IsStarted)
                {
                    await this._scheduler.Result.Start();
                    await Console.Out.WriteLineAsync("任务调度开启!");
                    content = content.OK(message: $"任务调度开启成功");
 
                    //WIDESEA.Helper.GetToken();
 
                    //调度开启成功后,开启Hsl的websocket,端口号为9111
                    if (!ToWCSService.webServer.IsStarted)
                    {
                        ToWCSService.webServer.ServerStart(9111); ;
                    }
 
                }
                else
                {
                    await Console.Out.WriteLineAsync($"任务调度已经开启");
                    content = content.Error($"任务调度已经开启");
                }
            }
            catch (Exception ex)
            {
                await Console.Out.WriteLineAsync($"任务调度开启异常:{ex.Message}");
                content = content.Error($"任务调度开启异常:{ex.Message}");
            }
            return content;
        }
        #endregion
 
        private ITrigger CreateSimpleTrigger(string jobName, string jobGroup)
        {
            ITrigger trigger = TriggerBuilder.Create()
            .WithIdentity(jobName, jobGroup)
            .WithSimpleSchedule(x => x
                .WithIntervalInSeconds(10)
                .RepeatForever()
            )
            .Build();
            return trigger;
        }
    }
}