NotificationScheduler.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using GreenTree.Nachtragsmanagement.Core.Domain.Misc;
  2. using GreenTree.Nachtragsmanagement.Services.Misc;
  3. using Quartz;
  4. using Quartz.Impl;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Web;
  9. namespace GreenTree.Nachtragsmanagement.Services.Scheduling
  10. {
  11. public class NotificationScheduler : INotificationScheduler
  12. {
  13. #region Fields
  14. private readonly INotificationService _notificationService;
  15. private readonly IMiscService _miscService;
  16. #endregion
  17. #region Ctor
  18. /// <summary>
  19. /// Initializes a new instance of the NotificationScheduler class
  20. /// </summary>
  21. public NotificationScheduler(
  22. INotificationService notificationService,
  23. IMiscService miscService)
  24. {
  25. _notificationService = notificationService;
  26. _miscService = miscService;
  27. }
  28. #endregion
  29. /// <summary>
  30. /// Starts the scheduler and builds all corresponding notification jobs
  31. /// </summary>
  32. public void Start()
  33. {
  34. var scheduler = StdSchedulerFactory.GetDefaultScheduler();
  35. scheduler.Clear();
  36. scheduler.Start();
  37. var mailNotifications = _miscService.GetAllMailNotifications();
  38. foreach (var mailNotification in mailNotifications)
  39. {
  40. var notificationPlugin = _notificationService.GetNotificationPlugin(mailNotification.NotificationPluginSystemName);
  41. if (notificationPlugin == null)
  42. continue;
  43. try
  44. {
  45. var job = JobBuilder.Create(notificationPlugin.GetType())
  46. .WithIdentity(mailNotification.Id.ToString())
  47. .SetJobData(new JobDataMap
  48. {
  49. { "MailNotifications", new [] { mailNotification } }
  50. })
  51. .Build();
  52. var trigger = TriggerBuilder.Create()
  53. .WithCronSchedule(mailNotification.CronExpression)
  54. .Build();
  55. scheduler.ScheduleJob(job, trigger);
  56. }
  57. catch (Exception ex)
  58. {
  59. continue;
  60. }
  61. }
  62. }
  63. /// <summary>
  64. /// Determines the next execution time of a specific job
  65. /// </summary>
  66. /// <param name="jobId">The job id.</param>
  67. public DateTime GetNextExecutionOfJob(string jobId)
  68. {
  69. var scheduler = StdSchedulerFactory.GetDefaultScheduler();
  70. var jobKey = new JobKey(jobId);
  71. var nextFireTime = DateTime.MinValue;
  72. var isJobExisting = scheduler.CheckExists(jobKey);
  73. if (isJobExisting)
  74. {
  75. var detail = scheduler.GetJobDetail(jobKey);
  76. var triggers = scheduler.GetTriggersOfJob(jobKey);
  77. if (triggers.Count > 0)
  78. {
  79. var nextFireTimeUtc = triggers[0].GetNextFireTimeUtc();
  80. nextFireTime = TimeZone.CurrentTimeZone.ToLocalTime(nextFireTimeUtc.Value.DateTime);
  81. }
  82. }
  83. return (nextFireTime);
  84. }
  85. /// <summary>
  86. /// Determines the next execution time of a specific mail notification
  87. /// </summary>
  88. /// <param name="mailNotification">The mail notification job.</param>
  89. public DateTime GetNextExecutionOfJob(MailNotification mailNotification)
  90. {
  91. if (mailNotification == null)
  92. return DateTime.MinValue;
  93. return GetNextExecutionOfJob(mailNotification.Id.ToString());
  94. }
  95. }
  96. }