diff --git a/Jobs/EDMI/ADSync/ADSyncJob.vb b/Jobs/EDMI/ADSync/ADSyncJob.vb index 34020432..64d5be38 100644 --- a/Jobs/EDMI/ADSync/ADSyncJob.vb +++ b/Jobs/EDMI/ADSync/ADSyncJob.vb @@ -37,7 +37,7 @@ Public Class ADSyncJob If oSyncedUsers Is Nothing Then _Logger.Warn("Group {0} could not be synced!", oGroup) Else - _Logger.Debug("Synced {0} users for group {1}", oSyncedUsers.Count, oGroup) + _Logger.Info("Synced {0} users for group {1}", oSyncedUsers.Count, oGroup) End If Next diff --git a/Jobs/JobConfigParser.vb b/Jobs/JobConfigParser.vb index e1f9c0c3..3c6f702c 100644 --- a/Jobs/JobConfigParser.vb +++ b/Jobs/JobConfigParser.vb @@ -8,7 +8,11 @@ Public Class JobConfigParser Private Const ARGS_KEYVALUE_DELIMITER As String = "::" Private Const ARGS_LIST_DELIMITER As String = "|" - + ''' + ''' Parse a job config string. ex: True|* 0/3 * * * ?|Arg1=Foo + ''' + ''' + ''' A populated JobConfig object Public Shared Function ParseConfig(ConfigString As String) As JobConfig If JobOptionsRegex.IsMatch(ConfigString) Then Dim oMatches = JobOptionsRegex.Matches(ConfigString) @@ -19,11 +23,11 @@ Public Class JobConfigParser If oSplitOptions.Length = 3 Then oOptions.Enabled = CBool(oSplitOptions(0)) oOptions.CronExpression = oSplitOptions(1) - oOptions.Arguments = New Dictionary(Of String, String) + oOptions.Arguments = ParseOptionalArguments(oSplitOptions(2)) ElseIf oSplitOptions.Length = 2 Then oOptions.Enabled = CBool(oSplitOptions(0)) oOptions.CronExpression = oSplitOptions(1) - oOptions.Arguments = ParseOptionalArguments(oSplitOptions(2)) + oOptions.Arguments = New Dictionary(Of String, String) Else Throw New ArgumentException("Config Malformed") End If diff --git a/Modules.Database/MSSQLServer.vb b/Modules.Database/MSSQLServer.vb index cf1b1fbc..aff2870e 100644 --- a/Modules.Database/MSSQLServer.vb +++ b/Modules.Database/MSSQLServer.vb @@ -4,73 +4,67 @@ Imports DigitalData.Modules.Logging Public Class MSSQLServer Public DBInitialized As Boolean = False Public CurrentSQLConnectionString As String = "" - Private CurrentSQLConnection As SqlConnection Private _Logger As Logger Public Sub New(LogConfig As LogConfig, ConnectionString As String) _Logger = LogConfig.GetLogger() + CurrentSQLConnectionString = ConnectionString + Try - Dim oSQLconnect As New SqlConnection - oSQLconnect.ConnectionString = ConnectionString - oSQLconnect.Open() - oSQLconnect.Close() - CurrentSQLConnectionString = ConnectionString - DBInitialized = True + DBInitialized = TestCanConnect() Catch ex As Exception DBInitialized = False _Logger.Error(ex) End Try End Sub - Private Function GetSQLConnection() + Private Function TestCanConnect() As Boolean Try - If IsNothing(CurrentSQLConnection) Then - Dim oSQLconnect As New SqlClient.SqlConnection - oSQLconnect.ConnectionString = CurrentSQLConnectionString - CurrentSQLConnection = oSQLconnect - CurrentSQLConnection.Open() - Else - If CurrentSQLConnection.State <> ConnectionState.Open Then - _Logger.Warn($"Actual ConnectionState is: '{CurrentSQLConnection.State.ToString}'") - Try - CurrentSQLConnection.Open() - Return True - Catch ex As Exception - _Logger.Warn("Could not reconnect to database!") - Return False - End Try - End If - End If + Dim oConnection As New SqlConnection(CurrentSQLConnectionString) + oConnection.Open() + oConnection.Close() Return True Catch ex As Exception _Logger.Error(ex) Return False End Try End Function + + Private Function GetSQLConnection() As SqlConnection + Try + Dim oConnection As New SqlConnection(CurrentSQLConnectionString) + oConnection.Open() + Return oConnection + Catch ex As Exception + _Logger.Error(ex) + Return Nothing + End Try + End Function ''' ''' Returns a datatable for a sql-statement ''' ''' sqlcommand for datatable (select XYZ from TableORView) - ''' Optional Timeout + ''' Optional Timeout ''' Returns a datatable ''' - Public Function GetDatatable(sqlcommand As String, Optional commandtimeout As Integer = 120) As DataTable + Public Function GetDatatable(sqlcommand As String, Optional Timeout As Integer = 120) As DataTable Try - Dim dt As DataTable = New DataTable() - If GetSQLConnection() = False Then + If TestCanConnect() = False Then Return Nothing End If - Dim oSQLCOmmand As SqlCommand + Using oConnection = GetSQLConnection() + Using oSQLCOmmand = oConnection.CreateCommand() + oSQLCOmmand.CommandText = sqlcommand + oSQLCOmmand.CommandTimeout = Timeout - oSQLCOmmand = CurrentSQLConnection.CreateCommand() - oSQLCOmmand.CommandText = sqlcommand - oSQLCOmmand.CommandTimeout = commandtimeout - Dim adapter1 As SqlDataAdapter = New SqlDataAdapter(oSQLCOmmand) - adapter1.Fill(dt) - - Return dt + Dim dt As DataTable = New DataTable() + Dim oAdapter As SqlDataAdapter = New SqlDataAdapter(oSQLCOmmand) + oAdapter.Fill(dt) + Return dt + End Using + End Using Catch ex As Exception _Logger.Error(ex) _Logger.Debug("sqlcommand: " & sqlcommand) @@ -81,29 +75,58 @@ Public Class MSSQLServer ''' Executes the passed sql-statement ''' ''' the sql statement - ''' Optional Timeout + ''' Optional Timeout ''' Returns true if properly executed, else false ''' - Public Function NewExecutenonQuery(executeStatement As String, Optional commandtimeout As Integer = 120) As Boolean + Public Function NewExecutenonQuery(executeStatement As String, Optional Timeout As Integer = 120) As Boolean Try - If GetSQLConnection() = False Then + If TestCanConnect() = False Then Return Nothing End If - 'Dim oSQLconnect As New SqlClient.SqlConnection - Dim oSQLCOmmand As SqlCommand - oSQLCOmmand = CurrentSQLConnection.CreateCommand() - oSQLCOmmand.CommandText = executeStatement - oSQLCOmmand.CommandTimeout = commandtimeout - oSQLCOmmand.ExecuteNonQuery() - oSQLCOmmand.Dispose() - Return True + Using oConnection = GetSQLConnection() + Using oSQLCOmmand = oConnection.CreateCommand() + oSQLCOmmand.CommandText = executeStatement + oSQLCOmmand.CommandTimeout = Timeout + oSQLCOmmand.ExecuteNonQuery() + Return True + End Using + End Using Catch ex As Exception _Logger.Error(ex) _Logger.Debug("executeStatement: " & executeStatement) Return False End Try End Function + + ''' + ''' Executes the passed sql-statement as Scalar + ''' + ''' the sql statement + ''' Optional Timeout + ''' Returns true if properly executed, else false + ''' + Public Function NewExecuteScalar(ScalarSQL As String, Optional Timeout As Integer = 120) As Object + Try + If TestCanConnect() = False Then + Return Nothing + End If + + Using oConnection As SqlConnection = GetSQLConnection() + Using oSQLCOmmand = oConnection.CreateCommand() + oSQLCOmmand.CommandText = ScalarSQL + oSQLCOmmand.CommandTimeout = Timeout + Dim oResult As Object = oSQLCOmmand.ExecuteScalar() + Return oResult + End Using + End Using + Catch ex As Exception + _Logger.Error(ex) + _Logger.Debug("executeStatement: " & ScalarSQL) + Return Nothing + End Try + End Function + ''' ''' Executes the passed sql-statement in asyncmode ''' @@ -111,18 +134,20 @@ Public Class MSSQLServer ''' Optional Timeout ''' Public Sub NewExecuteNonQueryAsync(executeStatement As String, Optional commandtimeout As Integer = 120) - If GetSQLConnection() = False Then + If TestCanConnect() = False Then Exit Sub End If - Dim oSQLCOmmand As SqlCommand - Dim callback As New AsyncCallback(AddressOf NewExecuteNonQueryAsync_Callback) Try - oSQLCOmmand = CurrentSQLConnection.CreateCommand() - oSQLCOmmand.CommandText = executeStatement - oSQLCOmmand.CommandTimeout = commandtimeout - oSQLCOmmand.BeginExecuteNonQuery(callback, oSQLCOmmand) - oSQLCOmmand.Dispose() + Dim oCallback As New AsyncCallback(AddressOf NewExecuteNonQueryAsync_Callback) + + Using oConnection As SqlConnection = GetSQLConnection() + Using oSQLCOmmand = oConnection.CreateCommand() + oSQLCOmmand.CommandText = executeStatement + oSQLCOmmand.CommandTimeout = commandtimeout + oSQLCOmmand.BeginExecuteNonQuery(oCallback, oSQLCOmmand) + End Using + End Using Catch ex As Exception _Logger.Error(ex) _Logger.Debug("executeStatement: " & executeStatement) @@ -132,33 +157,6 @@ Public Class MSSQLServer Private Sub NewExecuteNonQueryAsync_Callback(ByVal result As IAsyncResult) Dim command As SqlCommand = CType(result.AsyncState, SqlCommand) Dim res = command.EndExecuteNonQuery(result) - _Logger.Info(String.Format("Finished executing Async database operation: {0}", command.CommandText)) + _Logger.Info("Finished executing Async database operation: {0}", command.CommandText) End Sub - ''' - ''' Executes the passed sql-statement as Scalar - ''' - ''' the sql statement - ''' Optional Timeout - ''' Returns true if properly executed, else false - ''' - Public Function NewExecuteScalar(executeStatement As String, Optional commandtimeout As Integer = 120) - Dim result - Try - If GetSQLConnection() = False Then - Return Nothing - End If - - Dim oSQLCOmmand As SqlClient.SqlCommand - oSQLCOmmand = CurrentSQLConnection.CreateCommand() - oSQLCOmmand.CommandText = executeStatement - oSQLCOmmand.CommandTimeout = commandtimeout - result = oSQLCOmmand.ExecuteScalar() - oSQLCOmmand.Dispose() - Return result - Catch ex As Exception - _Logger.Error(ex) - _Logger.Debug("executeStatement: " & executeStatement) - Return Nothing - End Try - End Function End Class diff --git a/Modules.Interfaces/ActiveDirectoryInterface.vb b/Modules.Interfaces/ActiveDirectoryInterface.vb index ac057edb..56ddbb10 100644 --- a/Modules.Interfaces/ActiveDirectoryInterface.vb +++ b/Modules.Interfaces/ActiveDirectoryInterface.vb @@ -150,7 +150,7 @@ Public Class ActiveDirectoryInterface Using oContext As New PrincipalContext(ContextType.Domain) Using oGroupPrincipal As GroupPrincipal = GroupPrincipal.FindByIdentity(oContext, IdentityType.Name, GroupName) If oGroupPrincipal Is Nothing Then - _logger.Warn("Group {0} does not exist.", GroupName) + _logger.Debug("Group {0} does not exist.", GroupName) Return oUsers End If @@ -181,7 +181,7 @@ Public Class ActiveDirectoryInterface End If Next Else - _logger.Warn("Could not fetch CustomAttributes for user {0}", oUser) + _logger.Debug("Could not fetch CustomAttributes for user {0}", oUser) End If _logger.Debug("Trying to add User {0} to user list", oUser) diff --git a/Modules.Interfaces/ActiveDirectoryInterface/SyncUsers.MSSQL.vb b/Modules.Interfaces/ActiveDirectoryInterface/SyncUsers.MSSQL.vb index 15e75b18..7448a9b2 100644 --- a/Modules.Interfaces/ActiveDirectoryInterface/SyncUsers.MSSQL.vb +++ b/Modules.Interfaces/ActiveDirectoryInterface/SyncUsers.MSSQL.vb @@ -24,7 +24,7 @@ Namespace SyncUsers oGroupId = GetGroupId(GroupName) If oGroupId = 0 Then - _logger.Warn("Group {0} does not exist in database. Exiting", GroupName) + _logger.Debug("Group {0} does not exist in database. Exiting.", GroupName) Return oSyncedUsers End If @@ -46,7 +46,7 @@ Namespace SyncUsers _logger.Debug("User {0} exists in database: {1}", oUser, oUserExists) Catch ex As Exception _logger.Error(ex) - _logger.Warn("Could not get UserId for user. Skipping") + _logger.Warn("Could not get UserId for user. Skipping.") Continue For End Try @@ -59,7 +59,7 @@ Namespace SyncUsers End If Catch ex As Exception _logger.Error(ex) - _logger.Warn("Could not create user. Skipping") + _logger.Warn("Could not create user. Skipping.") Continue For End Try @@ -68,7 +68,7 @@ Namespace SyncUsers AddCustomAttributesToUser(oUser, oUserId) Catch ex As Exception _logger.Error(ex) - _logger.Warn("Could not add custom attributes to user {0}. Continuing", oUser) + _logger.Debug("Could not add custom attributes to user {0}. Continuing.", oUser) End Try ' Add the user to group @@ -76,7 +76,7 @@ Namespace SyncUsers AddUserToGroup(oUserId, oGroupId) Catch ex As Exception _logger.Error(ex) - _logger.Warn("Could not add user {0} to group {1}. Skipping", oUser, GroupName) + _logger.Warn("Could not add user {0} to group {1}. Skipping.", oUser, GroupName) Continue For End Try @@ -106,7 +106,7 @@ Namespace SyncUsers Dim oGroupId = _mssql.NewExecuteScalar(oSQL) If IsDBNull(oGroupId) OrElse oGroupId = 0 Then - _logger.Debug("Group {0} not found in database", GroupName) + _logger.Debug("Group {0} not found in database.", GroupName) Return 0 End If @@ -158,7 +158,7 @@ Namespace SyncUsers Dim oResult = _mssql.NewExecutenonQuery(oSQL) If oResult = False Then - _logger.Warn("Custom Attribute {0} could not be added to user {1}", oAttribute.Name, User.samAccountName) + _logger.Debug("Custom Attribute {0} could not be added to user {1}", oAttribute.Name, User.samAccountName) Continue For End If Next diff --git a/Service.JobRunner/JobRunner.vb b/Service.JobRunner/JobRunner.vb index e11eeecb..aa378bb4 100644 --- a/Service.JobRunner/JobRunner.vb +++ b/Service.JobRunner/JobRunner.vb @@ -15,7 +15,7 @@ Public Class JobRunner Private _Props As New NameValueCollection From { {"quartz.serializer.type", "binary"}, - {"quartz.threadPool.threadCount", 1} + {"quartz.threadPool.threadCount", 10} } Private _factory As StdSchedulerFactory Private _scheduler As IScheduler @@ -117,11 +117,16 @@ Public Class JobRunner Public Function Execute(context As IJobExecutionContext) As Task Implements Quartz.IJob.Execute Dim oJobData = context.MergedJobDataMap Dim oLogConfig As LogConfig = oJobData.Item("LogConfig") - Dim oArgs As Dictionary(Of String, String) = oJobData.Item("Args") - Dim oArg1 As String = oArgs.Item("Arg1") Dim oLogger = oLogConfig.GetLogger() - oLogger.Info("Running Test Job With Arg1: {0}", oArg1) + Dim oArgs As Dictionary(Of String, String) = oJobData.Item("Args") + + If oArgs.ContainsKey("Arg1") Then + Dim oArg1 As String = oArgs.Item("Arg1") + oLogger.Info("Running Test Job With Arg1: {0}", oArg1) + Else + oLogger.Warn("Running Test Job With missing Arg1 :/") + End If Return Task.FromResult(True) End Function