2020-12-10 11:33:54 +01:00

58 lines
2.3 KiB
VB.net

Imports System.Threading
Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Logging
Imports Quartz
Imports Quartz.Listener
Public Class JobListener
Inherits JobListenerSupport
Public Overrides ReadOnly Property Name As String = "JobListener"
Public Property Dataset As DataSet
Private ReadOnly _Logger As Logger
Private ReadOnly _LogConfig As LogConfig
Private ReadOnly _MSSQL As MSSQLServer
Public Sub New(LogConfig As LogConfig, MSSQL As MSSQLServer, ResultDataSet As DataSet)
MyBase.New()
_LogConfig = LogConfig
_Logger = LogConfig.GetLogger()
_MSSQL = MSSQL
Dataset = ResultDataSet
End Sub
Public Overrides Function JobWasExecuted(context As IJobExecutionContext, jobException As JobExecutionException, Optional cancellationToken As CancellationToken = Nothing) As Task
Try
Dim oDataTable As DataTable = context.Result
Dim oDetailRow As DataRow = context.MergedJobDataMap.Item("CronJobDetails")
Dim oDatatableName As String = oDetailRow.Item("DT_NAME")
Dim oDetailId As Integer = oDetailRow.Item("GUID")
Dim oDatatableNameTemp As String = oDatatableName & "-TEMP"
If Dataset.Tables.Contains(oDatatableName) Then
_Logger.Debug("DataTable [{0}] exists, renaming and replacing in DataSet", oDatatableName)
' Rename the new table, add TEMP suffix
oDataTable.TableName = oDatatableNameTemp
' Add the new table to the dataset
Dataset.Tables.Add(oDataTable)
' Remove the old table
Dataset.Tables.Remove(oDatatableName)
' Rename the new table
Dataset.Tables.Item(oDatatableNameTemp).TableName = oDatatableName
Else
_Logger.Debug("DataTable [{0}] does not exist, adding to DataSet", oDatatableName)
Dataset.Tables.Add(oDataTable)
End If
_MSSQL.ExecuteNonQuery($"INSERT INTO TBAPPSERV_CRON_DETAIL_HISTORY (DETAIL_ID) VALUES ({oDetailId})")
Catch ex As Exception
_Logger.Warn("Unexpected error in JobListener: {0}", ex.Message)
_Logger.Error(ex)
End Try
Return MyBase.JobWasExecuted(context, jobException, cancellationToken)
End Function
End Class