ningshuxia
2022-10-08 afedb8fd4e17a5a911deee3dae04a10a93e6a39a
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
using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace IStation.Server
{
    /// <summary>
    /// 服务任务
    /// </summary>
    [DisallowConcurrentExecution]//此特性标识 必须等待这次任务执行完成后,才能执行下次任务
    internal class ServiceJob : IJob
    {
        private static List<SingleJobHelper> _jobHelpers = new List<SingleJobHelper>();
        public Task Execute(IJobExecutionContext context)
        {
 
            return Task.Run(async () =>
            {
                try
                {
 
                    //客户标识列表
                    var corpIds = CorpHelper.GetCorpIds();
                    if (corpIds == null || corpIds.Count < 1)
                    {
                        LogHelper.Info($"中间库数据对接任务管理器中,未检索到需要对接的客户标识信息!");
                        CancelJobs();
                        return;
                    }
 
                    //数据对接配置列表
                    var service_configure = new Service.DataDockingConfigure();
                    var configure_list = service_configure.GetByCorpIds(corpIds);
                    if (configure_list == null || configure_list.Count < 1)
                    {
                        LogHelper.Info($"中间库数据对接任务管理器中,未检索到数据对接配置!");
                        CancelJobs();
                        return;
                    }
                    configure_list = configure_list.Where(x => x.DockingMode == Model.DataDockingConfigure.eDockingMode.MiddleLib).ToList();
                    if (configure_list == null || configure_list.Count < 1)
                    {
                        LogHelper.Info($"中间库数据对接任务管理器中,未检索到MiddleLib数据对接配置!");
                        CancelJobs();
                        return;
                    }
                    configure_list = configure_list.Where(x => x.UseStatus == Model.eUseStatus.Enable).ToList();
                    if (configure_list == null || configure_list.Count < 1)
                    {
                        LogHelper.Info($"中间库数据对接任务管理器中,未检索到有效的MiddleLib数据对接配置!");
                        CancelJobs();
                        return;
                    }
 
                    //循环关闭任务
                    foreach (var jobHelper in _jobHelpers.ToList())
                    {
                        var configure = configure_list.Find(t => t.ID == jobHelper.Configure.ID
                                                                                          && t.UpdateTime == jobHelper.Configure.UpdateTime);
                        if (configure == null)
                        {
                            await jobHelper.CancelJob();
                            _jobHelpers.Remove(jobHelper);
                            LogHelper.Info($"配置名称:{jobHelper.Configure.Name},MiddleLib数据对接任务关闭!");
                        }
                    }
 
                    //循环开启任务
                    foreach (var configure in configure_list)
                    {
                        var configure_api = new Model.DataDockingConfigureExMiddleLib(configure);
                        var jobHelper = _jobHelpers.Find(t => t.Configure.ID == configure_api.ID
                                                                                    && t.Configure.UpdateTime == configure_api.UpdateTime);
                        if (jobHelper == null)
                        {
                            jobHelper = new SingleJobHelper();
                            await jobHelper.StartJob(configure_api);
                            _jobHelpers.Add(jobHelper);
                            LogHelper.Info($"配置名称:{configure.Name},MiddleLib数据对接任务开启!");
                        }
                    }
 
 
                    LogHelper.Info($"MiddleLib数据对接任务,开启数量为{_jobHelpers.Count}!");
                }
                catch (Exception ex)
                {
                    LogHelper.Error("MiddleLib数据对接任务管理器中,任务执行出错!", ex);
                    var e = new JobExecutionException(ex);
                    throw e;
                }
            });
 
 
        }
 
        public static void CancelJobs()
        {
            if (_jobHelpers != null && _jobHelpers.Count > 0)
            {
                _jobHelpers.ForEach(async x => await x.CancelJob());
                _jobHelpers.Clear();
            }
        }
 
    }
}