151 lines
5.5 KiB
VB.net
151 lines
5.5 KiB
VB.net
Imports System.Collections.Specialized
|
|
Imports DigitalData.Modules.Config
|
|
Imports DigitalData.Modules.Database
|
|
Imports DigitalData.Modules.Jobs
|
|
Imports DigitalData.Modules.Logging
|
|
Imports DigitalData.Services.JobRunner.Config
|
|
Imports Quartz
|
|
Imports Quartz.Impl
|
|
Imports Quartz.Logging
|
|
|
|
Public Class JobRunner
|
|
Private ReadOnly _LogConfig As LogConfig
|
|
Private ReadOnly _Logger As DigitalData.Modules.Logging.Logger
|
|
Private ReadOnly _mssql As MSSQLServer
|
|
Private ReadOnly _config As Config
|
|
|
|
Private _factory As StdSchedulerFactory
|
|
Private _scheduler As IScheduler
|
|
|
|
Public Sub New(pLogConfig As LogConfig, pConfig As Config, pMSSQL As MSSQLServer)
|
|
_LogConfig = pLogConfig
|
|
_Logger = pLogConfig.GetLogger()
|
|
_mssql = pMSSQL
|
|
_config = pConfig
|
|
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"},
|
|
{"quartz.threadPool.threadCount", 10}
|
|
}
|
|
_factory = New StdSchedulerFactory(oProps)
|
|
_scheduler = Await _factory.GetScheduler()
|
|
|
|
_Logger.Info("Starting Scheduler..")
|
|
Await _scheduler.Start()
|
|
|
|
' [START] Job Scheduling
|
|
Await ScheduleJob(Of ADJob)("ADSync", GetJobConfig(JobConfig.JobType.ADSync))
|
|
Await ScheduleJob(Of TestJob)("TestJob", GetJobConfig(JobConfig.JobType.Test))
|
|
Await ScheduleJob(Of GraphQLJob)("GraphQLJob", GetJobConfig(JobConfig.JobType.GraphQL))
|
|
' [END] Job Scheduling
|
|
|
|
Catch ex As Exception
|
|
_Logger.Warn("Job Failed with message: [{0}].", ex.Message)
|
|
_Logger.Error(ex)
|
|
End Try
|
|
End Sub
|
|
|
|
Public Function GetJobConfig(pName As JobConfig.JobType) As JobConfig
|
|
Return _config.Jobs.Where(Function(j) j.Name = pName).SingleOrDefault()
|
|
End Function
|
|
|
|
Public Async Function ScheduleJob(Of T As Quartz.IJob)(JobName As String, pJobConfig As JobConfig) As Task
|
|
Dim oJobIdentity As String = JobName
|
|
Dim oTriggerIdentity As String = JobName & "-Trigger"
|
|
Dim oJobConfig As JobConfig = JobConfigParser.ParseConfig(pJobConfig)
|
|
Dim oJobData As New JobDataMap From {
|
|
{"LogConfig", _LogConfig},
|
|
{"Firebird", Nothing},
|
|
{"MSSQL", _mssql},
|
|
{"Args", oJobConfig.Args}
|
|
}
|
|
Dim oCountGraphJobs As Int16 = 0
|
|
Dim oJob = JobBuilder.Create(Of T)().
|
|
WithIdentity(oJobIdentity).
|
|
UsingJobData(oJobData).
|
|
Build()
|
|
|
|
Dim oTrigger = TriggerBuilder.Create().
|
|
WithIdentity(oTriggerIdentity).
|
|
StartNow().
|
|
WithCronSchedule(oJobConfig.CronSchedule).
|
|
Build()
|
|
|
|
If oJobConfig.Enabled Then
|
|
Await _scheduler.ScheduleJob(oJob, oTrigger)
|
|
|
|
_Logger.Info("Job {0} scheduled.", JobName)
|
|
oCountGraphJobs += 1
|
|
Else
|
|
_Logger.Info("Job {0} is disabled.", JobName)
|
|
End If
|
|
|
|
' If StartWithoutDelay is True, start Job after 10 Seconds
|
|
If oJobConfig.StartWithoutDelay And oCountGraphJobs > 0 Then
|
|
Dim oDebugJob = JobBuilder.Create(Of T)().
|
|
WithIdentity(oJobIdentity & "-DEBUG").
|
|
UsingJobData(oJobData).
|
|
Build()
|
|
|
|
Dim oDebugTrigger = TriggerBuilder.Create().
|
|
WithIdentity(oTriggerIdentity & "-DEBUG").
|
|
StartAt(DateBuilder.FutureDate(10, IntervalUnit.Second)).
|
|
Build()
|
|
|
|
_Logger.Info("Job {0} will start in 10 Seconds.", JobName)
|
|
Await _scheduler.ScheduleJob(oDebugJob, oDebugTrigger)
|
|
Else
|
|
If oCountGraphJobs = 0 Then
|
|
_Logger.Info("No Jobs configured!!")
|
|
End If
|
|
End If
|
|
End Function
|
|
|
|
Public Async Sub [Stop]()
|
|
_Logger.Info("Stopping JobRunner")
|
|
Await _scheduler.Shutdown()
|
|
End Sub
|
|
|
|
Private Class LogProvider
|
|
Implements ILogProvider
|
|
|
|
Private _Logger As 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
|
|
|
|
Public Function OpenMappedContext(key As String, value As Object, Optional destructure As Boolean = False) As IDisposable Implements ILogProvider.OpenMappedContext
|
|
_Logger.Warn("OpenMappedContext is not implemented")
|
|
Throw New NotImplementedException()
|
|
End Function
|
|
End Class
|
|
End Class
|