#Region "Includes" Imports System.Data.SqlClient Imports Common.Common #End Region Public Class Job Public Shared Sub SendNotification(ByVal common As Common.Common, ByVal programmId As Integer, ByVal ds As DataSet, ByVal errorMessage As String, ByVal jobId As Integer, ByVal jobType As JobType) Try 'get all notifications to a programm Dim subject As String, message As String = "", receiver As String Dim dr As DataRow If ds.Tables.Count > 0 Then For Each dr In ds.Tables(0).Rows subject = dr("Titel").ToString().Replace("##PROG_NAME##", dr("ProgrammName")) message = dr("Meldung").ToString() If (dr("IsMitFehlerText")) Then message += Environment.NewLine & Environment.NewLine & "Fehlermeldung:" & Environment.NewLine & errorMessage End If message = message.Replace("##PROG_NAME##", dr("ProgrammName")) message += Environment.NewLine & Environment.NewLine receiver = dr("eMail") Select Case CInt(dr("BenachrichtigungTypId")) Case NotificationType.File SendToFile(dr("NotiParamValue"), subject, message) Case NotificationType.Mail common.SendMail(receiver, subject, message) Case Else Throw New Exception("Der Benachrichtigungs-Typ " & dr("BenachrichtigungTypId") & " ist unbekannt") End Select Next 'write log common.Log(common.SERVICE_DISPLAY_NAME, "Sending Notification (ProgrammId: " + programmId.ToString() + ", JobId: " + jobId.ToString() + ", Message: " + message + ") on " + DateTime.Now.ToString("G"), EventLogEntryType.Information) DataAccess.Job.SetNotificationCounter(common.DSN, jobId, jobType, False) End If Catch ex As Exception Throw ex End Try End Sub Public Shared Sub SendToFile(ByVal filePath As String, ByVal subject As String, ByVal message As String) Try Dim fs As New System.IO.FileStream(filePath, IO.FileMode.Append, IO.FileAccess.Write) Dim writer As New System.IO.StreamWriter(fs) writer.BaseStream.Seek(0, IO.SeekOrigin.End) writer.WriteLine("Datum: " & DateTime.Now) writer.WriteLine("Betreff: " & subject) writer.WriteLine("Meldung: " & message) writer.WriteLine("-------------------------------------------") writer.Close() Catch ex As Exception Throw ex End Try End Sub Public Shared Sub SetNextExecDateTime(ByVal dsn As String, ByVal jobId As Integer) Dim sqlConn As New SqlConnection(dsn) Try Dim sqlCom As New SqlCommand sqlConn.Open() sqlCom.CommandType = CommandType.StoredProcedure sqlCom.Connection = sqlConn sqlCom.CommandText = "SetNextStartDate" sqlCom.Parameters.Add(New SqlParameter("@JobId", jobId)) sqlCom.ExecuteNonQuery() sqlConn.Close() SetNextStartDateCalculated(dsn, jobId, False) Catch ex As Exception If sqlConn.State = ConnectionState.Open Then sqlConn.Close() End If Throw ex End Try End Sub 'Sets, if a NextStartDate is calculated or not Public Shared Sub SetNextStartDateCalculated(ByVal dsn As String, ByVal jobId As Integer, ByVal resetFlag As Boolean) Dim sqlConn As New SqlConnection(dsn) Try Dim sqlCom As New SqlCommand sqlConn.Open() sqlCom.CommandType = CommandType.StoredProcedure sqlCom.Connection = sqlConn sqlCom.CommandText = "SetNextStartDateCalculated" sqlCom.Parameters.Add(New SqlParameter("@JobId", jobId)) sqlCom.Parameters.Add(New SqlParameter("@ResetFlag", resetFlag)) sqlCom.ExecuteNonQuery() sqlConn.Close() Catch ex As Exception If sqlConn.State = ConnectionState.Open Then sqlConn.Close() End If Throw ex End Try End Sub 'sets the counter state if an error occured Public Shared Sub SetNotificationCounter(ByVal dsn As String, ByVal jobId As Integer, ByVal jobTypId As JobType, ByVal resetCounter As Boolean) Dim sqlConn As New SqlConnection(dsn) Try Dim sqlCom As New SqlCommand sqlConn.Open() sqlCom.CommandType = CommandType.StoredProcedure sqlCom.Connection = sqlConn sqlCom.CommandText = "SetNotificationCounter" sqlCom.Parameters.Add(New SqlParameter("@JobId", jobId)) sqlCom.Parameters.Add(New SqlParameter("@JobTypId", CInt(jobTypId))) sqlCom.Parameters.Add(New SqlParameter("@Reset", resetCounter)) sqlCom.ExecuteNonQuery() sqlConn.Close() Catch ex As Exception If sqlConn.State = ConnectionState.Open Then sqlConn.Close() End If Throw ex End Try End Sub Public Shared Sub GetNotifications(ByVal common As Common.Common, ByVal programmId As Integer, ByRef notifications As DataSet) Dim sqlConn As New SqlConnection(common.DSN) Try Dim sqlCom As New SqlCommand Dim da As New SqlDataAdapter Dim ds As New DataSet sqlConn.Open() sqlCom.CommandType = CommandType.StoredProcedure sqlCom.Connection = sqlConn sqlCom.CommandText = "GetNotifications" sqlCom.Parameters.Add(New SqlParameter("@ProgrammId", programmId)) da.SelectCommand = sqlCom da.Fill(notifications) sqlConn.Close() Catch ex As Exception If sqlConn.State = ConnectionState.Open Then sqlConn.Close() End If Throw ex End Try End Sub ' gets all jobs, that taked to long to execute... (Jobs.MaxLaufzeit) Public Shared Sub GetFailedExecJobs(ByVal common As Common.Common, ByVal programmId As Integer, ByRef failedExecjobs As DataSet) Dim sqlConn As New SqlConnection(common.DSN) Try Dim sqlCom As New SqlCommand Dim da As New SqlDataAdapter Dim ds As New DataSet sqlConn.Open() sqlCom.CommandType = CommandType.StoredProcedure sqlCom.Connection = sqlConn sqlCom.CommandText = "GetFailedExecutionJobs" sqlCom.Parameters.Add(New SqlParameter("@ProgrammId", programmId)) da.SelectCommand = sqlCom da.Fill(failedExecjobs) sqlConn.Close() Catch ex As Exception If sqlConn.State = ConnectionState.Open Then sqlConn.Close() End If Throw ex End Try End Sub 'Gets all bad start jobs of a program Public Shared Sub GetFailedStartJobs(ByVal common As Common.Common, ByVal programmId As Integer, ByRef failedStartjobs As DataSet) Dim sqlConn As New SqlConnection(common.DSN) Try Dim sqlCom As New SqlCommand Dim da As New SqlDataAdapter Dim ds As New DataSet sqlConn.Open() sqlCom.CommandType = CommandType.StoredProcedure sqlCom.Connection = sqlConn sqlCom.CommandText = "GetFailedStartJobs" sqlCom.Parameters.Add(New SqlParameter("@ProgrammId", programmId)) sqlCom.Parameters.Add(New SqlParameter("@MaxStartDuration", common.MaximalStartDuration)) da.SelectCommand = sqlCom da.Fill(failedStartjobs) sqlConn.Close() Catch ex As Exception If sqlConn.State = ConnectionState.Open Then sqlConn.Close() End If Throw ex End Try End Sub 'sets start or end datetime of a run-job Public Shared Sub SetJobLastRun(ByVal dsn As String, ByVal jobId As Integer, ByVal jobLastRun As LastRun) Dim sqlConn As New SqlConnection(dsn) Try Dim sqlCom As New SqlCommand sqlConn.Open() sqlCom.CommandType = CommandType.StoredProcedure sqlCom.Connection = sqlConn sqlCom.CommandText = "SetJobLastRun" sqlCom.Parameters.Add(New SqlParameter("@JobId", jobId)) sqlCom.Parameters.Add(New SqlParameter("@LastRunType", CInt(jobLastRun))) sqlCom.ExecuteNonQuery() sqlConn.Close() Catch If sqlConn.State = ConnectionState.Open Then sqlConn.Close() End If Throw End Try End Sub 'Sets if currently job is running in db Public Shared Sub SetIsRunning(ByVal dsn As String, ByVal programmId As Integer, ByVal isRunning As Boolean) Dim sqlConn As New SqlConnection(dsn) Try Dim sqlCom As New SqlCommand sqlConn.Open() sqlCom.CommandType = CommandType.StoredProcedure sqlCom.Connection = sqlConn sqlCom.CommandText = "SetJobIsRunning" sqlCom.Parameters.Add(New SqlParameter("@ProgrammId", programmId)) sqlCom.Parameters.Add(New SqlParameter("@IsRunning", isRunning)) sqlCom.ExecuteNonQuery() sqlConn.Close() Catch If sqlConn.State = ConnectionState.Open Then sqlConn.Close() End If Throw End Try End Sub Public Shared Function CheckIsRunning(ByVal dsn As String, ByVal programmId As Integer) As Object Dim sqlConn As New SqlConnection(dsn) Try Dim sqlCom As New SqlCommand sqlConn.Open() sqlCom.CommandType = CommandType.StoredProcedure sqlCom.Connection = sqlConn sqlCom.CommandText = "CheckIsRunning" sqlCom.Parameters.Add(New SqlParameter("@ProgrammId", programmId)) Dim sqlParam As SqlParameter = sqlCom.Parameters.Add("@IsRunning", SqlDbType.Bit) sqlParam.Direction = ParameterDirection.Output sqlCom.ExecuteNonQuery() sqlConn.Close() Return sqlParam.Value Catch If sqlConn.State = ConnectionState.Open Then sqlConn.Close() End If Throw End Try End Function 'Gets a start parameter from db Public Shared Sub GetStartParameter(ByVal dsn As String, ByVal jobId As Integer, ByVal startParameterType As StartParameterType, ByRef parameterValue As String) Dim sqlConn As New SqlConnection(dsn) Try Dim sqlCom As New SqlCommand Dim da As New SqlDataAdapter Dim ds As New DataSet sqlConn.Open() sqlCom.CommandType = CommandType.StoredProcedure sqlCom.Connection = sqlConn sqlCom.CommandText = "GetStartParameter" sqlCom.Parameters.Add(New SqlParameter("@JobId", CInt(jobId))) sqlCom.Parameters.Add(New SqlParameter("@StartParameterTypId", CInt(startParameterType))) da.SelectCommand = sqlCom da.Fill(ds) sqlConn.Close() If ds.Tables.Count > 0 Then If ds.Tables(0).Rows.Count > 0 Then parameterValue = ds.Tables.Item(0).Rows(0).Item(0) End If End If Catch ex As Exception If sqlConn.State = ConnectionState.Open Then sqlConn.Close() End If Throw ex End Try End Sub 'Loads all active jobs from database Public Shared Sub LoadJobs(ByVal common As Common.Common, ByVal jobType As JobType, ByVal ds As DataSet) Dim sqlConn As New SqlConnection(common.DSN) Try Dim sqlCom As New SqlCommand Dim da As New SqlDataAdapter sqlConn.Open() sqlCom.CommandType = CommandType.StoredProcedure sqlCom.Connection = sqlConn sqlCom.CommandText = "GetJobs" sqlCom.Parameters.Add(New SqlParameter("@JobTypId", CInt(jobType))) da.SelectCommand = sqlCom da.Fill(ds) sqlConn.Close() Catch ex As Exception If sqlConn.State = ConnectionState.Open Then sqlConn.Close() End If Throw ex End Try End Sub End Class