I have a list of tasks I get from my database (as strings) and then execute. However I'm uncertain on how to implement this correctly.
Currently I have implemented this in the following way:
Private Sub Monitoring(installationCode As String, drive As String, syncFolder As String)
InitializeDatabaseConnection(installationCode)
TraceInitialize(drive, "Monitoring")
Trace.TraceInformation("Monitoring started for installation: " & installationCode)
For Each param In JdnParamDAO.GetJdnParams(General.AmosRemoteTask)
If param.Value Then
RemoteTask.Deptid = Department.GetDepartmentsNotInOne().Where(Function(x) x.Code = "001").FirstOrDefault.DepartmentID
Dim monitorTasks = TaskDefinitionDAO.GetActiveTaskDefinitionsByCategory(TaskDefinitionCategoryEnum.Monitor)
Trace.TraceInformation(monitorTasks.Count() & " have been found.")
'Perform each monitor task and save the results to the database
For Each monitorTask In monitorTasks
Try
Trace.TraceInformation("MonitorTask: " & monitorTask.Description.ToLowerInvariant() & " found.")
Dim result = String.Empty
Select Case monitorTask.Description.ToLowerInvariant()
Case "getlastbackup"
result = RemoteTaskHelper.GetLastBackup(drive, installationCode)
Case "getamosrtversion"
result = General.AmosRTVersion
Case "getstockondummypercentage"
result = RemoteTaskHelper.GetStockOnDummyPercentage()
Case "getnumberofhistoryrecords"
result = RemoteTaskHelper.GetNumerOfHistoryRecords()
Case "getnumberofsparepartlogrecords"
result = RemoteTaskHelper.GetNumberOfSparePartLogRecords()
Case "getinconsistenciesbetweencompjobnextdueandlastdone"
result = RemoteTaskHelper.GetInconsistenciesBetweenCompJobNextDueAndLastDone()
Case "getdifferencesofcompjobnextdueandworkorderdue"
result = RemoteTaskHelper.GetDifferencesOfCompJobNextDueAndWorkOrderDue()
Case "getinconsistenciesbetweencompjobsprioandworkordersprio"
result = RemoteTaskHelper.GetWorkOrderCompJobPriority()
Case "getactivecompjobswithnextdue"
result = RemoteTaskHelper.GetActiveCompJobsWithNextDue()
Case "getactivecompjobswithoutnextdue"
result = RemoteTaskHelper.GetActiveCompJobsWithoutNextDue()
Case "getopenworkorders"
result = RemoteTaskHelper.GetOpenWorkOrders()
Case "getamosguiversion"
result = RemoteTaskHelper.GetAmosGuiVersion(syncFolder)
Case "checkaddinsactivation"
result = RemoteTaskHelper.CheckAddinsActivation()
Case "checkamosmobileactivation"
result = RemoteTaskHelper.CheckAmosMobileActivation()
Case "checkchangelogactivation"
result = RemoteTaskHelper.CheckChangelogActivation()
Case "retrievejdnmodules"
result = RemoteTaskHelper.RetrieveJdnModules(installationCode, syncFolder)
Case "retrieveamosparameters"
result = RemoteTaskHelper.RetrieveAmosParameters(installationCode, syncFolder)
Case "retrievestocksituation"
result = RemoteTaskHelper.RetrieveStockSituation(installationCode, syncFolder)
Case "executesql"
result = RemoteTaskHelper.ExecuteSQL(drive, installationCode)
Case "getfreespace"
result = RemoteTaskHelper.GetFreeSpace(drive)
Case Else
Trace.TraceInformation("Non excisting monitoring task: " + monitorTask.Description)
ExceptionMonitor.LogExceptionEvent(
"Non excisting monitoring task: " + monitorTask.Description, ProblemSeverity.Error)
End Select
If Not String.IsNullOrEmpty(result) Then
Dim taskLog = New RemoteTaskLog()
taskLog.CreationDate = DateTime.Now()
taskLog.TaskDefinition = monitorTask
taskLog.SourceInstallation = Int32.Parse(installationCode)
taskLog.DestinationInstallation = CLng(General.AmosRTHeadOffice)
taskLog.AppVersion = General.AmosRTVersion
taskLog.TaskResult = result.ToString()
RemoteTaskLogDAO.Save(taskLog)
Else
Trace.TraceInformation("No result returned for monitoring task: " + monitorTask.Description)
ExceptionMonitor.LogExceptionEvent(
"No result returned for monitoring task: " + monitorTask.Description, ProblemSeverity.Error)
End If
Catch ex As Exception
Trace.TraceInformation("Error " + ex.ToString())
ExceptionMonitor.LogExceptionEvent(ex, ProblemSeverity.Error)
End Try
Next
End If
Next
End Sub
I am certain there is a better way to do this. I did some reading and always end up with the Strategy pattern (like asked here) but I don't seem to understand how to implement it in this situation.
Am I going in the correct direction here or not at all?