205 lines
8.3 KiB
VB.net
205 lines
8.3 KiB
VB.net
Imports System.ComponentModel
|
|
Imports System.IO
|
|
Imports System.Timers
|
|
Imports DigitalData.Modules.Config
|
|
Imports DigitalData.Modules.Database
|
|
Imports DigitalData.Modules.Interfaces
|
|
Imports DigitalData.Modules.Jobs
|
|
Imports DigitalData.Modules.Logging
|
|
|
|
Public Class ThreadRunner
|
|
|
|
Private WithEvents _workerThread As BackgroundWorker
|
|
Private WithEvents _workerTimer As Timer
|
|
|
|
Private ReadOnly _config As ConfigManager(Of Config)
|
|
Private ReadOnly _logConfig As LogConfig
|
|
Private ReadOnly _logger As Logger
|
|
Private ReadOnly _jobArguments As WorkerArgs
|
|
Private ReadOnly _mssql As MSSQLServer
|
|
|
|
Private BusyCounter As Integer = 0
|
|
|
|
Public Sub New(LogConfig As LogConfig, ConfigManager As ConfigManager(Of Config), MSSQL As MSSQLServer)
|
|
_logConfig = LogConfig
|
|
_logger = _logConfig.GetLogger()
|
|
_config = ConfigManager
|
|
_mssql = MSSQL
|
|
Try
|
|
Dim directory As New IO.DirectoryInfo(_logConfig.LogDirectory)
|
|
|
|
For Each file As IO.FileInfo In directory.GetFiles
|
|
If (Now - file.CreationTime).Days > 29 Then
|
|
file.Delete()
|
|
Else
|
|
Exit For
|
|
End If
|
|
|
|
|
|
Next
|
|
Catch ex As Exception
|
|
|
|
End Try
|
|
|
|
Dim oArgs As New WorkerArgs With {
|
|
.ExceptionEmailAddress = _config.Config.ExceptionEmailAddress,
|
|
.IgnoreRejectionStatus = _config.Config.Custom.IgnoreRejectionStatus,
|
|
.MaxAttachmentSizeInMegaBytes = _config.Config.Custom.MaxAttachmentSizeInMegaBytes,
|
|
.MinFileAgeInMinutes = _config.Config.Custom.MinFileAgeInMinutes,
|
|
.NamePortal = _config.Config.PORTAL_NAME,
|
|
.EmailOutProfileId = _config.Config.MSSQLEmailOutAccountID,
|
|
.AllowFacturX = _config.Config.Custom.AllowFacturX,
|
|
.AllowXRechnung = _config.Config.Custom.AllowXRechnung,
|
|
.AllowZugferd10 = _config.Config.Custom.AllowZugferd10,
|
|
.AllowZugferd2x = _config.Config.Custom.AllowZugferd2x,
|
|
.AllowPeppolBISBill3x = _config.Config.Custom.AllowPeppolBISBill3x,
|
|
.RejectionTemplateId = _config.Config.Custom.RejectionTemplateId,
|
|
.GDPictureVersion = _config.Config.Custom.GDPictureVersion
|
|
}
|
|
|
|
oArgs = LoadFolderConfig(oArgs)
|
|
oArgs = LoadPropertyMap(oArgs)
|
|
|
|
_logger.Debug("Custom Options:")
|
|
_logger.Debug("ExceptionEmailAddress: [{0}]", oArgs.ExceptionEmailAddress)
|
|
_logger.Debug("IgnoreRejectionStatus: [{0}]", oArgs.IgnoreRejectionStatus)
|
|
_logger.Debug("MaxAttachmentSizeInMegaBytes: [{0}]", oArgs.MaxAttachmentSizeInMegaBytes)
|
|
_logger.Debug("MinFileAgeInMinutes: [{0}]", oArgs.MinFileAgeInMinutes)
|
|
_logger.Debug("RejectionTemplateId: [{0}]", oArgs.RejectionTemplateId)
|
|
_logger.Debug("GDPictureVersion: [{0}]", oArgs.GDPictureVersion)
|
|
|
|
_jobArguments = oArgs
|
|
|
|
_logger.Debug("Checking SuccessDirectory [{0}]", oArgs.SuccessDirectory)
|
|
If Not Directory.Exists(oArgs.SuccessDirectory) Then
|
|
_logger.Warn("SuccessDirectory [{0}] does not exist!", oArgs.SuccessDirectory)
|
|
End If
|
|
|
|
_logger.Debug("Checking ErrorDirectory [{0}]", oArgs.ErrorDirectory)
|
|
If Not Directory.Exists(oArgs.ErrorDirectory) Then
|
|
_logger.Warn("ErrorDirectory [{0}] does not exist!", oArgs.ErrorDirectory)
|
|
End If
|
|
|
|
_logger.Debug("Checking Original Email Directory [{0}]", oArgs.OriginalEmailDirectory)
|
|
If Not Directory.Exists(oArgs.OriginalEmailDirectory) Then
|
|
_logger.Warn("OriginalEmailDirectory [{0}] does not exist!", oArgs.OriginalEmailDirectory)
|
|
End If
|
|
|
|
_logger.Debug("Checking Rejected Email Directory [{0}]", oArgs.RejectedEmailDirectory)
|
|
If Not Directory.Exists(oArgs.RejectedEmailDirectory) Then
|
|
_logger.Warn("RejectedEmailDirectory [{0}] does not exist!", oArgs.RejectedEmailDirectory)
|
|
End If
|
|
|
|
_logger.Debug("Checking Non ZUGFeRD Directory [{0}]", oArgs.NonZugferdDirectory)
|
|
If Not Directory.Exists(oArgs.NonZugferdDirectory) Then
|
|
_logger.Warn("NonZugferdDirectory [{0}] does not exist!", oArgs.NonZugferdDirectory)
|
|
End If
|
|
|
|
_logger.Debug("Checking Watch Directory [{0}]", oArgs.WatchDirectory)
|
|
If Not Directory.Exists(oArgs.WatchDirectory) Then
|
|
_logger.Warn("WatchDirectory [{0}] does not exist!", oArgs.WatchDirectory)
|
|
End If
|
|
|
|
_logger.Debug("Checking Exception Email Adress [{0}]", oArgs.ExceptionEmailAddress)
|
|
If oArgs.ExceptionEmailAddress = String.Empty Then
|
|
_logger.Warn("ExceptionEmailAddress [{0}] is not set!", oArgs.ExceptionEmailAddress)
|
|
End If
|
|
|
|
_workerThread = New BackgroundWorker() With {
|
|
.WorkerReportsProgress = False,
|
|
.WorkerSupportsCancellation = True
|
|
}
|
|
|
|
_workerTimer = New Timer()
|
|
End Sub
|
|
|
|
Public Sub Start(Interval As Integer)
|
|
_workerTimer.Interval = Interval * 1000
|
|
_workerTimer.Start()
|
|
_logger.Debug("ThreadRunner started with {0}s Interval.", Interval)
|
|
End Sub
|
|
|
|
Public Sub [Stop]()
|
|
Try
|
|
If _workerThread.IsBusy Then
|
|
_workerThread.CancelAsync()
|
|
_logger.Debug("Worker cancelled.")
|
|
End If
|
|
_workerTimer.Stop()
|
|
_logger.Debug("ThreadRunner 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
|
|
BusyCounter = 0
|
|
_workerThread.RunWorkerAsync(_jobArguments)
|
|
Else
|
|
BusyCounter = +1
|
|
_logger.Info("Worker is busy, skipping execution. Tried for [{0}] times.", BusyCounter)
|
|
End If
|
|
End Sub
|
|
|
|
Private Sub DoWork(sender As Object, e As DoWorkEventArgs) Handles _workerThread.DoWork
|
|
Try
|
|
Dim oArgs As WorkerArgs = e.Argument
|
|
_logger.Debug("Background worker running..")
|
|
|
|
Dim oJob As New ImportZUGFeRDFiles(_logConfig, _mssql)
|
|
oJob.Start(oArgs)
|
|
Catch ex As Exception
|
|
_logger.Warn("Background worker failed!")
|
|
_logger.Error(ex)
|
|
End Try
|
|
End Sub
|
|
|
|
Private Sub WorkCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles _workerThread.RunWorkerCompleted
|
|
_logger.Debug("Background worker completed!")
|
|
End Sub
|
|
|
|
Private Function LoadFolderConfig(pArgs As WorkerArgs) As WorkerArgs
|
|
pArgs.WatchDirectory = _config.Config.WatchDirectory
|
|
pArgs.SuccessDirectory = _config.Config.SuccessDirectory
|
|
pArgs.ErrorDirectory = _config.Config.ErrorDirectory
|
|
pArgs.OriginalEmailDirectory = _config.Config.OriginalEmailDirectory
|
|
pArgs.RejectedEmailDirectory = _config.Config.RejectedEmailDirectory
|
|
pArgs.AttachmentsSubDirectory = _config.Config.AttachmentsSubDirectory
|
|
pArgs.NonZugferdDirectory = _config.Config.NonZugferdDirectory
|
|
|
|
Return pArgs
|
|
End Function
|
|
|
|
Private Function LoadPropertyMap(pArgs As WorkerArgs) As WorkerArgs
|
|
Dim oSQL As String = $"SELECT * FROM TBDD_ZUGFERD_XML_ITEMS WHERE ACTIVE = 1 ORDER BY XML_PATH"
|
|
Dim oResult As DataTable = _mssql.GetDatatable(oSQL)
|
|
|
|
For Each oRow As DataRow In oResult.Rows
|
|
Dim oXmlPath As String = oRow.Item("XML_PATH")
|
|
Dim oTableName = oRow.Item("TABLE_NAME")
|
|
Dim oTableColumn = oRow.Item("TABLE_COLUMN")
|
|
Dim oDescription = oRow.Item("DESCRIPTION")
|
|
Dim oIsRequired = oRow.Item("IS_REQUIRED")
|
|
Dim oIsGrouped = oRow.Item("IS_GROUPED")
|
|
Dim oGroupScope = oRow.Item("GROUP_SCOPE")
|
|
Dim oSpecification = oRow.Item("SPECIFICATION")
|
|
Dim oItemType = oRow.Item("ITEM_TYPE")
|
|
|
|
pArgs.PropertyMap.Add(oXmlPath, New XmlItemProperty() With {
|
|
.Description = oDescription,
|
|
.TableName = oTableName,
|
|
.TableColumn = oTableColumn,
|
|
.IsRequired = oIsRequired,
|
|
.IsGrouped = oIsGrouped,
|
|
.GroupScope = oGroupScope,
|
|
.Specification = oSpecification,
|
|
.XMLPath = oXmlPath.Replace(".Value", ""),
|
|
.ItemType = oItemType
|
|
})
|
|
Next
|
|
|
|
Return pArgs
|
|
End Function
|
|
End Class
|