using Autofac;
using GreenTree.Nachtragsmanagement.Core;
using GreenTree.Nachtragsmanagement.Core.Domain.Misc;
using GreenTree.Nachtragsmanagement.Services.Logging;
using GreenTree.Nachtragsmanagement.Services.Misc;
using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace GreenTree.Nachtragsmanagement.Services.Scheduling
{
public class NotificationScheduler : INotificationScheduler
{
#region Fields
private readonly INotificationService _notificationService;
private readonly IMiscService _miscService;
private readonly ILogger _logger;
private readonly IScheduler _scheduler;
#endregion
#region Ctor
///
/// Initializes a new instance of the NotificationScheduler class
///
public NotificationScheduler(
INotificationService notificationService,
IMiscService miscService,
ILogger logger,
IScheduler scheduler)
{
_notificationService = notificationService;
_miscService = miscService;
_logger = logger;
_scheduler = scheduler;
}
#endregion
///
/// Starts the scheduler and builds all corresponding notification jobs
///
public void Start()
{
//var scheduler = StdSchedulerFactory.GetDefaultScheduler();
//scheduler.Clear();
//scheduler.Start();
_scheduler.Clear();
_scheduler.Start();
var mailNotifications = _miscService.GetAllMailNotifications();
foreach (var mailNotification in mailNotifications)
{
var notificationPlugin = _notificationService.GetNotificationPlugin(mailNotification.NotificationPluginSystemName);
if (notificationPlugin == null)
continue;
try
{
var job = JobBuilder.Create(notificationPlugin.GetType())
.WithIdentity(mailNotification.Id.ToString())
.SetJobData(new JobDataMap
{
{ "MailNotifications", new [] { mailNotification } }
})
.Build();
var trigger = TriggerBuilder.Create()
.WithCronSchedule(mailNotification.CronExpression)
.Build();
_scheduler.ScheduleJob(job, trigger);
_logger.Information(
String.Format("Mailbenachrichtigung \"({0}) {1} - {2}\" erfolgreich eingeplant.",
mailNotification.Id, mailNotification.NotificationPluginSystemName,
mailNotification.NotificationJobSystemName));
}
catch (Exception ex)
{
_logger.Error("Fehler beim Einplanen des Jobs für eine Mail-Benachrichtigung.", ex, null);
}
}
}
///
/// Determines the next execution time of a specific job
///
/// The job id.
public DateTime GetNextExecutionOfJob(string jobId)
{
//var scheduler = StdSchedulerFactory.GetDefaultScheduler();
var jobKey = new JobKey(jobId);
var nextFireTime = DateTime.MinValue;
var isJobExisting = _scheduler.CheckExists(jobKey);
if (isJobExisting)
{
var detail = _scheduler.GetJobDetail(jobKey);
var triggers = _scheduler.GetTriggersOfJob(jobKey);
if (triggers.Count > 0)
{
var nextFireTimeUtc = triggers[0].GetNextFireTimeUtc();
nextFireTime = TimeZone.CurrentTimeZone.ToLocalTime(nextFireTimeUtc.Value.DateTime);
}
}
return (nextFireTime);
}
///
/// Determines the next execution time of a specific mail notification
///
/// The mail notification job.
public DateTime GetNextExecutionOfJob(MailNotification mailNotification)
{
if (mailNotification == null)
return DateTime.MinValue;
return GetNextExecutionOfJob(mailNotification.Id.ToString());
}
}
}