From 2855cc6208ddf87f49e6f726e7cb7f3d0d8f04c1 Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Wed, 10 Apr 2019 11:43:15 +0200 Subject: [PATCH] work on jobrunner --- JobRunner/App.config | 9 +++-- JobRunner/JobRunner.vb | 13 +++++-- JobRunner/JobRunner.vbproj | 7 ---- JobRunner/My Project/Settings.Designer.vb | 27 ++++++++++----- JobRunner/My Project/Settings.settings | 9 +++-- JobRunner/WindowsService.vb | 1 + Jobs/EDMI/ADSync/ADSyncArgs.vb | 2 ++ Jobs/EDMI/ADSync/ADSyncJob.vb | 25 +++++++++++++- Jobs/JobArgs.vb | 4 +++ Jobs/JobBase.vb | 14 ++++++++ Jobs/{IJob.vb => JobInterface.vb} | 2 ++ Jobs/Jobs.vbproj | 4 ++- Modules.Database/Firebird.vb | 6 ++-- .../ActiveDirectoryInterface.vb | 34 +++++-------------- 14 files changed, 103 insertions(+), 54 deletions(-) create mode 100644 Jobs/JobArgs.vb create mode 100644 Jobs/JobBase.vb rename Jobs/{IJob.vb => JobInterface.vb} (73%) diff --git a/JobRunner/App.config b/JobRunner/App.config index f6c972d9..584c1495 100644 --- a/JobRunner/App.config +++ b/JobRunner/App.config @@ -19,9 +19,6 @@ False - - 1,2,3,4,5,6,7,8,9,10 - @@ -37,6 +34,12 @@ 120 + + 120 + + + + \ No newline at end of file diff --git a/JobRunner/JobRunner.vb b/JobRunner/JobRunner.vb index e6c7a376..0b079680 100644 --- a/JobRunner/JobRunner.vb +++ b/JobRunner/JobRunner.vb @@ -60,8 +60,17 @@ Public Class JobRunner _logger.Debug("Background worker running..") ' TODO: WORK - 'Dim job As New ImportZUGFeRDFiles(_logConfig, _firebird) - 'job.Start(args) + + Dim oJob As New ADSyncJob(_logConfig, _firebird) + Dim oArgs As New ADSyncArgs() With { + .Enabled = My.Settings.JOB_ADSYNC_ENABLED, + .Interval = My.Settings.JOB_ADSYNC_INTERVAL, + .RootPath = My.Settings.JOB_ADSYNC_ROOT_PATH + } + If oJob.ShouldStart(oArgs) Then + oJob.Start(oArgs) + End If + Catch ex As Exception _logger.Warn("Background worker failed!") _logger.Error(ex) diff --git a/JobRunner/JobRunner.vbproj b/JobRunner/JobRunner.vbproj index 6a0e0ea7..37a3ec1f 100644 --- a/JobRunner/JobRunner.vbproj +++ b/JobRunner/JobRunner.vbproj @@ -47,13 +47,6 @@ On - - - - - - ..\LookupGrid\obj\Debug\DigitalData.Controls.LookupGrid.dll - ..\packages\NLog.4.5.11\lib\net45\NLog.dll diff --git a/JobRunner/My Project/Settings.Designer.vb b/JobRunner/My Project/Settings.Designer.vb index 501da1af..a28f946b 100644 --- a/JobRunner/My Project/Settings.Designer.vb +++ b/JobRunner/My Project/Settings.Designer.vb @@ -81,15 +81,6 @@ Namespace My End Get End Property - _ - Public ReadOnly Property JOB_ADSYNC_INTERVAL() As String - Get - Return CType(Me("JOB_ADSYNC_INTERVAL"),String) - End Get - End Property - _ @@ -134,6 +125,24 @@ Namespace My Return CType(Me("JOB_INTERVAL"),Long) End Get End Property + + _ + Public ReadOnly Property JOB_ADSYNC_INTERVAL() As Long + Get + Return CType(Me("JOB_ADSYNC_INTERVAL"),Long) + End Get + End Property + + _ + Public ReadOnly Property JOB_ADSYNC_ROOT_PATH() As String + Get + Return CType(Me("JOB_ADSYNC_ROOT_PATH"),String) + End Get + End Property End Class End Namespace diff --git a/JobRunner/My Project/Settings.settings b/JobRunner/My Project/Settings.settings index ccd102fc..3281b731 100644 --- a/JobRunner/My Project/Settings.settings +++ b/JobRunner/My Project/Settings.settings @@ -11,9 +11,6 @@ False - - 1,2,3,4,5,6,7,8,9,10 - @@ -29,5 +26,11 @@ 120 + + 120 + + + + \ No newline at end of file diff --git a/JobRunner/WindowsService.vb b/JobRunner/WindowsService.vb index 166892c5..cfca9dfa 100644 --- a/JobRunner/WindowsService.vb +++ b/JobRunner/WindowsService.vb @@ -33,6 +33,7 @@ Public Class WindowsService End Sub Protected Overrides Sub OnStop() + _jobRunner.Stop() _logger.Info($"{My.Settings.SERVICE_NAME} is stopping.") End Sub End Class diff --git a/Jobs/EDMI/ADSync/ADSyncArgs.vb b/Jobs/EDMI/ADSync/ADSyncArgs.vb index 27845e36..1e64916b 100644 --- a/Jobs/EDMI/ADSync/ADSyncArgs.vb +++ b/Jobs/EDMI/ADSync/ADSyncArgs.vb @@ -1,3 +1,5 @@ Public Class ADSyncArgs + Inherits JobArgs + Public RootPath As String End Class diff --git a/Jobs/EDMI/ADSync/ADSyncJob.vb b/Jobs/EDMI/ADSync/ADSyncJob.vb index fba9ad50..b0fc49a3 100644 --- a/Jobs/EDMI/ADSync/ADSyncJob.vb +++ b/Jobs/EDMI/ADSync/ADSyncJob.vb @@ -1,9 +1,32 @@ -Imports DigitalData.Modules.Jobs +Imports DigitalData.Modules.Database +Imports DigitalData.Modules.Interfaces +Imports DigitalData.Modules.Jobs +Imports DigitalData.Modules.Logging Public Class ADSyncJob + Inherits JobBase Implements IJob(Of ADSyncArgs) + Private _ADSync As ActiveDirectoryInterface + + Public Sub New(LogConfig As LogConfig, Firebird As Firebird) + MyBase.New(LogConfig, Firebird) + End Sub + Public Sub Start(Arguments As ADSyncArgs) Implements IJob(Of ADSyncArgs).Start + _ADSync = New ActiveDirectoryInterface(_LogConfig, _Firebird, Arguments.RootPath) + + 'TODO: Do AD Sync! + + If _ADSync.Authenticate() = False Then + _Logger.Warn("Job could not be completed! Authentication failed!") + Exit Sub + End If + End Sub + + Public Function ShouldStart(Arguments As ADSyncArgs) As Boolean Implements IJob(Of ADSyncArgs).ShouldStart + Return Arguments.Enabled + End Function End Class diff --git a/Jobs/JobArgs.vb b/Jobs/JobArgs.vb new file mode 100644 index 00000000..43156ec6 --- /dev/null +++ b/Jobs/JobArgs.vb @@ -0,0 +1,4 @@ +Public Class JobArgs + Public Enabled As Boolean + Public Interval As Long +End Class diff --git a/Jobs/JobBase.vb b/Jobs/JobBase.vb new file mode 100644 index 00000000..03a77ca6 --- /dev/null +++ b/Jobs/JobBase.vb @@ -0,0 +1,14 @@ +Imports DigitalData.Modules.Database +Imports DigitalData.Modules.Logging + +Public Class JobBase + Protected _LogConfig As LogConfig + Protected _Logger As Logger + Protected _Firebird As Firebird + + Public Sub New(LogConfig As LogConfig, Firebird As Firebird) + _LogConfig = LogConfig + _Logger = LogConfig.GetLogger() + _Firebird = Firebird + End Sub +End Class diff --git a/Jobs/IJob.vb b/Jobs/JobInterface.vb similarity index 73% rename from Jobs/IJob.vb rename to Jobs/JobInterface.vb index 6cd3f6dc..edd8a83a 100644 --- a/Jobs/IJob.vb +++ b/Jobs/JobInterface.vb @@ -4,4 +4,6 @@ End Interface Public Interface IJob(Of T) Sub Start(Arguments As T) + + Function ShouldStart(Arguments As T) As Boolean End Interface diff --git a/Jobs/Jobs.vbproj b/Jobs/Jobs.vbproj index fc678fc3..2ac0fe84 100644 --- a/Jobs/Jobs.vbproj +++ b/Jobs/Jobs.vbproj @@ -90,7 +90,9 @@ - + + + True diff --git a/Modules.Database/Firebird.vb b/Modules.Database/Firebird.vb index 0d9ffffe..75ddb44d 100644 --- a/Modules.Database/Firebird.vb +++ b/Modules.Database/Firebird.vb @@ -147,7 +147,7 @@ Public Class Firebird }.ToString() End Function - Private Function MaybeGetTransaction(Connection As FbConnection, Mode As TransactionMode, Transaction As FbTransaction) + Private Function MaybeGetTransaction(Connection As FbConnection, Mode As TransactionMode, Transaction As FbTransaction) As FbTransaction If Mode = TransactionMode.NoTransaction Then Return Nothing ElseIf Mode = TransactionMode.ExternalTransaction Then @@ -157,7 +157,7 @@ Public Class Firebird End If End Function - Private Function MaybeCommitTransaction(Transaction As FbTransaction, TransactionMode As TransactionMode) + Private Function MaybeCommitTransaction(Transaction As FbTransaction, TransactionMode As TransactionMode) As Boolean Select Case TransactionMode Case TransactionMode.NoTransaction Return True @@ -168,7 +168,7 @@ Public Class Firebird Transaction.Commit() Return True Catch ex As Exception - _logger.Error(ex) + _Logger.Error(ex) Return False End Try Case Else diff --git a/Modules.Interfaces/ActiveDirectoryInterface.vb b/Modules.Interfaces/ActiveDirectoryInterface.vb index b9549f4a..a9c40555 100644 --- a/Modules.Interfaces/ActiveDirectoryInterface.vb +++ b/Modules.Interfaces/ActiveDirectoryInterface.vb @@ -112,29 +112,32 @@ Public Class ActiveDirectoryInterface Return oSyncedUsers End Function - - Public Sub Authenticate() + Public Function Authenticate() As Boolean Try Dim oEntry = GetRootNode() oEntry.RefreshCache() _rootNode = oEntry + Return True Catch ex As Exception _logger.Error(ex) _logger.Warn("Could not authenticate with Active Directory.") + Return False End Try - End Sub - Public Sub Authenticate(Username As String, Password As String) + End Function + Public Function Authenticate(Username As String, Password As String) As Boolean Try Dim oEntry = GetRootNode(Username, Password) oEntry.RefreshCache() _rootNode = oEntry + Return True Catch ex As Exception _logger.Error(ex) _logger.Warn("Could not authenticate with Active Directory.") + Return False End Try - End Sub + End Function Public Function ListGroups(Optional Query As String = "(&(objectClass=group) (samAccountName=*))") As List(Of ADGroup) Return ListGroups(_rootNode, Query) @@ -196,27 +199,8 @@ Public Class ActiveDirectoryInterface Throw ex End Try End Function - Public Function ListUsers(GroupNames As List(Of String)) As List(Of ADUser) - Try - Dim oUsers As New List(Of ADUser) - Dim oComparer As New UserEqualityComparer() - - For Each oGroup In GroupNames - Dim oGroupUsers = ListUsers(oGroup) - Dim oNewUsers = oGroupUsers. - Except(oUsers, oComparer). - ToList() - oUsers.AddRange(oNewUsers) - Next - - Return oUsers - Catch ex As Exception - _logger.Error(ex) - Throw ex - End Try - End Function - Public Function GetGroupId(GroupName As String) As Integer + Private Function GetGroupId(GroupName As String) As Integer Try Dim oSQL As String = $"SELECT FNICM_GET_RECORD4SYSKEY('{GroupName}','002-NAME') from RDB$DATABASE" Dim oGroupId = _firebird.GetScalarValue(oSQL)