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
_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

View File

@ -8,7 +8,11 @@ Public Class JobConfigParser
Private Const ARGS_KEYVALUE_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
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

View File

@ -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
''' <summary>
''' Returns a datatable for a sql-statement
''' </summary>
''' <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>
''' <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
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
''' </summary>
''' <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>
''' <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
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
''' <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>
''' Executes the passed sql-statement in asyncmode
''' </summary>
@ -111,18 +134,20 @@ Public Class MSSQLServer
''' <param name="commandtimeout">Optional Timeout</param>
''' <remarks></remarks>
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
''' <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

View File

@ -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)

View File

@ -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

View File

@ -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