fix jobconfigparser, fix error in database->mssql, reduce logging for jobrunner

This commit is contained in:
Jonathan Jenne 2019-04-24 11:47:34 +02:00
parent 0bf0f8a05d
commit b387ff5376
6 changed files with 108 additions and 101 deletions

View File

@ -37,7 +37,7 @@ Public Class ADSyncJob
If oSyncedUsers Is Nothing Then If oSyncedUsers Is Nothing Then
_Logger.Warn("Group {0} could not be synced!", oGroup) _Logger.Warn("Group {0} could not be synced!", oGroup)
Else 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 End If
Next Next

View File

@ -8,7 +8,11 @@ Public Class JobConfigParser
Private Const ARGS_KEYVALUE_DELIMITER As String = "::" Private Const ARGS_KEYVALUE_DELIMITER As String = "::"
Private Const ARGS_LIST_DELIMITER As String = "|" Private Const ARGS_LIST_DELIMITER As String = "|"
''' <summary>
''' Parse a job config string. ex: True|* 0/3 * * * ?|Arg1=Foo
''' </summary>
''' <param name="ConfigString"></param>
''' <returns>A populated JobConfig object</returns>
Public Shared Function ParseConfig(ConfigString As String) As JobConfig Public Shared Function ParseConfig(ConfigString As String) As JobConfig
If JobOptionsRegex.IsMatch(ConfigString) Then If JobOptionsRegex.IsMatch(ConfigString) Then
Dim oMatches = JobOptionsRegex.Matches(ConfigString) Dim oMatches = JobOptionsRegex.Matches(ConfigString)
@ -19,11 +23,11 @@ Public Class JobConfigParser
If oSplitOptions.Length = 3 Then If oSplitOptions.Length = 3 Then
oOptions.Enabled = CBool(oSplitOptions(0)) oOptions.Enabled = CBool(oSplitOptions(0))
oOptions.CronExpression = oSplitOptions(1) oOptions.CronExpression = oSplitOptions(1)
oOptions.Arguments = New Dictionary(Of String, String) oOptions.Arguments = ParseOptionalArguments(oSplitOptions(2))
ElseIf oSplitOptions.Length = 2 Then ElseIf oSplitOptions.Length = 2 Then
oOptions.Enabled = CBool(oSplitOptions(0)) oOptions.Enabled = CBool(oSplitOptions(0))
oOptions.CronExpression = oSplitOptions(1) oOptions.CronExpression = oSplitOptions(1)
oOptions.Arguments = ParseOptionalArguments(oSplitOptions(2)) oOptions.Arguments = New Dictionary(Of String, String)
Else Else
Throw New ArgumentException("Config Malformed") Throw New ArgumentException("Config Malformed")
End If End If

View File

@ -4,73 +4,67 @@ Imports DigitalData.Modules.Logging
Public Class MSSQLServer Public Class MSSQLServer
Public DBInitialized As Boolean = False Public DBInitialized As Boolean = False
Public CurrentSQLConnectionString As String = "" Public CurrentSQLConnectionString As String = ""
Private CurrentSQLConnection As SqlConnection
Private _Logger As Logger Private _Logger As Logger
Public Sub New(LogConfig As LogConfig, ConnectionString As String) Public Sub New(LogConfig As LogConfig, ConnectionString As String)
_Logger = LogConfig.GetLogger() _Logger = LogConfig.GetLogger()
CurrentSQLConnectionString = ConnectionString
Try Try
Dim oSQLconnect As New SqlConnection DBInitialized = TestCanConnect()
oSQLconnect.ConnectionString = ConnectionString
oSQLconnect.Open()
oSQLconnect.Close()
CurrentSQLConnectionString = ConnectionString
DBInitialized = True
Catch ex As Exception Catch ex As Exception
DBInitialized = False DBInitialized = False
_Logger.Error(ex) _Logger.Error(ex)
End Try End Try
End Sub End Sub
Private Function GetSQLConnection() Private Function TestCanConnect() As Boolean
Try Try
If IsNothing(CurrentSQLConnection) Then Dim oConnection As New SqlConnection(CurrentSQLConnectionString)
Dim oSQLconnect As New SqlClient.SqlConnection oConnection.Open()
oSQLconnect.ConnectionString = CurrentSQLConnectionString oConnection.Close()
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
Return True Return True
Catch ex As Exception Catch ex As Exception
_Logger.Error(ex) _Logger.Error(ex)
Return False Return False
End Try End Try
End Function 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
''' <summary> ''' <summary>
''' Returns a datatable for a sql-statement ''' Returns a datatable for a sql-statement
''' </summary> ''' </summary>
''' <param name="sqlcommand">sqlcommand for datatable (select XYZ from TableORView)</param> ''' <param name="sqlcommand">sqlcommand for datatable (select XYZ from TableORView)</param>
''' <param name="commandtimeout">Optional Timeout</param> ''' <param name="Timeout">Optional Timeout</param>
''' <returns>Returns a datatable</returns> ''' <returns>Returns a datatable</returns>
''' <remarks></remarks> ''' <remarks></remarks>
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 Try
Dim dt As DataTable = New DataTable() If TestCanConnect() = False Then
If GetSQLConnection() = False Then
Return Nothing Return Nothing
End If End If
Dim oSQLCOmmand As SqlCommand Using oConnection = GetSQLConnection()
Using oSQLCOmmand = oConnection.CreateCommand()
oSQLCOmmand.CommandText = sqlcommand
oSQLCOmmand.CommandTimeout = Timeout
oSQLCOmmand = CurrentSQLConnection.CreateCommand() Dim dt As DataTable = New DataTable()
oSQLCOmmand.CommandText = sqlcommand Dim oAdapter As SqlDataAdapter = New SqlDataAdapter(oSQLCOmmand)
oSQLCOmmand.CommandTimeout = commandtimeout oAdapter.Fill(dt)
Dim adapter1 As SqlDataAdapter = New SqlDataAdapter(oSQLCOmmand) Return dt
adapter1.Fill(dt) End Using
End Using
Return dt
Catch ex As Exception Catch ex As Exception
_Logger.Error(ex) _Logger.Error(ex)
_Logger.Debug("sqlcommand: " & sqlcommand) _Logger.Debug("sqlcommand: " & sqlcommand)
@ -81,29 +75,58 @@ Public Class MSSQLServer
''' Executes the passed sql-statement ''' Executes the passed sql-statement
''' </summary> ''' </summary>
''' <param name="executeStatement">the sql statement</param> ''' <param name="executeStatement">the sql statement</param>
''' <param name="commandtimeout">Optional Timeout</param> ''' <param name="Timeout">Optional Timeout</param>
''' <returns>Returns true if properly executed, else false</returns> ''' <returns>Returns true if properly executed, else false</returns>
''' <remarks></remarks> ''' <remarks></remarks>
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 Try
If GetSQLConnection() = False Then If TestCanConnect() = False Then
Return Nothing Return Nothing
End If End If
'Dim oSQLconnect As New SqlClient.SqlConnection
Dim oSQLCOmmand As SqlCommand
oSQLCOmmand = CurrentSQLConnection.CreateCommand() Using oConnection = GetSQLConnection()
oSQLCOmmand.CommandText = executeStatement Using oSQLCOmmand = oConnection.CreateCommand()
oSQLCOmmand.CommandTimeout = commandtimeout oSQLCOmmand.CommandText = executeStatement
oSQLCOmmand.ExecuteNonQuery() oSQLCOmmand.CommandTimeout = Timeout
oSQLCOmmand.Dispose() oSQLCOmmand.ExecuteNonQuery()
Return True Return True
End Using
End Using
Catch ex As Exception Catch ex As Exception
_Logger.Error(ex) _Logger.Error(ex)
_Logger.Debug("executeStatement: " & executeStatement) _Logger.Debug("executeStatement: " & executeStatement)
Return False Return False
End Try End Try
End Function End Function
''' <summary>
''' Executes the passed sql-statement as Scalar
''' </summary>
''' <param name="ScalarSQL">the sql statement</param>
''' <param name="Timeout">Optional Timeout</param>
''' <returns>Returns true if properly executed, else false</returns>
''' <remarks></remarks>
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
''' <summary> ''' <summary>
''' Executes the passed sql-statement in asyncmode ''' Executes the passed sql-statement in asyncmode
''' </summary> ''' </summary>
@ -111,18 +134,20 @@ Public Class MSSQLServer
''' <param name="commandtimeout">Optional Timeout</param> ''' <param name="commandtimeout">Optional Timeout</param>
''' <remarks></remarks> ''' <remarks></remarks>
Public Sub NewExecuteNonQueryAsync(executeStatement As String, Optional commandtimeout As Integer = 120) Public Sub NewExecuteNonQueryAsync(executeStatement As String, Optional commandtimeout As Integer = 120)
If GetSQLConnection() = False Then If TestCanConnect() = False Then
Exit Sub Exit Sub
End If End If
Dim oSQLCOmmand As SqlCommand
Dim callback As New AsyncCallback(AddressOf NewExecuteNonQueryAsync_Callback)
Try Try
oSQLCOmmand = CurrentSQLConnection.CreateCommand() Dim oCallback As New AsyncCallback(AddressOf NewExecuteNonQueryAsync_Callback)
oSQLCOmmand.CommandText = executeStatement
oSQLCOmmand.CommandTimeout = commandtimeout Using oConnection As SqlConnection = GetSQLConnection()
oSQLCOmmand.BeginExecuteNonQuery(callback, oSQLCOmmand) Using oSQLCOmmand = oConnection.CreateCommand()
oSQLCOmmand.Dispose() oSQLCOmmand.CommandText = executeStatement
oSQLCOmmand.CommandTimeout = commandtimeout
oSQLCOmmand.BeginExecuteNonQuery(oCallback, oSQLCOmmand)
End Using
End Using
Catch ex As Exception Catch ex As Exception
_Logger.Error(ex) _Logger.Error(ex)
_Logger.Debug("executeStatement: " & executeStatement) _Logger.Debug("executeStatement: " & executeStatement)
@ -132,33 +157,6 @@ Public Class MSSQLServer
Private Sub NewExecuteNonQueryAsync_Callback(ByVal result As IAsyncResult) Private Sub NewExecuteNonQueryAsync_Callback(ByVal result As IAsyncResult)
Dim command As SqlCommand = CType(result.AsyncState, SqlCommand) Dim command As SqlCommand = CType(result.AsyncState, SqlCommand)
Dim res = command.EndExecuteNonQuery(result) 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 End Sub
''' <summary>
''' Executes the passed sql-statement as Scalar
''' </summary>
''' <param name="executeStatement">the sql statement</param>
''' <param name="commandtimeout">Optional Timeout</param>
''' <returns>Returns true if properly executed, else false</returns>
''' <remarks></remarks>
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 End Class

View File

@ -150,7 +150,7 @@ Public Class ActiveDirectoryInterface
Using oContext As New PrincipalContext(ContextType.Domain) Using oContext As New PrincipalContext(ContextType.Domain)
Using oGroupPrincipal As GroupPrincipal = GroupPrincipal.FindByIdentity(oContext, IdentityType.Name, GroupName) Using oGroupPrincipal As GroupPrincipal = GroupPrincipal.FindByIdentity(oContext, IdentityType.Name, GroupName)
If oGroupPrincipal Is Nothing Then If oGroupPrincipal Is Nothing Then
_logger.Warn("Group {0} does not exist.", GroupName) _logger.Debug("Group {0} does not exist.", GroupName)
Return oUsers Return oUsers
End If End If
@ -181,7 +181,7 @@ Public Class ActiveDirectoryInterface
End If End If
Next Next
Else Else
_logger.Warn("Could not fetch CustomAttributes for user {0}", oUser) _logger.Debug("Could not fetch CustomAttributes for user {0}", oUser)
End If End If
_logger.Debug("Trying to add User {0} to user list", oUser) _logger.Debug("Trying to add User {0} to user list", oUser)

View File

@ -24,7 +24,7 @@ Namespace SyncUsers
oGroupId = GetGroupId(GroupName) oGroupId = GetGroupId(GroupName)
If oGroupId = 0 Then 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 Return oSyncedUsers
End If End If
@ -46,7 +46,7 @@ Namespace SyncUsers
_logger.Debug("User {0} exists in database: {1}", oUser, oUserExists) _logger.Debug("User {0} exists in database: {1}", oUser, oUserExists)
Catch ex As Exception Catch ex As Exception
_logger.Error(ex) _logger.Error(ex)
_logger.Warn("Could not get UserId for user. Skipping") _logger.Warn("Could not get UserId for user. Skipping.")
Continue For Continue For
End Try End Try
@ -59,7 +59,7 @@ Namespace SyncUsers
End If End If
Catch ex As Exception Catch ex As Exception
_logger.Error(ex) _logger.Error(ex)
_logger.Warn("Could not create user. Skipping") _logger.Warn("Could not create user. Skipping.")
Continue For Continue For
End Try End Try
@ -68,7 +68,7 @@ Namespace SyncUsers
AddCustomAttributesToUser(oUser, oUserId) AddCustomAttributesToUser(oUser, oUserId)
Catch ex As Exception Catch ex As Exception
_logger.Error(ex) _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 End Try
' Add the user to group ' Add the user to group
@ -76,7 +76,7 @@ Namespace SyncUsers
AddUserToGroup(oUserId, oGroupId) AddUserToGroup(oUserId, oGroupId)
Catch ex As Exception Catch ex As Exception
_logger.Error(ex) _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 Continue For
End Try End Try
@ -106,7 +106,7 @@ Namespace SyncUsers
Dim oGroupId = _mssql.NewExecuteScalar(oSQL) Dim oGroupId = _mssql.NewExecuteScalar(oSQL)
If IsDBNull(oGroupId) OrElse oGroupId = 0 Then 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 Return 0
End If End If
@ -158,7 +158,7 @@ Namespace SyncUsers
Dim oResult = _mssql.NewExecutenonQuery(oSQL) Dim oResult = _mssql.NewExecutenonQuery(oSQL)
If oResult = False Then 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 Continue For
End If End If
Next Next

View File

@ -15,7 +15,7 @@ Public Class JobRunner
Private _Props As New NameValueCollection From { Private _Props As New NameValueCollection From {
{"quartz.serializer.type", "binary"}, {"quartz.serializer.type", "binary"},
{"quartz.threadPool.threadCount", 1} {"quartz.threadPool.threadCount", 10}
} }
Private _factory As StdSchedulerFactory Private _factory As StdSchedulerFactory
Private _scheduler As IScheduler Private _scheduler As IScheduler
@ -117,11 +117,16 @@ Public Class JobRunner
Public Function Execute(context As IJobExecutionContext) As Task Implements Quartz.IJob.Execute Public Function Execute(context As IJobExecutionContext) As Task Implements Quartz.IJob.Execute
Dim oJobData = context.MergedJobDataMap Dim oJobData = context.MergedJobDataMap
Dim oLogConfig As LogConfig = oJobData.Item("LogConfig") 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() 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) Return Task.FromResult(True)
End Function End Function