NotificationScheduler.cs 4.3 KB

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