NotificationScheduler.cs 4.5 KB

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