Imports System.ComponentModel Imports System.Timers Imports DigitalData.Modules.Database Imports DigitalData.Modules.Jobs Imports DigitalData.Modules.Logging Public Class JobRunner Private WithEvents _workerThread As BackgroundWorker Private WithEvents _workerTimer As Timer Private ReadOnly _interval As Long Private ReadOnly _logConfig As LogConfig Private ReadOnly _logger As Logger Private ReadOnly _mssql As MSSQLServer Private ReadOnly _firebird As Firebird Public Sub New(LogConfig As LogConfig, Firebird As Firebird, MSSQL As MSSQLServer, Interval As Long) _logConfig = LogConfig _logger = _logConfig.GetLogger() _firebird = Firebird _mssql = MSSQL _interval = Interval _workerTimer = New Timer() _workerThread = New BackgroundWorker() With { .WorkerReportsProgress = True, .WorkerSupportsCancellation = True } End Sub Public Sub Start() _workerTimer.Interval = _interval * 1000 _workerTimer.Start() _logger.Debug("JobRunner started with {0}s Interval.", _interval) End Sub Public Sub [Stop]() Try _logger.Debug("Stopping Background worker...") If _workerThread.IsBusy Then _workerThread.CancelAsync() _logger.Debug("Background Worker cancelled.") End If _workerTimer.Stop() _logger.Debug("Background Worker stopped.") Catch ex As Exception _logger.Error(ex) End Try End Sub Private Sub TimerElapsed(sender As Object, e As ElapsedEventArgs) Handles _workerTimer.Elapsed If Not _workerThread.IsBusy Then _workerThread.RunWorkerAsync() Else _logger.Warn("Background Worker is busy. Waiting for next interval.") End If End Sub Private Sub DoWork(sender As Object, e As DoWorkEventArgs) Handles _workerThread.DoWork _logger.Debug("Background worker running..") Dim args As WorkerArgs = e.Argument Dim oJob As New ADSyncJob(_logConfig, _firebird, _mssql) Dim oArgs As New ADSyncArgs() With { .Enabled = My.Settings.JOB_ADSYNC_ENABLED, .Interval = My.Settings.JOB_ADSYNC_INTERVAL, .RootPath = My.Settings.JOB_ADSYNC_ROOT_PATH } If oJob.ShouldStart(oArgs) Then oJob.Start(oArgs) End If End Sub Private Sub WorkCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles _workerThread.RunWorkerCompleted If e.Error Is Nothing Then _logger.Debug("Background worker completed!") Else _logger.Warn("Background worker failed!") _logger.Error(e.Error) End If End Sub End Class