tangxu
2024-01-09 ddc2780231ea76be74fadb7486401a3d0d17b101
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
namespace Yw.Server
{
    /// <summary>
    /// 设备综合评价服务任务
    /// </summary>
    [DisallowConcurrentExecution]
    public class EquipmentEvaluateServiceJob : IJob
    {
        private static List<EquipmentEvaluateJobHelper> _jobHelpers = new();
 
        /// <summary>
        /// 执行
        /// </summary>
        public Task Execute(IJobExecutionContext context)
        {
 
            return Task.Run(async () =>
            {
                try
                {
                    #region 获取所有绑定
 
                    var bindingList = new Service.HealthMultiEvaluationModelBinding().GetAllValidList();
                    if (bindingList == null || bindingList.Count < 1)
                    {
                        CancelJobs();
                        return;
                    }
 
                    #endregion
 
                    #region 关闭任务
 
                    foreach (var jobHelper in _jobHelpers.ToList())
                    {
                        if (!bindingList.Exists(x => x.ID == jobHelper.Binding.ID && x.CronType == jobHelper.Binding.CronType && x.CronParas == jobHelper.Binding.CronParas))
                        {
                            await jobHelper.CancelJob();
                            _jobHelpers.Remove(jobHelper);
                            LogHelper.Info($"设备评价服务任务中,设备id:{jobHelper.Binding.EquipmentID},模型id:{jobHelper.Binding.ModelID}, 的任务关闭!");
                        }
                    }
 
                    #endregion
 
                    #region 开启任务
 
                    foreach (var binding in bindingList)
                    {
                        var jobHelper = _jobHelpers.Find(t => t.Binding.ID == binding.ID && t.Binding.CronType == binding.CronType && t.Binding.CronParas == binding.CronParas);
                        if (jobHelper == null)
                        {
                            jobHelper = new EquipmentEvaluateJobHelper();
                            await jobHelper.StartJob(binding);
                            _jobHelpers.Add(jobHelper);
                            LogHelper.Info($"设备评价服务任务中,设备id:{binding.EquipmentID},模型id:{binding.ModelID}, 的任务开启!");
                        }
                    }
 
                    #endregion
 
                    LogHelper.Info($"设备评价服务任务中,开启的任务数量:{_jobHelpers.Count} !");
                }
                catch (Exception ex)
                {
                    LogHelper.Error("设备评价服务任务中,执行出错!", ex);
                    var e = new JobExecutionException(ex);
                    throw e;
                }
            });
 
 
        }
 
        /// <summary>
        /// 取消任务
        /// </summary>
        public static void CancelJobs()
        {
            if (_jobHelpers != null && _jobHelpers.Count > 0)
            {
                _jobHelpers.ForEach(async x => await x.CancelJob());
                _jobHelpers.Clear();
            }
        }
 
 
    }
 
}