Imports System.Collections.Specialized Imports System.Threading Imports DigitalData.Modules.Database Imports DigitalData.Modules.Logging Imports Quartz Imports Quartz.Logging.OperationName Namespace Scheduler Public Class JobScheduler Private ReadOnly Settings As New NameValueCollection From { {"quartz.serializer.type", "binary"}, {"quartz.threadPool.threadCount", 10} } Private ReadOnly LogConfig As LogConfig Private ReadOnly Logger As Logger Private ReadOnly Database As MSSQLServer Private ReadOnly Factory As Quartz.Impl.StdSchedulerFactory Private ReadOnly State As State Private Scheduler As IScheduler Private Const JOB_TYPE_IMPORT As Integer = 1 Private Const JOB_TYPE_INDEX As Integer = 2 Public Sub New(pLogConfig As LogConfig, pDatabase As MSSQLServer, pState As State) LogConfig = pLogConfig Logger = pLogConfig.GetLogger() Factory = New Impl.StdSchedulerFactory(Settings) Database = pDatabase State = pState End Sub Public Async Function Start() As Task(Of Boolean) Try ' Log all quartz events into our standard log files Logging.LogProvider.SetCurrentLogProvider(New LogProvider(LogConfig)) ' initialize the scheduler Scheduler = Await Factory.GetScheduler() Scheduler.ListenerManager.AddJobListener(New JobListener(LogConfig, State.JobHistory)) ' start the scheduler Await Scheduler.Start() ' load job Config and setup job schedules Logger.Info("Loading [{0}] Job Definitions..", State.JobDefinitions.Count) For Each oJob In State.JobDefinitions Logger.Debug("Loading Job Definition [{0}]", oJob.Name) Select Case oJob.TypeId Case JOB_TYPE_IMPORT Await ScheduleJob(Of Jobs.FileImportJob)(New JobConfig With { .Name = oJob.Name, .Enabled = True, .Arguments = New Dictionary(Of String, String) From { {"Name", oJob.Name} }, .CronSchedule = oJob.CronSchedule }) Case JOB_TYPE_INDEX Await ScheduleJob(Of Jobs.FileIndexJob)(New JobConfig With { .Name = oJob.Name, .Enabled = True, .Arguments = New Dictionary(Of String, String) From { {"Name", oJob.Name} }, .CronSchedule = oJob.CronSchedule }) End Select Next ' setup debug job Await ScheduleJob(Of Jobs.DebugJob)(New JobConfig With { .Name = "Debug Job", .Enabled = True, .Arguments = New Dictionary(Of String, String) From {{"Arg1", "My awesome argument"}}, .CronSchedule = "0 * * * * ?" }) Return True Catch ex As Exception Logger.Error(ex) Return False End Try End Function Public Async Function Shutdown() As Task Await Scheduler.Shutdown() End Function Public Async Function ScheduleJob(Of T As IJob)(pJobConfig As JobConfig) As Task Dim oJobName As String = pJobConfig.Name Dim oTriggerName As String = $"{oJobName}-TRIGGER" pJobConfig.Name = oJobName Dim oJobData As New JobDataMap From { {Constants.Scheduler.JOB_CONFIG_LOGCONFIG, LogConfig}, {Constants.Scheduler.JOB_CONFIG_ARGUMENTS, pJobConfig.Arguments}, {Constants.Scheduler.JOB_CONFIG_DATABASE, Database} } Dim oJob = JobBuilder.Create(Of T)(). WithIdentity(oJobName). UsingJobData(oJobData). Build() Dim oTrigger = TriggerBuilder.Create(). WithIdentity(oTriggerName). StartNow(). WithCronSchedule(pJobConfig.CronSchedule). Build() If pJobConfig.Enabled Then Await Scheduler.ScheduleJob(oJob, oTrigger) Logger.Info("Job {0} scheduled.", oJobName) Else Logger.Info("Job {0} is disabled.", oJobName) End If If pJobConfig.StartWithoutDelay Then Dim oDebugJob = JobBuilder.Create(Of T)(). WithIdentity(oJobName & "-DEBUG"). UsingJobData(oJobData). Build() Dim oDebugTrigger = TriggerBuilder.Create(). WithIdentity(oTriggerName & "-DEBUG"). StartAt(DateBuilder.FutureDate(10, IntervalUnit.Second)). Build() Logger.Info("Job {0} will start in 10 Seconds.", oJobName) Await Scheduler.ScheduleJob(oDebugJob, oDebugTrigger) End If End Function End Class End Namespace