zhaolei
7 days ago 921de2254ff5712a44ed8575ee8efe34252f6603
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#if NET45
using Prow.Logging;
#endif
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
 
namespace Prow.Tasks
{
    /// <summary>
    /// 后台服务管理操作类
    /// </summary>
    public static class TaskServicesManager
    {
#if !NET45
        private static TaskServicesFactory? Factory { get { return TaskServicesFactory.Instance; } }
#else
        private static ILogger? _logger;
        private static TaskServicesOptions? _options;
        private static TaskServicesFactory? Factory;
 
        /// <summary>
        /// 后台服务初始化
        /// </summary>
        /// <param name="logger">后台任务日志实例</param>
        /// <param name="options">后台任务服务配置实例</param>
        /// <param name="storage">IStorage 任务持久化接口实例</param>
        public static void Init(ILogger? logger = null, TaskServicesOptions? options = null, IStorage? storage = null)
        {
            _logger = logger ?? new FileLoggerProvider(new FileLoggerOptions()).CreateLogger(nameof(TaskServicesFactory));
            _options = options ?? new TaskServicesOptions();
 
            Factory = new TaskServicesFactory(_logger, _options, storage ?? new NoneStorage());
            var _ = Factory.StartAsync();
        }
#endif
 
        internal static readonly ConcurrentDictionary<string, Lazy<SchedulerProcess>> _schedulerPool = new ConcurrentDictionary<string, Lazy<SchedulerProcess>>();
 
        /// <summary>
        /// 将任务与触发器添加到调度中 多线程安全
        /// </summary>
        /// <typeparam name="T">任务</typeparam>
        /// <param name="trigger">ITrigger 实例 为空时内部使用 TriggerBuilder.Default</param>
        /// <returns>返回 IScheduler 实例</returns>
        public static IScheduler GetOrAdd<T>(ITrigger? trigger = null) where T : ITask, new() => GetOrAdd<T>(typeof(T).Name, trigger);
 
        /// <summary>
        /// 将任务与触发器添加到调度中 多线程安全
        /// </summary>
        /// <typeparam name="T">任务</typeparam>
        /// <param name="schedulerName">Scheduler 名称</param>
        /// <param name="trigger">ITrigger 实例 为空时内部使用 TriggerBuilder.Default</param>
        /// <returns>返回 IScheduler 实例</returns>
        public static IScheduler GetOrAdd<T>(string schedulerName, ITrigger? trigger = null) where T : ITask, new()
        {
            if (string.IsNullOrEmpty(schedulerName)) schedulerName = typeof(T).Name;
            return _schedulerPool.GetOrAdd(schedulerName, key => new Lazy<SchedulerProcess>(() =>
            {
                var process = GetSchedulerProcess(key);
 
                // 绑定任务与触发器
                process.Start<T>(trigger ?? TriggerBuilder.Default.Build());
                return process;
            })).Value.Scheduler;
        }
 
        /// <summary>
        /// 将任务与触发器添加到调度中 多线程安全
        /// </summary>
        /// <param name="schedulerName">Scheduler 名称</param>
        /// <returns>返回 IScheduler 实例</returns>
        public static IScheduler? Get(string schedulerName)
        {
            if (string.IsNullOrEmpty(schedulerName)) throw new ArgumentNullException(nameof(schedulerName));
            _schedulerPool.TryGetValue(schedulerName, out var process);
            return process?.Value.Scheduler;
        }
 
        /// <summary>
        /// 将任务与触发器添加到调度中 多线程安全
        /// </summary>
        /// <param name="schedulerName">Scheduler 名称</param>
        /// <param name="methodCall">创建任务委托 string 为 Scheduler 名称</param>
        /// <param name="trigger">ITrigger 实例 为空时内部使用 TriggerBuilder.Default</param>
        /// <returns>返回 IScheduler 实例</returns>
        public static IScheduler GetOrAdd(string schedulerName, Func<CancellationToken, Task> methodCall, ITrigger? trigger = null)
        {
            if (string.IsNullOrEmpty(schedulerName)) throw new ArgumentNullException(nameof(schedulerName));
            if (methodCall == null) throw new ArgumentNullException(nameof(methodCall));
 
            return GetOrAdd(schedulerName, new DefaultTask(methodCall), trigger);
        }
 
        /// <summary>
        /// 将任务与触发器添加到调度中 多线程安全
        /// </summary>
        /// <param name="schedulerName">Scheduler 名称</param>
        /// <param name="task">创建任务委托 string 为 Scheduler 名称</param>
        /// <param name="trigger">ITrigger 实例 为空时内部使用 TriggerBuilder.Default</param>
        /// <returns>返回 IScheduler 实例</returns>
        public static IScheduler GetOrAdd(string schedulerName, ITask task, ITrigger? trigger = null)
        {
            if (string.IsNullOrEmpty(schedulerName)) throw new ArgumentNullException(nameof(schedulerName));
            if (task == null) throw new ArgumentNullException(nameof(task));
 
            return _schedulerPool.GetOrAdd(schedulerName, key => new Lazy<SchedulerProcess>(() =>
            {
                var process = GetSchedulerProcess(key);
 
                // 绑定任务与触发器
                process.Start(task, trigger ?? TriggerBuilder.Default.Build());
                return process;
            })).Value.Scheduler;
        }
 
        private static SchedulerProcess GetSchedulerProcess(string key)
        {
            // 创建调度
#if !NET45
            if (Factory == null) throw new InvalidOperationException("Please Call the AddTaskServices method in the Startup ConfigureServices first.");
#else
            if (Factory == null) throw new InvalidOperationException("Please Call the Init method first.");
#endif
            var sche = new DefaultScheduler(key);
            var process = new SchedulerProcess(sche, Factory.Log, Factory.Storage);
            process.LoggerAction($"{nameof(TaskServicesManager)} {nameof(DefaultScheduler)}({key}) Created");
 
            // 关联调度与调度执行器
            sche.SchedulerProcess = process;
            return process;
        }
 
        /// <summary>
        /// 移除指定名称的任务
        /// </summary>
        /// <param name="schedulerName">任务名称</param>
        public static bool Remove(string schedulerName)
        {
            Factory?.Storage.Remove(new string[] { schedulerName });
            var ret = _schedulerPool.TryRemove(schedulerName, out var sche);
            if (ret && sche != null)
            {
                sche.Value.LoggerAction("Remove()");
                sche.Value.Stop();
            }
            return ret;
        }
 
        /// <summary>
        /// 清除所有调度
        /// </summary>
        public static void Clear()
        {
            Shutdown();
            var sches = _schedulerPool.Keys;
            Factory?.Storage.Remove(sches);
            _schedulerPool.Clear();
        }
 
        /// <summary>
        /// 将内部所有调度转化为集合
        /// </summary>
        /// <returns>IScheduler 集合实例</returns>
        public static IEnumerable<IScheduler> ToList() => _schedulerPool.Values.Select(v => v.Value.Scheduler);
 
        /// <summary>
        /// 停止所有后台调度
        /// </summary>
        internal static void Shutdown(CancellationToken token = default)
        {
            foreach (var sche in _schedulerPool.Values)
            {
                sche.Value.Stop();
                if (token.IsCancellationRequested) break;
            }
        }
    }
}