ningshuxia
2022-11-18 b4093bc3cbe7e1c347bd9d366528a74b8a18f570
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
using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace IStation.Server
{
    /// <summary>
    /// Socket数据对接服务任务辅助类
    /// </summary>
    internal class ServiceJobHelper
    {
        private const string _jobName = "SocketServiceJob";
        private const string _jobGroup = "SocketServiceJobGroup";
        private const string _triggerName = "SocketServiceJobTrigger";
        private static IScheduler _sched;//调度器
 
        public async Task StartJob()
        {
            if (_sched != null)
                return;
 
            // 1.创建scheduler的引用
            var fac = new Quartz.Impl.StdSchedulerFactory();
            _sched = await fac.GetScheduler();
 
            //2.启动 scheduler
            await _sched.Start();
 
            //3.创建任务
            var job = JobBuilder.Create<ServiceJob>()
             .WithIdentity(_jobName, _jobGroup)
             .Build();
 
            //4.创建Trigger
            var trigger = TriggerBuilder.Create()
            .WithIdentity(_triggerName, _jobGroup)
            .WithSimpleSchedule(x => x.WithIntervalInMinutes(ConfigHelper.ResetFrequency)
            .RepeatForever()
            .WithMisfireHandlingInstructionNextWithRemainingCount())
            .Build();
 
            //5.加入调度管理器
            await _sched.ScheduleJob(job, trigger);
        }
 
        /// <summary>
        /// 取消任务
        /// </summary>
        public async Task CancelJob()
        {
            if (_sched == null)
                return;
            var triggerKey = new TriggerKey(_triggerName, _jobGroup);
            if (await _sched.CheckExists(triggerKey))
            {
                await _sched.UnscheduleJob(triggerKey);
            }
            ServiceJob.CancelServers();
        }
 
    }
}