1
wankeda
2025-03-22 867aba2636e34a1050b1c4c84bbe78cc9c39b553
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
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Core.Helper;
 
namespace WIDESEA_Core.QuartzJob
{
    public class QuartzJobHostedService : IHostedService
    {
        private readonly ISchedulerCenter _schedulerCenter;
        private readonly ILogger<QuartzJobHostedService> _logger;
 
        public QuartzJobHostedService(ILogger<QuartzJobHostedService> logger, ISchedulerCenter schedulerCenter)
        {
            _logger = logger;
            _schedulerCenter = schedulerCenter;
        }
        private static string seedDataTasksQz = "{0}.json";
        /// <summary>
        /// 启动程序自动开启调度服务
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task StartAsync(CancellationToken cancellationToken)
        {
            string path = string.Format(Path.Combine(App.WebHostEnvironment.WebRootPath, seedDataTasksQz), "DispatchInfo") ;
            string fileData = FileHelper.ReadFile(path, Encoding.UTF8);
            List<DispatchInfoDTO>? data = JsonConvert.DeserializeObject<List<DispatchInfoDTO>>(fileData);
            if (data != null) 
            {
                for (int i = 0; i < data.Count; i++)
                {
                    var result = _schedulerCenter.AddScheduleJobAsync(data[i]).Result;
                }
            }
            return Task.CompletedTask;
        }
 
        public Task StopAsync(CancellationToken cancellationToken)
        {
            _logger.LogInformation("Stop QuartzJob Service!");
            return Task.CompletedTask;
        }
    }
}