Modules/DDZUGFeRDService/ThreadRunner.vb
2018-12-14 16:18:12 +01:00

98 lines
3.3 KiB
VB.net

Imports System.ComponentModel
Imports System.IO
Imports System.Timers
Imports System.Xml.XPath
Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Interfaces
Imports DigitalData.Modules.Jobs
Imports DigitalData.Modules.Jobs.ImportZUGFeRDFiles
Imports DigitalData.Modules.Logging
Public Class ThreadRunner
Private WithEvents _workerThread As BackgroundWorker
Private WithEvents _workerTimer As Timer
Private _logConfig As LogConfig
Private _logger As Logger
Private _firebird As Firebird
Private _watchDirectories As List(Of String)
Private _successDirectory As String
Private _errorDirectory As String
Private _zugferd As ZUGFeRDInterface
Private Const TIMER_INTERVAL = 60_000
Public Sub New(LogConfig As LogConfig, WatchDirectories As List(Of String), SuccessDirectory As String, ErrorDirectory As String)
_logConfig = LogConfig
_logger = _logConfig.GetLogger()
_watchDirectories = WatchDirectories
_successDirectory = SuccessDirectory
_errorDirectory = ErrorDirectory
_zugferd = New ZUGFeRDInterface(_logConfig)
If Not Directory.Exists(SuccessDirectory) Then
Throw New DirectoryNotFoundException("SuccessDirectory: " & SuccessDirectory)
End If
If Not Directory.Exists(ErrorDirectory) Then
Throw New DirectoryNotFoundException("ErrorDirectory: " & ErrorDirectory)
End If
For Each oDirectory In WatchDirectories
If Not Directory.Exists(oDirectory) Then
Throw New DirectoryNotFoundException("WatchDirectory: " & oDirectory)
End If
Next
_workerThread = New BackgroundWorker() With {
.WorkerReportsProgress = True,
.WorkerSupportsCancellation = True
}
_workerTimer = New Timer With {
.Interval = TIMER_INTERVAL
}
End Sub
Public Sub Start()
_workerTimer.Start()
_logger.Info("ThreadRunner started.")
End Sub
Public Sub [Stop]()
If _workerThread.IsBusy Then
_workerThread.CancelAsync()
_logger.Info("Worker cancelled.")
End If
_workerTimer.Stop()
_logger.Info("ThreadRunner stopped.")
End Sub
Private Sub TimerElapsed(sender As Object, e As ElapsedEventArgs) Handles _workerTimer.Elapsed
If Not _workerThread.IsBusy Then
_workerThread.RunWorkerAsync(New WorkerArgs() With {
.WatchDirectories = _watchDirectories,
.SuccessDirectory = _successDirectory,
.ErrorDirectory = _errorDirectory
})
Else
_logger.Warn("Worker is busy")
End If
End Sub
Private Sub DoWork(sender As Object, e As DoWorkEventArgs) Handles _workerThread.DoWork
Dim args As WorkerArgs = e.Argument
Dim job As New ImportZUGFeRDFiles(_logConfig, _firebird)
job.Start(args)
End Sub
Private Sub WorkProgress(sender As Object, e As ProgressChangedEventArgs) Handles _workerThread.ProgressChanged
Throw New NotImplementedException()
End Sub
Private Sub WorkCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles _workerThread.RunWorkerCompleted
Throw New NotImplementedException()
End Sub
End Class