ClientSuite: Add WorkerManager
This commit is contained in:
70
GUIs.ClientSuite/Workers/WorkerManager.vb
Normal file
70
GUIs.ClientSuite/Workers/WorkerManager.vb
Normal file
@@ -0,0 +1,70 @@
|
||||
Imports System.Timers
|
||||
Imports DigitalData.GUIs.ClientSuite.Base
|
||||
Imports DigitalData.Modules.Logging
|
||||
|
||||
Namespace Workers
|
||||
Public Class WorkerManager
|
||||
Inherits BaseClass
|
||||
|
||||
''' <summary>
|
||||
''' List of workers that will be started.
|
||||
''' </summary>
|
||||
Private WorkerTypes As New List(Of Type) From {
|
||||
GetType(HeartbeatWorker)
|
||||
}
|
||||
Private Workers As New List(Of IWorker)
|
||||
Private TimerList As New List(Of Timer)
|
||||
|
||||
Public Sub New(LogConfig As LogConfig, SyncronizingObject As frmMain)
|
||||
MyBase.New(LogConfig)
|
||||
|
||||
Workers = CreateWorkers()
|
||||
|
||||
For Each oWorker As IWorker In Workers
|
||||
Dim oTimer = New Timer(oWorker.Interval) With {
|
||||
.SynchronizingObject = SyncronizingObject,
|
||||
.Enabled = True
|
||||
}
|
||||
|
||||
AddHandler oTimer.Elapsed, Sub(sender As Object, e As ElapsedEventArgs)
|
||||
Try
|
||||
oWorker.Callback(Me, e)
|
||||
Catch ex As Exception
|
||||
Logger.Warn("Worker {0} threw an error in callback.", oWorker.GetType.Name)
|
||||
End Try
|
||||
End Sub
|
||||
|
||||
TimerList.Add(oTimer)
|
||||
Next
|
||||
End Sub
|
||||
|
||||
Public Function GetWorker(Of T)() As T
|
||||
For Each oWorker In Workers
|
||||
If TypeOf oWorker Is T Then
|
||||
Return oWorker
|
||||
End If
|
||||
Next
|
||||
|
||||
Return Nothing
|
||||
End Function
|
||||
|
||||
Private Function CreateWorkers() As List(Of IWorker)
|
||||
Dim oWorkers As New List(Of IWorker)
|
||||
|
||||
For Each oWorkerType In WorkerTypes
|
||||
Try
|
||||
Dim oWorker As IWorker = Activator.CreateInstance(oWorkerType, New Object() {LogConfig})
|
||||
oWorker.Setup()
|
||||
oWorkers.Add(oWorker)
|
||||
Catch ex As Exception
|
||||
Logger.Warn("Worker {0} threw an error while creating. Skipping.", oWorkerType.Name)
|
||||
Logger.Error(ex)
|
||||
Continue For
|
||||
End Try
|
||||
Next
|
||||
|
||||
Return oWorkers
|
||||
End Function
|
||||
End Class
|
||||
End Namespace
|
||||
|
||||
Reference in New Issue
Block a user