Scheduler.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using Bowin.Common.Utility;
  2. using Microsoft.AspNetCore.Builder;
  3. using Microsoft.Extensions.Configuration;
  4. using Microsoft.Extensions.DependencyInjection;
  5. using Quartz;
  6. using Quartz.Impl;
  7. using Quartz.Spi;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Reflection;
  13. using System.Text;
  14. namespace Bowin.Common.Scheduler
  15. {
  16. public static class Scheduler
  17. {
  18. private static List<Type> jobList { get; set; }
  19. private static Dictionary<IJobDetail, IReadOnlyCollection<ITrigger>> schedulerJobDic { get; set; }
  20. //private static IServiceCollection ServiceCollection;
  21. #region 配置
  22. public static void AddQuartz(this IServiceCollection services, Assembly assembly = null)
  23. {
  24. services.AddSingleton<IJobFactory, QuartzFactory>();
  25. //ServiceCollection = services;
  26. ScheduleAllJob(assembly);
  27. jobList.ForEach(jobType => services.Add(new ServiceDescriptor(jobType, jobType, ServiceLifetime.Scoped)));
  28. services.AddSingleton(provider =>
  29. {
  30. var schedulerFactory = new StdSchedulerFactory();
  31. var scheduler = schedulerFactory.GetScheduler().Result;
  32. scheduler.JobFactory = provider.GetService<IJobFactory>();
  33. scheduler.Start();
  34. return scheduler;
  35. });
  36. //Scheduler.StartJobs();
  37. }
  38. #endregion
  39. public static void ScheduleAllJob(Assembly assembly = null)
  40. {
  41. if (assembly == null)
  42. {
  43. assembly = Assembly.GetEntryAssembly();
  44. }
  45. jobList = assembly.GetTypes().Where(x => x.GetInterfaces().Any(i => i.Name == "IJob"))
  46. .ToList();
  47. schedulerJobDic = new Dictionary<IJobDetail, IReadOnlyCollection<ITrigger>>();
  48. foreach (var job in jobList)
  49. {
  50. var configuration = new ConfigurationBuilder()
  51. .SetBasePath(Directory.GetCurrentDirectory())
  52. .AddJsonFile("appsettings.json")
  53. .Build();
  54. string intervalStr = configuration.GetSection("Scheduler").GetSection("Jobs").GetValue<string>(job.FullName + ".Interval");
  55. IJobDetail jobIns = JobBuilder.Create(job).WithIdentity(job.FullName, job.Namespace).Build();
  56. ITrigger triggerIns;
  57. int interval;
  58. if (!int.TryParse(intervalStr, out interval))
  59. {
  60. string cronStr = configuration.GetSection("Scheduler").GetSection("Jobs").GetValue<string>(job.FullName + ".cron");
  61. if (string.IsNullOrEmpty(cronStr))
  62. {
  63. continue;
  64. }
  65. else
  66. {
  67. triggerIns = TriggerBuilder.Create()
  68. .WithIdentity(job.FullName + "_Trigger", job.Namespace)
  69. .StartNow()
  70. .WithCronSchedule(cronStr).Build();
  71. }
  72. }
  73. else
  74. {
  75. triggerIns = TriggerBuilder.Create()
  76. .WithIdentity(job.FullName + "_Trigger", job.Namespace)
  77. .StartNow()
  78. .WithSimpleSchedule(x => x.WithIntervalInSeconds(interval).RepeatForever()).Build();
  79. }
  80. var triggerSet = new HashSet<ITrigger>();
  81. triggerSet.Add(triggerIns);
  82. schedulerJobDic.Add(jobIns, triggerSet);
  83. }
  84. }
  85. public static void StartSchedulerJobs(this IApplicationBuilder app)
  86. {
  87. var scheduler = new StdSchedulerFactory().GetScheduler().Result;
  88. scheduler.JobFactory = HttpHelper.GetService<IJobFactory>();
  89. //scheduler.JobFactory = ServiceCollection.BuildServiceProvider().GetService<IJobFactory>();
  90. scheduler.ScheduleJobs(schedulerJobDic, true);
  91. scheduler.Start();
  92. }
  93. }
  94. public class QuartzFactory : IJobFactory
  95. {
  96. private readonly IServiceProvider _serviceProvider;
  97. public QuartzFactory(IServiceProvider serviceProvider)
  98. {
  99. _serviceProvider = serviceProvider;
  100. }
  101. public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
  102. {
  103. var jobDetail = bundle.JobDetail;
  104. var job = (IJob)_serviceProvider.GetService(jobDetail.JobType);
  105. return job;
  106. }
  107. public void ReturnJob(IJob job)
  108. {
  109. var disposable = job as IDisposable;
  110. disposable?.Dispose();
  111. }
  112. }
  113. }