ZUGFeRDService: Remove Firebird Database, Fix Worker Busy Error

This commit is contained in:
Jonathan Jenne
2023-07-25 15:28:28 +02:00
parent 770ddef67c
commit a16d40169d
4 changed files with 30 additions and 120 deletions

View File

@@ -1,12 +1,10 @@
Imports System.ComponentModel
Imports System.IO
Imports System.Timers
Imports System.Xml.XPath
Imports DigitalData.Modules.Config
Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Interfaces
Imports DigitalData.Modules.Jobs
Imports DigitalData.Modules.Jobs.ImportZUGFeRDFiles
Imports DigitalData.Modules.Logging
Public Class ThreadRunner
@@ -17,7 +15,6 @@ Public Class ThreadRunner
Private ReadOnly _config As ConfigManager(Of Config)
Private ReadOnly _logConfig As LogConfig
Private ReadOnly _logger As Logger
Private ReadOnly _firebird As Firebird
Private ReadOnly _jobArguments As WorkerArgs
Private ReadOnly _mssql As MSSQLServer
@@ -27,7 +24,6 @@ Public Class ThreadRunner
Public Sub New(LogConfig As LogConfig, ConfigManager As ConfigManager(Of Config), Firebird As Firebird, Optional MSSQL As MSSQLServer = Nothing)
_logConfig = LogConfig
_logger = _logConfig.GetLogger()
_firebird = Firebird
_config = ConfigManager
_mssql = MSSQL
Try
@@ -61,12 +57,6 @@ Public Class ThreadRunner
oArgs = LoadFolderConfig(oArgs)
oArgs = LoadPropertyMap(oArgs)
' Use MSSQL Server if available
If Not IsNothing(_mssql) Then
_logger.Debug("Data will also be inserted into MSSQL Server.")
oArgs.InsertIntoSQLServer = True
End If
_logger.Debug("Custom Options:")
_logger.Debug("ExceptionEmailAddress: {0}", oArgs.ExceptionEmailAddress)
_logger.Debug("IgnoreRejectionStatus: {0}", oArgs.IgnoreRejectionStatus)
@@ -146,58 +136,12 @@ Public Class ThreadRunner
End If
End Sub
Private Sub MaybeUpdateRejected()
Try
Dim oTimeUnit = _config.Config.Custom.RejectionTransferTimeUnit
Dim oTimeValue = _config.Config.Custom.RejectionTransferTimeValue
Dim oDifference As TimeSpan = Now - RejectedLastRun
If oDifference.TotalMinutes < RejectedMaxDifferenceInMinutes Then
_logger.Debug("Updating rejected files: Waiting for next run.")
Exit Sub
End If
_logger.Info("Updating rejected files in Firebird.")
Dim oSQL = $"
SELECT [EMAIL_MSGID]
FROM TBEMLP_HISTORY
WHERE (STATUS = 'REJECTED' OR CUST_REJECTED = 1) AND FB_UPDATED = 0
AND DATEDIFF({oTimeUnit}, CHANGED_WHEN, GETDATE()) <= {oTimeValue}
ORDER BY GUID DESC"
Dim oTable As DataTable = _mssql.GetDatatable(oSQL)
If Not IsNothing(oTable) Then
For Each oROW As DataRow In oTable.Rows
Dim oUpdate = $"UPDATE TBEDM_ZUGFERD_HISTORY_IN SET REJECTED = True WHERE MESSAGE_ID = '{oROW.Item(0)}' and REJECTED = false"
If _firebird.ExecuteNonQuery(oUpdate) = True Then
Dim oUpdateSQL = $"
UPDATE TBEMLP_HISTORY
SET FB_UPDATED = 1
WHERE [EMAIL_MSGID] = '{oROW.Item(0)}' AND FB_UPDATED = 0"
_mssql.ExecuteNonQuery(oUpdateSQL)
End If
Next
RejectedLastRun = Now
End If
Catch ex As Exception
_logger.Warn("Error while Updating REJECTED State: " & ex.Message)
End Try
End Sub
Private Sub DoWork(sender As Object, e As DoWorkEventArgs) Handles _workerThread.DoWork
Try
If _config.Config.Custom.RejectionTransferEnabled = True Then
MaybeUpdateRejected()
Else
_logger.Debug("Transferring rejection status to Firebird is disabled.")
End If
Dim oArgs As WorkerArgs = e.Argument
_logger.Debug("Background worker running..")
Dim oJob As New ImportZUGFeRDFiles(_logConfig, _firebird, _mssql)
Dim oJob As New ImportZUGFeRDFiles(_logConfig, _mssql)
oJob.Start(oArgs)
Catch ex As Exception
_logger.Warn("Background worker failed!")
@@ -210,44 +154,20 @@ Public Class ThreadRunner
End Sub
Private Function LoadFolderConfig(pArgs As WorkerArgs) As WorkerArgs
Dim oSQL As String = "SELECT T1.FOLDER_TYPE, T.FOLDER_PATH FROM TBEDM_FOLDER T, TBEDM_FOLDER_TYPE T1 WHERE T.FOLDER_TYPE_ID = T1.GUID AND T1.""ACTIVE"" = True AND T.""ACTIVE"" = True"
Dim oResult As DataTable = _firebird.GetDatatable(oSQL)
For Each oRow As DataRow In oResult.Rows
Dim oFolderType = oRow.Item("FOLDER_TYPE")
Select Case oFolderType
Case ZUGFERD_IN
pArgs.WatchDirectories.Add(oRow.Item("FOLDER_PATH"))
Case ZUGFERD_SUCCESS
pArgs.SuccessDirectory = oRow.Item("FOLDER_PATH")
Case ZUGFERD_ERROR
pArgs.ErrorDirectory = oRow.Item("FOLDER_PATH")
Case ZUGFERD_EML
pArgs.OriginalEmailDirectory = oRow.Item("FOLDER_PATH")
Case ZUGFERD_REJECTED_EML
pArgs.RejectedEmailDirectory = oRow.Item("FOLDER_PATH")
Case ZUGFERD_ATTACHMENTS
pArgs.AttachmentsSubDirectory = oRow.Item("FOLDER_PATH")
Case ZUGFERD_NO_ZUGFERD
_logger.Info($"## {ZUGFERD_NO_ZUGFERD}-Constant has been defined! [{oRow.Item("FOLDER_PATH")}]##")
pArgs.NonZugferdDirectory = oRow.Item("FOLDER_PATH")
End Select
Next
pArgs.WatchDirectories.Add(_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 TBEDM_XML_ITEMS WHERE ACTIVE = True ORDER BY XML_PATH"
Dim oResult As DataTable = _firebird.GetDatatable(oSQL)
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 = oRow.Item("XML_PATH")