Modules/Service.JobRunner/JobRunner.vb
2019-04-30 16:54:04 +02:00

178 lines
6.3 KiB
VB.net

Imports System.Collections.Specialized
Imports System.Text.RegularExpressions
Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Jobs
Imports DigitalData.Modules.Logging
Imports Quartz
Imports Quartz.Impl
Imports Quartz.Logging
Public Class JobRunner
Private _LogConfig As LogConfig
Private _Logger As DigitalData.Modules.Logging.Logger
Private _firebird As Firebird
Private _mssql As MSSQLServer
Private _Props As New NameValueCollection From {
{"quartz.serializer.type", "binary"},
{"quartz.threadPool.threadCount", 10}
}
Private _factory As StdSchedulerFactory
Private _scheduler As IScheduler
Public Sub New(LogConfig As LogConfig, MSSQL As MSSQLServer, Firebird As Firebird)
_LogConfig = LogConfig
_Logger = LogConfig.GetLogger()
_firebird = Firebird
_mssql = MSSQL
End Sub
Public Async Sub Start()
Try
Logging.LogProvider.SetCurrentLogProvider(New LogProvider(_Logger))
_Logger.Info("Starting JobRunner")
Dim oProps As New NameValueCollection From {
{"quartz.serializer.type", "binary"}
}
_factory = New StdSchedulerFactory(oProps)
_scheduler = Await _factory.GetScheduler()
_Logger.Info("Starting Scheduler..")
Await _scheduler.Start()
' [START] Job Scheduling
' ADSync
Await ScheduleJob(Of ADJob)("ADSync", My.Settings.ADSYNC_CONFIG)
' Test Job
Await ScheduleJob(Of TestJob)("TestJob", My.Settings.TEST_CONFIG)
' [END] Job Scheduling
Catch ex As Exception
_Logger.Warn("Job Failed.")
_Logger.Error(ex)
End Try
End Sub
Public Async Function ScheduleJob(Of T As Quartz.IJob)(JobName As String, JobConfigString As String) As Task
Dim oJobIdentity As String = JobName
Dim oTriggerIdentity As String = JobName & "-Trigger"
Dim oJobConfig As JobConfig = JobConfigParser.ParseConfig(JobConfigString)
Dim oJobData As New JobDataMap From {
{"LogConfig", _LogConfig},
{"Firebird", _firebird},
{"MSSQL", _mssql},
{"Args", oJobConfig.Arguments}
}
Dim oJobDetail = JobBuilder.Create(Of T)().
WithIdentity(oJobIdentity).
UsingJobData(oJobData).
Build()
Dim oTrigger = TriggerBuilder.Create().
WithIdentity(oTriggerIdentity).
StartNow().
WithCronSchedule(oJobConfig.CronExpression).
Build()
If oJobConfig.Enabled Then
Await _scheduler.ScheduleJob(oJobDetail, oTrigger)
_Logger.Info("Job {0} started.", JobName)
Else
_Logger.Info("Job {0} is disabled.", JobName)
End If
End Function
Public Async Sub [Stop]()
_Logger.Info("Stopping JobRunner")
Await _scheduler.Shutdown()
End Sub
Public Class ADJob
Implements Quartz.IJob
Public Function Execute(context As IJobExecutionContext) As Task Implements Quartz.IJob.Execute
Dim oJobData = context.MergedJobDataMap
Dim oLogConfig As LogConfig = oJobData.Item("LogConfig")
Dim oFirebird As Firebird = oJobData.Item("Firebird")
Dim oMSSQL As MSSQLServer = oJobData.Item("MSSQL")
Dim oArgs As Dictionary(Of String, String) = oJobData.Item("Args")
Dim oADJobArgs = New ADSyncArgs()
If oArgs.ContainsKey("RootPath") Then
oADJobArgs.RootPath = oArgs.Item("RootPath")
End If
If oArgs.ContainsKey("DisableFirebird") AndAlso oArgs.Item("DisableFirebird") = "True" Then
oFirebird = Nothing
End If
If oArgs.ContainsKey("DisableMSSQL") AndAlso oArgs.Item("DisableMSSQL") = "True" Then
oMSSQL = Nothing
End If
Dim oADSyncJob As New ADSyncJob(oLogConfig, oFirebird, oMSSQL)
oADSyncJob.Start(oADJobArgs)
Return Task.FromResult(True)
End Function
End Class
Public Class TestJob
Implements Quartz.IJob
Public Function Execute(context As IJobExecutionContext) As Task Implements Quartz.IJob.Execute
Dim oJobData = context.MergedJobDataMap
Dim oLogConfig As LogConfig = oJobData.Item("LogConfig")
Dim oLogger = oLogConfig.GetLogger()
Dim oArgs As Dictionary(Of String, String) = oJobData.Item("Args")
If oArgs.ContainsKey("Arg1") Then
Dim oArg1 As String = oArgs.Item("Arg1")
oLogger.Info("Running Test Job With Arg1: {0}", oArg1)
Else
oLogger.Warn("Running Test Job With missing Arg1 :/")
End If
Return Task.FromResult(True)
End Function
End Class
Private Class LogProvider
Implements ILogProvider
Private _Logger As DigitalData.Modules.Logging.Logger
Public Sub New(Logger As DigitalData.Modules.Logging.Logger)
MyBase.New()
_Logger = Logger
End Sub
Private Function GetLogger(name As String) As Logging.Logger Implements ILogProvider.GetLogger
Return Function(level, func, exception, parameters)
If exception IsNot Nothing Then
_Logger.Error(exception)
ElseIf level >= LogLevel.Debug AndAlso func IsNot Nothing Then
_Logger.Debug(func(), parameters)
ElseIf level >= LogLevel.Info AndAlso func IsNot Nothing Then
_Logger.Info(func(), parameters)
End If
Return True
End Function
End Function
Private Function OpenNestedContext(message As String) As IDisposable Implements ILogProvider.OpenNestedContext
_Logger.Warn("OpenNestedContext is not implemented")
Throw New NotImplementedException()
End Function
Private Function OpenMappedContext(key As String, value As String) As IDisposable Implements ILogProvider.OpenMappedContext
_Logger.Warn("OpenMappedContext is not implemented")
Throw New NotImplementedException()
End Function
End Class
End Class