using System; using System.Linq; using System.Threading.Tasks; using HHWMS.Web.Models; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Quartz; using Quartz.Impl; using Quartz.Impl.Triggers; using WalkingTec.Mvvm.Core; using WalkingTec.Mvvm.Core.Support.Quartz; namespace HHWMS.Web.Support { public static class WmsServicesExtension { public static IApplicationBuilder AddQuartzJobs(this IApplicationBuilder app) { var configs = app.ApplicationServices.GetRequiredService>().CurrentValue; var gd = app.ApplicationServices.GetRequiredService(); var dc = configs.CreateDC(); var sp = app.ApplicationServices.GetService(); Task task_scheduler = StdSchedulerFactory.GetDefaultScheduler(); var _scheduler = task_scheduler.Result; var jobList = dc.Set().ToList(); foreach (var job in jobList) { new QuartzService(sp, gd, dc).AddJob(job).Wait(); } var test = gd.AllAssembly.FirstOrDefault(x => x.FullName.Contains("HHWMS.Web")); var t = test.GetExportedTypes().Where(x => typeof(WtmJob).IsAssignableFrom(x) && x.Name != "WtmJob"); foreach (var item in t) { var ci = item.GetConstructor(Type.EmptyTypes); var job = ci?.Invoke(null) as WtmJob; if (job != null) { string jobName = item.Name; string groupName = item.Name; if (_scheduler.CheckExists(new JobKey(jobName, groupName)).Result) { continue; } JobDataMap jobDataMap = new JobDataMap(); jobDataMap.Add("Sp", sp); // 创建作业 IJobDetail j = JobBuilder.Create(item) .WithIdentity(jobName, groupName) .UsingJobData(jobDataMap) .Build(); // 创建触发器 ICronTrigger trigger = (ICronTrigger)TriggerBuilder.Create() .WithIdentity(jobName, groupName) .WithCronSchedule("0 0 0 1 1 ? *") .ForJob(new JobKey(jobName, groupName)) .Build(); ((CronTriggerImpl)trigger).MisfireInstruction = MisfireInstruction.CronTrigger.DoNothing; _scheduler.ScheduleJob(j, trigger).Wait(); //var builder = TriggerBuilder.Create() // .WithIdentity(jobName, groupName) // .WithCronSchedule("0 0 0 1 1 ? *") // .ForJob(new JobKey(jobName, groupName)); //var trigger = builder.Build(); //_scheduler.ScheduleJob(j, trigger).Wait(); //_scheduler.PauseJob(new JobKey(jobName, groupName)).Wait(); } } if (!_scheduler.IsStarted) _scheduler.Start().Wait(); return app; } } }