lixiaojun
2023-12-12 c19747ed95dc9899c6148858bbb9184d02d825a4
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
namespace IStation.Server
{
    /// <summary>
    /// 设备运行分析单任务辅助类
    /// </summary>
    public class EquipmentRunAnalySingleJobHelper
    {
        private IScheduler _sched;//调度器
 
        /// <summary>
        /// 配置
        /// </summary>
        public Yw.Model.RunAnalyConfigure Configure { get; set; }
 
        /// <summary>
        /// 开始任务
        /// </summary>
        public async Task StartJob(Yw.Model.RunAnalyConfigure configure)
        {
            if (_sched != null)
            {
                return;
            }
            this.Configure = configure;
 
            var jobName = EquipmentRunAnalySingleJobNameHelper.GetJobName(configure);
            var jobGroupName = EquipmentRunAnalySingleJobNameHelper.GetJobGroupName(configure);
            var triggerName = EquipmentRunAnalySingleJobNameHelper.GetTriggerName(configure);
 
            // 1.创建scheduler的引用
            var fac = new Quartz.Impl.StdSchedulerFactory();
            _sched = await fac.GetScheduler();
 
            //2.启动 scheduler
            await _sched.Start();
 
            //3.创建任务
            var job = JobBuilder.Create<EquipmentRunAnalySingleJob>()
             .WithIdentity(jobName, jobGroupName)
             .Build();
            job.JobDataMap.Put(EquipmentRunAnalySingleJob.Instance, configure);
 
            //4.创建Trigger
            var trigger = TriggerBuilder.Create()
               .WithIdentity(triggerName, jobGroupName)
               .WithSimpleSchedule(x => x.WithIntervalInSeconds(configure.Frequency)
               .RepeatForever().WithMisfireHandlingInstructionNextWithRemainingCount())
               .Build();
 
            //5.加入调度管理器
            await _sched.ScheduleJob(job, trigger);
        }
 
        /// <summary>
        /// 取消任务
        /// </summary>
        public async Task CancelJob()
        {
            if (_sched == null)
                return;
            var jobGroupName = EquipmentRunAnalySingleJobNameHelper.GetJobGroupName(this.Configure);
            var triggerName = EquipmentRunAnalySingleJobNameHelper.GetTriggerName(this.Configure);
            var triggerKey = new TriggerKey(triggerName, jobGroupName);
            if (await _sched.CheckExists(triggerKey))
            {
                await _sched.UnscheduleJob(triggerKey);
            }
        }
 
 
 
    }
}