QuartzJobMildd.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using log4net;
  2. using Microsoft.AspNetCore.Builder;
  3. using Ropin.Inspection.Common.Helper;
  4. using Ropin.Inspection.Service.Interface;
  5. using Ropin.Inspection.Tasks.QuartzNet;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace Ropin.Core.Extensions.Middlewares
  12. {
  13. /// <summary>
  14. /// Quartz 启动服务
  15. /// </summary>
  16. public static class QuartzJobMildd
  17. {
  18. private static readonly ILog log = LogManager.GetLogger(typeof(QuartzJobMildd));
  19. public static void UseQuartzJobMildd(this IApplicationBuilder app, ITasksQzServices tasksQzServices, ISchedulerCenter schedulerCenter)
  20. {
  21. if (app == null) throw new ArgumentNullException(nameof(app));
  22. try
  23. {
  24. if (Appsettings.app("Middleware", "QuartzNetJob", "Enabled").ObjToBool())
  25. {
  26. var allQzServices = tasksQzServices.GetAllAsync().Result;//.Query().Result;
  27. if (allQzServices == null || !allQzServices.Any())
  28. {
  29. return;
  30. }
  31. foreach (var item in allQzServices)
  32. {
  33. if (item.IsStart)
  34. {
  35. var ResuleModel = schedulerCenter.AddScheduleJobAsync(item).Result;
  36. if (ResuleModel.success)
  37. {
  38. Console.WriteLine($"QuartzNetJob{item.Name}启动成功!");
  39. }
  40. else
  41. {
  42. Console.WriteLine($"QuartzNetJob{item.Name}启动失败!错误信息:{ResuleModel.msg}");
  43. }
  44. }
  45. }
  46. }
  47. }
  48. catch (Exception e)
  49. {
  50. log.Error($"An error was reported when starting the job service.\n{e.Message}");
  51. throw;
  52. }
  53. }
  54. }
  55. }