zhaolei
2020-11-20 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
#if !NET45
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
#else
using Prow.Logging;
#endif
using System;
using System.Threading;
using System.Threading.Tasks;
 
namespace Prow.Tasks
{
    /// <summary>
    /// 后台服务工厂操作类
    /// </summary>
    internal sealed class TaskServicesFactory : IDisposable
#if !NET45
        , IHostedService
#endif
    {
        private readonly CancellationTokenSource _shutdownCts = new CancellationTokenSource();
        private TaskServicesOptions _options;
 
#if !NET45
        internal static TaskServicesFactory? Instance = null;
        /// <summary>
        /// 默认构造函数
        /// </summary>
        /// <param name="logger">ILogger(TaskServicesFactory) 实例</param>
        /// <param name="options">后台服务配置类实例</param>
        /// <param name="storage">IStorage 任务持久化接口实例</param>
        public TaskServicesFactory(ILogger<TaskServicesFactory> logger, IOptionsMonitor<TaskServicesOptions> options, IStorage storage)
        {
            Logger = logger;
            _options = options.CurrentValue;
            options.OnChange(op => _options = op);
            Storage = storage;
            Instance = this;
        }
#else
        /// <summary>
        /// 默认构造函数 
        /// </summary>
        /// <param name="logger">ILogger(TaskServicesFactory) 实例</param>
        /// <param name="options">后台服务配置类实例</param>
        /// <param name="storage">IStorage 任务持久化接口实例</param>
        public TaskServicesFactory(ILogger logger, TaskServicesOptions options, IStorage storage)
        {
            Logger = logger;
            Storage = storage;
            _options = options;
        }
#endif
 
        /// <summary>
        /// 获得 日志实例
        /// </summary>
        public ILogger Logger { get; }
 
        /// <summary>
        /// 获得 任务持久化配置项实例
        /// </summary>
        public IStorage Storage { get; }
 
        /// <summary>
        /// IHostedService 接口异步开始方法
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task StartAsync(CancellationToken cancellationToken = default)
        {
            Log($"{nameof(TaskServicesFactory)} StartAsync() Started");
#if !NET45
            return Task.CompletedTask;
#else
            return Task.FromResult(0);
#endif
        }
 
        /// <summary>
        /// IHostedService 接口异步结束方法
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task StopAsync(CancellationToken cancellationToken)
        {
            Log($"{nameof(TaskServicesFactory)} -> {nameof(StopAsync)}() Shutdown After({_options.ShutdownTimeout})");
            _shutdownCts.CancelAfter(_options.ShutdownTimeout);
            WaitForShutdown(CancellationTokenSource.CreateLinkedTokenSource(_shutdownCts.Token, cancellationToken).Token);
#if !NET45
            return Task.CompletedTask;
#else
            return Task.FromResult(0);
#endif
        }
 
        private void WaitForShutdown(CancellationToken token)
        {
            TaskServicesManager.Shutdown(token);
            Log($"{nameof(TaskServicesFactory)} WaitForShutdown()");
        }
 
        /// <summary>
        /// 日志记录方法
        /// </summary>
        /// <param name="message">日志内容</param>
        public void Log(string message)
        {
            Logger.Log(LogLevel.Information, $"{DateTimeOffset.Now}: {message}");
        }
 
        /// <summary>
        /// Dispose 方法
        /// </summary>
        public void Dispose()
        {
            Log($"{nameof(TaskServicesFactory)} Disposed");
            _shutdownCts?.Dispose();
        }
    }
}