EDMIService: Small fixes and exception handling

This commit is contained in:
Jonathan Jenne
2020-12-23 14:19:47 +01:00
parent 62ac7860ef
commit 590407fbce
20 changed files with 638 additions and 201 deletions

View File

@@ -64,7 +64,7 @@ Public Class DatatableJob
oResultTable.TableRelationColumn = oParentColumn
End If
oLogger.Info("Fetched Datatable [{0}]", oDatatableName)
oLogger.Debug("Fetched Datatable [{0}]", oDatatableName)
oResult.Tables.Add(oResultTable)
Catch ex As Exception
oLogger.Warn("Execute: Error while saving Table: [{0}]", oDatatableName)
@@ -72,6 +72,8 @@ Public Class DatatableJob
End Try
Next
oLogger.Info("Fetched [{0}] Datatables", oResult.Tables.Count)
' Das Ergebnis speichern
context.Result = oResult
@@ -81,60 +83,5 @@ Public Class DatatableJob
End Try
Return Task.FromResult(True)
#Region "OLD"
'Dim oJobData = context.MergedJobDataMap
'Dim oLogConfig As LogConfig = oJobData.Item("LogConfig")
'Dim oLogger As Logger = oLogConfig.GetLogger()
'Dim oDetailRow As DataRow = oJobData.Item("CronJobDetails")
'Dim oDatatableName As String = NotNull(oDetailRow.Item("DT_NAME"), String.Empty)
'Try
' Dim oCronJobTitle As String = oJobData.Item("CronJobTitle")
' Dim oMSSQL As MSSQLServer = oJobData.Item("MSSQL")
' Dim oConnectionId As Integer = NotNull(oDetailRow.Item("CON_ID"), String.Empty)
' Dim oTitle As String = NotNull(oDetailRow.Item("TITLE"), String.Empty)
' Dim oSQL As String = NotNull(oDetailRow.Item("COMMAND"), String.Empty)
' oLogger.Debug("Running Command-Job [{0}]", oTitle)
' oLogger.Debug("Datatable Name: {0}", oDatatableName)
' oLogger.Debug("Connection Id: {0}", oConnectionId)
' Dim oConnectionString = oMSSQL.Get_ConnectionStringforID(oConnectionId)
' Dim oTable = oMSSQL.GetDatatableWithConnection(oSQL, oConnectionString)
' oTable.TableName = oDatatableName
' oLogger.Debug("Result Datatable [{0}] contains [{1}] rows", oTable.TableName, oTable.Rows.Count)
' Dim oResult = New JobResult() With {
' .Table = oTable
' }
' Dim oChildTableNAme As String = NotNull(oDetailRow.Item("CHILD_DT_NAME"), String.Empty)
' If oChildTableNAme <> String.Empty Then
' Dim oParentColumn As String = NotNull(oDetailRow.Item("DT_COLUMN"), String.Empty)
' Dim oChildColumn As String = NotNull(oDetailRow.Item("CHILD_DT_COLUMN"), String.Empty)
' oLogger.Debug("Child Datatable [{0}] defined, Relation: Parent [{1}] -> Child [{2}]", oChildTableName, oParentColumn, oChildColumn)
' Dim oChildTable As DataTable = oMSSQL.GetDatatableWithConnection($"SELECT * FROM {oChildTableName}", oConnectionString)
' oChildTable.TableName = oChildTableName
' oLogger.Debug("Child Datatable [{0}] contains [{1}] rows", oChildTable.TableName, oChildTable.Rows.Count)
' oResult.ChildTable = oChildTable
' oResult.ChildRelationColumn = oChildColumn
' oResult.TableRelationColumn = oParentColumn
' End If
' ' Das Ergebnis speichern
' context.Result = oResult
'Catch ex As Exception
' oLogger.Error(ex)
' oLogger.Warn("Unhandled exception while executing SQL for Datatable {0}", oDatatableName)
'End Try
'Return Task.FromResult(True)
#End Region
End Function
End Class

View File

@@ -167,8 +167,8 @@ Public Class JobListener
Dim oDatatableNameTemp As String = oName & "-TEMP"
' Used for debugging relations and constraints
_Logger.Debug("Dataset BEFORE saving datatables")
ListTables(Dataset)
'_Logger.Debug("Dataset BEFORE saving datatables")
'ListTables(Dataset)
If Dataset.Tables.Contains(oName) Then
' Replace existing table
@@ -193,14 +193,9 @@ Public Class JobListener
_Logger.Debug(oDataTable.TableName)
Next
_Logger.Debug("Listing Relations in Dataset")
For Each oRelation As DataRelation In Dataset.Relations
_Logger.Debug(oRelation.RelationName)
Next
' Used for debugging relations and constraints
_Logger.Debug("Dataset AFTER saving datatables")
ListTables(Dataset)
'_Logger.Debug("Dataset AFTER saving datatables")
'ListTables(Dataset)
_MSSQL.ExecuteNonQuery($"INSERT INTO TBAPPSERV_CRON_DETAIL_HISTORY (DETAIL_ID) VALUES ({oDetailId})")
Catch ex As Exception

View File

