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 _logger; public QuartzJobHostedService(ILogger logger, ISchedulerCenter schedulerCenter) { _logger = logger; _schedulerCenter = schedulerCenter; } private static string seedDataTasksQz = "{0}.json"; /// /// 启动程序自动开启调度服务 /// /// /// public Task StartAsync(CancellationToken cancellationToken) { string path = string.Format(Path.Combine(App.WebHostEnvironment.WebRootPath, seedDataTasksQz), "DispatchInfo") ; string fileData = FileHelper.ReadFile(path, Encoding.UTF8); List? data = JsonConvert.DeserializeObject>(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; } } }