@@ -0,0 +1,170 @@
Imports System.Collections.Specialized
Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Logging
Imports Quartz
Imports Quartz.Impl
Imports Quartz.Impl.Matchers
Imports Quartz.Logging
Public Class Scheduler
Private _Factory As StdSchedulerFactory
Private _MSSQL As MSSQLServer
Private _Scheduler As IScheduler
Private _LogConfig As LogConfig
Private _Logger As DigitalData.Modules.Logging.Logger
Private _JobListener As JobListener
Private _Props = New NameValueCollection From {
{"quartz.serializer.type", "binary"}
}
Private Const JOB_GROUP As String = "DatatableJobs"
Public Sub New(LogConfig As LogConfig, MSSQL_ECM As MSSQLServer, TableStore As DataSet)
_LogConfig = LogConfig
_Logger = LogConfig.GetLogger()
_Factory = New StdSchedulerFactory(_Props)
_MSSQL = MSSQL_ECM
_JobListener = New JobListener(LogConfig, _MSSQL, TableStore)
Logging.LogProvider.SetCurrentLogProvider(New LogProvider(_Logger))
End Sub
Public Async Sub Start()
' Get new scheduler
_Scheduler = Await _Factory.GetScheduler()
' configure it
_Scheduler.ListenerManager.AddJobListener(_JobListener,
GroupMatcher(Of JobKey).GroupEquals(JOB_GROUP))
' start it
Await _Scheduler.Start()
Dim oCronjobs As DataTable = Await GetCronJobs()
Try
If oCronjobs IsNot Nothing Then
_Logger.Debug("Loaded {0} cron jobs", oCronjobs.Rows.Count)
For Each oRow As DataRow In oCronjobs.Rows
Dim oDefinition As String = oRow.Item("CRON_DEFINITION")
Dim oTitle As String = oRow.Item("TITLE")
Dim oGuid As Integer = oRow.Item("GUID")
Dim oCronTrigger As ITrigger
Dim oInitTrigger As ITrigger
Dim oJob As IJobDetail
Dim oInitJob As IJobDetail
Dim oJobData As New JobDataMap From {
{"LogConfig", _LogConfig},
{"MSSQL", _MSSQL},
{"CronJobId", oGuid},
{"CronJobTitle", oTitle}
}
Dim oIdentity As String = $"CRON-JOB-{oGuid}"
oCronTrigger = TriggerBuilder.Create().
WithIdentity(oIdentity & "-TRIGGER", JOB_GROUP).
WithCronSchedule(oDefinition).
Build()
oJob = JobBuilder.Create(Of DatatableJob)().
WithIdentity(oIdentity, JOB_GROUP).
UsingJobData(oJobData).
Build()
Await _Scheduler.ScheduleJob(oJob, oCronTrigger)
' Also create variant of the job that runs immediately, once
Dim oInitIdentity As String = $"CRON-JOB-INIT-{oGuid}"
oInitTrigger = TriggerBuilder.Create().
WithIdentity(oInitIdentity & "-TRIGGER", JOB_GROUP).
StartNow().
Build()
oInitJob = JobBuilder.Create(Of DatatableJob)().
WithIdentity(oInitIdentity, JOB_GROUP).
UsingJobData(oJobData).
Build()
Await _Scheduler.ScheduleJob(oInitJob, oInitTrigger)
_Logger.Debug("Scheduled a new job for Cron Job [{0}]", oTitle)
Next
Else
_Logger.Warn("CronJobs could not be fetched!")
End If
Catch ex As Exception
_Logger.Error(ex)
_Logger.Warn("Unexpected Error while setting up scheduler: " & ex.Message)
End Try
End Sub
Public Async Function GetCronJobs() As Task(Of DataTable)
Try
Dim oSQL As String = "SELECT * FROM TBAPPSERV_CRON_JOB WHERE ACTIVE = 1"
Dim oDatatable As DataTable = Await _MSSQL.GetDatatableAsync(oSQL)
Return oDatatable
Catch ex As Exception
Return Nothing
End Try
End Function
Public Async Function GetCronJobDetails(CronJobId As Integer) As Task(Of DataTable)
Try
Dim oSQL As String = $"SELECT * FROM TBAPPSERV_CRON_DETAIL WHERE CRON_ID = {CronJobId}"
Dim oDatatable As DataTable = Await _MSSQL.GetDatatableAsync(oSQL)
Return oDatatable
Catch ex As Exception
Return Nothing
End Try
End Function
Public Async Sub [Stop]()
Await _Scheduler.Shutdown()
End Sub
Private Class LogProvider
Implements ILogProvider
Private _Logger As Modules.Logging.Logger
Public Sub New(Logger As DigitalData.Modules.Logging.Logger)
MyBase.New()
_Logger = Logger
End Sub
Public Function OpenNestedContext(message As String) As IDisposable Implements ILogProvider.OpenNestedContext
Throw New NotImplementedException()
End Function
Public Function OpenMappedContext(key As String, value As Object, Optional destructure As Boolean = False) As IDisposable Implements ILogProvider.OpenMappedContext
Throw New NotImplementedException()
End Function
Private Function GetLogger(name As String) As Logging.Logger Implements ILogProvider.GetLogger
Return Function(level, func, exception, parameters)
If exception IsNot Nothing Then
_Logger.Error(exception)
ElseIf level >= LogLevel.Debug AndAlso func IsNot Nothing Then
_Logger.Debug(func(), parameters)
ElseIf level >= LogLevel.Info AndAlso func IsNot Nothing Then
_Logger.Info(func(), parameters)
End If
Return True
End Function
End Function
End Class
End Class