Compare commits
2 Commits
aebe4cd149
...
8fca292a50
| Author | SHA1 | Date | |
|---|---|---|---|
| 8fca292a50 | |||
| 2c94b89801 |
63
SqlAgent/01_Devl/4_Job_Maintenance/FederalHolidaysDML.sql
Normal file
63
SqlAgent/01_Devl/4_Job_Maintenance/FederalHolidaysDML.sql
Normal file
@ -0,0 +1,63 @@
|
||||
USE [DevlDUTASJobSchedule];
|
||||
GO
|
||||
|
||||
/**************************************************************************************************
|
||||
-- FILE: FederalHolidays.sql
|
||||
-- PURPOSE: Manage FederalHolidays table - Create, Read, Update, Delete for a single holiday
|
||||
-- TABLE: dbo.FederalHolidays
|
||||
**************************************************************************************************/
|
||||
|
||||
-------------------------------
|
||||
-- SELECT - View Holiday
|
||||
-------------------------------
|
||||
SELECT *
|
||||
FROM [dbo].[FederalHolidays]
|
||||
WHERE [Year] = 2025
|
||||
AND [HolidayName] = N'New Year''s Day';
|
||||
GO
|
||||
|
||||
-------------------------------
|
||||
-- INSERT - Create Holiday
|
||||
-------------------------------
|
||||
INSERT INTO [dbo].[FederalHolidays]
|
||||
(
|
||||
[HolidayDate],
|
||||
[HolidayName],
|
||||
[Year]
|
||||
)
|
||||
VALUES
|
||||
(
|
||||
'2025-01-01', -- Holiday date
|
||||
N'New Year''s Day', -- Holiday name
|
||||
2025 -- Year
|
||||
);
|
||||
GO
|
||||
|
||||
-------------------------------
|
||||
-- UPDATE - Modify Holiday
|
||||
-------------------------------
|
||||
UPDATE [dbo].[FederalHolidays]
|
||||
SET
|
||||
[HolidayName] = N'New Year''s Day (Observed)',
|
||||
[HolidayDate] = '2025-01-02'
|
||||
WHERE
|
||||
[Year] = 2025
|
||||
AND [HolidayName] = N'New Year''s Day';
|
||||
GO
|
||||
|
||||
-------------------------------
|
||||
-- DELETE - Remove Holiday
|
||||
-------------------------------
|
||||
DELETE FROM [dbo].[FederalHolidays]
|
||||
WHERE [Year] = 2025
|
||||
AND [HolidayName] = N'New Year''s Day (Observed)';
|
||||
GO
|
||||
|
||||
-------------------------------
|
||||
-- SELECT - View Holiday
|
||||
-------------------------------
|
||||
SELECT *
|
||||
FROM [dbo].[FederalHolidays]
|
||||
WHERE [Year] = 2025
|
||||
AND [HolidayName] = N'New Year''s Day';
|
||||
GO
|
||||
68
SqlAgent/01_Devl/4_Job_Maintenance/JobControlDML.sql
Normal file
68
SqlAgent/01_Devl/4_Job_Maintenance/JobControlDML.sql
Normal file
@ -0,0 +1,68 @@
|
||||
USE [DevlDUTASJobSchedule];
|
||||
GO
|
||||
|
||||
/**************************************************************************************************
|
||||
-- FILE: JobControl.sql
|
||||
-- PURPOSE: Manage JobControl table - Create, Read, Update, Delete for a single job
|
||||
-- TABLE: dbo.JobControl
|
||||
**************************************************************************************************/
|
||||
|
||||
-------------------------------
|
||||
-- SELECT - View Job Record
|
||||
-------------------------------
|
||||
SELECT *
|
||||
FROM [dbo].[JobControl]
|
||||
WHERE [JobName] = N'PROD_DAILY_DTSBX215';
|
||||
GO
|
||||
|
||||
-------------------------------
|
||||
-- INSERT - Create Job Record
|
||||
-------------------------------
|
||||
INSERT INTO [dbo].[JobControl]
|
||||
(
|
||||
[JobName],
|
||||
[ScheduledStartTime],
|
||||
[SchedulerAction],
|
||||
[IsActive],
|
||||
[CreatedDate],
|
||||
[Frequency],
|
||||
[FrequencyPattern]
|
||||
)
|
||||
VALUES
|
||||
(
|
||||
N'PROD_DAILY_DTSBX215', -- Job name
|
||||
'14:00:00', -- 2 PM (24-hour format)
|
||||
N'CONTINUE', -- Action
|
||||
1, -- Active
|
||||
GETDATE(), -- Current system time
|
||||
N'DAILY', -- Frequency
|
||||
N'{"Frequency":"Daily","Days":["Mon","Tue","Wed","Thu","Fri"]}' -- JSON pattern
|
||||
);
|
||||
GO
|
||||
|
||||
-------------------------------
|
||||
-- UPDATE - Modify Job Record
|
||||
-------------------------------
|
||||
UPDATE [dbo].[JobControl]
|
||||
SET
|
||||
[ScheduledStartTime] = '15:00:00', -- Change to 3 PM
|
||||
[SchedulerAction] = 'STOP', -- Change to STOP
|
||||
[IsActive] = 0 -- Deactivate
|
||||
WHERE
|
||||
[JobName] = N'PROD_DAILY_DTSBX215';
|
||||
GO
|
||||
|
||||
-------------------------------
|
||||
-- DELETE - Remove Job Record
|
||||
-------------------------------
|
||||
DELETE FROM [dbo].[JobControl]
|
||||
WHERE [JobName] = N'PROD_DAILY_DTSBX215';
|
||||
GO
|
||||
|
||||
-------------------------------
|
||||
-- SELECT - View Job Record
|
||||
-------------------------------
|
||||
SELECT *
|
||||
FROM [dbo].[JobControl]
|
||||
WHERE [JobName] = N'PROD_DAILY_DTSBX215';
|
||||
GO
|
||||
56
SqlAgent/01_Devl/4_Job_Maintenance/JobDependenciesDML.sql
Normal file
56
SqlAgent/01_Devl/4_Job_Maintenance/JobDependenciesDML.sql
Normal file
@ -0,0 +1,56 @@
|
||||
USE [DevlDUTASJobSchedule];
|
||||
GO
|
||||
|
||||
/**************************************************************************************************
|
||||
-- FILE: JobDependencies.sql
|
||||
-- PURPOSE: Manage JobDependencies table - Create, Read, Update, Delete for a single dependency
|
||||
-- TABLE: dbo.JobDependencies
|
||||
**************************************************************************************************/
|
||||
|
||||
-------------------------------
|
||||
-- SELECT - View Dependency
|
||||
-------------------------------
|
||||
SELECT *
|
||||
FROM [dbo].[JobDependencies]
|
||||
WHERE [JobName] = N'PROD_DAILY_DTSGSID0';
|
||||
GO
|
||||
|
||||
-------------------------------
|
||||
-- INSERT - Create Dependency
|
||||
-------------------------------
|
||||
INSERT INTO [dbo].[JobDependencies]
|
||||
(
|
||||
[JobName],
|
||||
[PredecessorJobName]
|
||||
)
|
||||
VALUES
|
||||
(
|
||||
N'PROD_DAILY_DTSGSID0', -- Dependent job
|
||||
N'PROD_DAILY_DTSBX305' -- Predecessor job
|
||||
);
|
||||
GO
|
||||
|
||||
-------------------------------
|
||||
-- UPDATE - Modify Dependency
|
||||
-------------------------------
|
||||
UPDATE [dbo].[JobDependencies]
|
||||
SET [PredecessorJobName] = N'PROD_DAILY_DTSBX215'
|
||||
WHERE [JobName] = N'PROD_DAILY_DTSGSID0'
|
||||
AND [PredecessorJobName] = N'PROD_DAILY_DTSBX305';
|
||||
GO
|
||||
|
||||
-------------------------------
|
||||
-- DELETE - Remove Dependency
|
||||
-------------------------------
|
||||
DELETE FROM [dbo].[JobDependencies]
|
||||
WHERE [JobName] = N'PROD_DAILY_DTSGSID0'
|
||||
AND [PredecessorJobName] = N'PROD_DAILY_DTSBX215';
|
||||
GO
|
||||
|
||||
-------------------------------
|
||||
-- SELECT - View Dependency
|
||||
-------------------------------
|
||||
SELECT *
|
||||
FROM [dbo].[JobDependencies]
|
||||
WHERE [JobName] = N'PROD_DAILY_DTSGSID0';
|
||||
GO
|
||||
118
SqlAgent/01_Devl/5_PowerShell_Scripts/Execute-RemoteJob.ps1
Normal file
118
SqlAgent/01_Devl/5_PowerShell_Scripts/Execute-RemoteJob.ps1
Normal file
@ -0,0 +1,118 @@
|
||||
param(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string]$JobName,
|
||||
|
||||
```
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateSet('DEVL', 'TEST', 'PROD')]
|
||||
[string]$Env
|
||||
```
|
||||
|
||||
)
|
||||
|
||||
try {
|
||||
Write-Host "Starting job $JobName in environment $Env at $(Get-Date)"
|
||||
|
||||
```
|
||||
# ------------------------------------------------------------
|
||||
# Extract JCL from JobName (ENV_FREQUENCY_JOB pattern)
|
||||
# ------------------------------------------------------------
|
||||
$JCL = $JobName -replace '^[A-Z]+_[A-Z]+_', ''
|
||||
Write-Host "Extracted JCL: $JCL from JobName: $JobName" -ForegroundColor Yellow
|
||||
|
||||
# ------------------------------------------------------------
|
||||
# Environment-specific configuration
|
||||
# ------------------------------------------------------------
|
||||
switch ($Env.ToUpper()) {
|
||||
'DEVL' {
|
||||
$ComputerName = '10.57.110.120'
|
||||
$DatabaseName = 'DevlDUTASJobSchedule'
|
||||
$SqlServer = 'DOES_DUTAS-SQL1'
|
||||
}
|
||||
'TEST' {
|
||||
$ComputerName = '10.57.110.141'
|
||||
$DatabaseName = 'TestDUTASJobSchedule'
|
||||
$SqlServer = 'DOES_DUTAS-SQL1'
|
||||
}
|
||||
'PROD' {
|
||||
$ComputerName = '10.57.111.120'
|
||||
$DatabaseName = 'ProdDUTASJobSchedule'
|
||||
$SqlServer = 'DOES_DUTAS-SQL'
|
||||
}
|
||||
default {
|
||||
throw "Invalid environment specified: $Env"
|
||||
}
|
||||
}
|
||||
|
||||
# ------------------------------------------------------------
|
||||
# Remote credentials (consider secure vault in production)
|
||||
# ------------------------------------------------------------
|
||||
$username = "DUTASSQLAdminP@does.dcgov.priv"
|
||||
$password = "4ho@3Pr&Xof8" | ConvertTo-SecureString -AsPlainText -Force
|
||||
$credential = New-Object System.Management.Automation.PSCredential($username, $password)
|
||||
|
||||
# ------------------------------------------------------------
|
||||
# Execute job remotely using the extracted JCL
|
||||
# ------------------------------------------------------------
|
||||
Write-Host "Connecting to remote server $ComputerName ..."
|
||||
$exitCode = Invoke-Command -ComputerName $ComputerName `
|
||||
-Credential $credential `
|
||||
-Authentication CredSSP `
|
||||
-SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck) `
|
||||
-ArgumentList $JobName, $JCL, $Env `
|
||||
-ScriptBlock {
|
||||
param($JobName, $JCL, $Env)
|
||||
Write-Host "Remote execution - JobName: $JobName, JCL: $JCL, Env: $Env"
|
||||
|
||||
$process = Start-Process -FilePath "powershell.exe" `
|
||||
-ArgumentList "-ExecutionPolicy Bypass -File `"E:\PSScript\RCSubmit-Job.ps1`" -JobName `"$JCL`" -Env `"$Env`"" `
|
||||
-NoNewWindow -Wait -PassThru
|
||||
return $process.ExitCode
|
||||
}
|
||||
|
||||
if (-not $exitCode) { $exitCode = 1 } # Guard for null/false exit codes
|
||||
|
||||
if ($exitCode -eq 0) {
|
||||
Write-Host "Job $JobName (JCL: $JCL) completed successfully at $(Get-Date)" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Error "Job $JobName (JCL: $JCL) failed with exit code: $exitCode"
|
||||
}
|
||||
```
|
||||
|
||||
}
|
||||
catch {
|
||||
Write-Error "Job $JobName failed with error: $($_.Exception.Message)"
|
||||
$exitCode = 1
|
||||
}
|
||||
finally {
|
||||
# ------------------------------------------------------------
|
||||
# Log exit code to the appropriate database
|
||||
# ------------------------------------------------------------
|
||||
try {
|
||||
$connectionString = "Server=$SqlServer;Database=$DatabaseName;Trusted_Connection=true;"
|
||||
$connection = New-Object System.Data.SqlClient.SqlConnection($connectionString)
|
||||
$connection.Open()
|
||||
|
||||
```
|
||||
$command = $connection.CreateCommand()
|
||||
$command.CommandText = @"
|
||||
INSERT INTO dbo.JobExitCodes (JobName, RunDate, ExitCode, RecordedTime)
|
||||
VALUES ('$JobName', CAST(GETDATE() AS DATE), $exitCode, GETDATE());
|
||||
```
|
||||
|
||||
"@
|
||||
$command.ExecuteNonQuery()
|
||||
$connection.Close()
|
||||
|
||||
```
|
||||
Write-Host "Exit code $exitCode logged to database $DatabaseName on $SqlServer for job $JobName"
|
||||
}
|
||||
catch {
|
||||
$errorMsg = $_.Exception.Message
|
||||
Write-Host "Failed to log exit code to database $DatabaseName on $SqlServer`: $errorMsg"
|
||||
}
|
||||
```
|
||||
|
||||
}
|
||||
|
||||
exit $exitCode
|
||||
@ -27,7 +27,7 @@ $summary = @()
|
||||
|
||||
try {
|
||||
$syslogPath = $output | Select-String -Pattern "SYSLOG=\[(.*)\]" | ForEach-Object { $_.Matches.Groups[1].Value }
|
||||
|
||||
|
||||
if ($syslogPath -and (Test-Path $syslogPath)) {
|
||||
$syslogFolder = Split-Path $syslogPath -Parent
|
||||
$content = Get-Content $syslogPath -ErrorAction Stop
|
||||
@ -65,17 +65,16 @@ if ($successConditions | Where-Object { & $_ }) {
|
||||
} else {
|
||||
Write-Host "Job $JobName failed with code $exitCode" -ForegroundColor Red
|
||||
$isSuccess = 1
|
||||
# Only use error if we don't have a proper summary
|
||||
if ($summary.Count -eq 0) {
|
||||
$summary = @($Error[0])
|
||||
}
|
||||
}
|
||||
|
||||
# --- Email Notification ---
|
||||
# --- Email Notification Settings ---
|
||||
$smtpServer = "smtp4.dc.gov"
|
||||
$fromEmail = "Dutas@dc.gov"
|
||||
$toEmail = "neerajk@innovaconsulting.com"
|
||||
$ccEmail = @("neerajk@innovaconsulting.com")
|
||||
$fromEmail = "Dutas@dc.gov"
|
||||
$toEmail = "zarath.lalputan@dc.gov"
|
||||
$ccEmail = @("srujani.chandragiri@dc.gov", "neerajk@innovaconsulting.com")
|
||||
|
||||
$emailBody = @"
|
||||
<pre style='font-family: Courier, monospace;'>
|
||||
@ -84,25 +83,58 @@ DUTAS Batch Job: $JobName Execution Report
|
||||
$($summary -join "`n")
|
||||
==========================================
|
||||
|
||||
Return Code: $exitCode
|
||||
Status: $(if ($isSuccess -eq 0) { "SUCCESS" } else { "FAILED" })
|
||||
JOBLOG Path: $syslogFolder
|
||||
Return Code : $exitCode
|
||||
Status : $(if ($isSuccess -eq 0) { "SUCCESS" } else { "FAILED" })
|
||||
JOBLOG Path : $syslogFolder
|
||||
</pre>
|
||||
"@
|
||||
|
||||
# --- Attachment Setup ---
|
||||
# Automatically attach MSGLOG.txt if it exists in the JOBLOG folder
|
||||
$attachmentPath = Join-Path $syslogFolder "MSGLOG.txt"
|
||||
|
||||
$sendParams = @{
|
||||
SmtpServer = $smtpServer
|
||||
Port = 25
|
||||
From = $fromEmail
|
||||
To = $toEmail
|
||||
Cc = $ccEmail
|
||||
Subject = "DUTAS-[$Env] Job Status: $JobName - $(if ($isSuccess -eq 0) { 'SUCCESS' } else { 'FAILED' })"
|
||||
Body = $emailBody
|
||||
BodyAsHtml = $true
|
||||
}
|
||||
|
||||
if (Test-Path $attachmentPath) {
|
||||
$sendParams['Attachments'] = $attachmentPath
|
||||
Write-Host "Attachment found: $attachmentPath" -ForegroundColor Cyan
|
||||
} else {
|
||||
Write-Host "No MSGLOG.txt found at $syslogFolder" -ForegroundColor Yellow
|
||||
}
|
||||
|
||||
# --- Conditional Email Sending ---
|
||||
# ===================================================================================
|
||||
# EMAIL SEND CONDITION TOGGLE:
|
||||
# By default, email will ALWAYS be sent whether the job succeeded or failed.
|
||||
# To send email ONLY if the job fails, uncomment the next line:
|
||||
# if ($isSuccess -eq 1) {
|
||||
# ===================================================================================
|
||||
|
||||
try {
|
||||
Send-MailMessage -SmtpServer $smtpServer -Port 25 `
|
||||
-From $fromEmail -To $toEmail -Cc $ccEmail `
|
||||
-Subject "DUTAS-[$Env] Job Status: $JobName - $(if ($isSuccess -eq 0) { 'SUCCESS' } else { 'FAILED' })" `
|
||||
-Body $emailBody -BodyAsHtml
|
||||
Write-Host "Preparing to send email to $toEmail..." -ForegroundColor Yellow
|
||||
Send-MailMessage @sendParams
|
||||
Write-Host "Email sent successfully to $toEmail" -ForegroundColor Cyan
|
||||
}
|
||||
catch {
|
||||
Write-Host "Failed to send email: $($_.Exception.Message)" -ForegroundColor Yellow
|
||||
}
|
||||
|
||||
# ===================================================================================
|
||||
# End of email send condition block (only required if you uncomment the IF above)
|
||||
# } # End Email send condition
|
||||
# ===================================================================================
|
||||
|
||||
Write-Host "`n---------------------------------------------"
|
||||
Write-Host "Job: $JobName | ExitCode: $exitCode | Status: $(if ($isSuccess -eq 0) { 'SUCCESS' } else { 'FAILED' })"
|
||||
Write-Host "---------------------------------------------`n"
|
||||
|
||||
exit $isSuccess
|
||||
exit $isSuccess
|
||||
63
SqlAgent/02_Test/4_Job_Maintenance/FederalHolidaysDML.sql
Normal file
63
SqlAgent/02_Test/4_Job_Maintenance/FederalHolidaysDML.sql
Normal file
@ -0,0 +1,63 @@
|
||||
USE [TestDUTASJobSchedule];
|
||||
GO
|
||||
|
||||
/**************************************************************************************************
|
||||
-- FILE: FederalHolidays.sql
|
||||
-- PURPOSE: Manage FederalHolidays table - Create, Read, Update, Delete for a single holiday
|
||||
-- TABLE: dbo.FederalHolidays
|
||||
**************************************************************************************************/
|
||||
|
||||
-------------------------------
|
||||
-- SELECT - View Holiday
|
||||
-------------------------------
|
||||
SELECT *
|
||||
FROM [dbo].[FederalHolidays]
|
||||
WHERE [Year] = 2025
|
||||
AND [HolidayName] = N'New Year''s Day';
|
||||
GO
|
||||
|
||||
-------------------------------
|
||||
-- INSERT - Create Holiday
|
||||
-------------------------------
|
||||
INSERT INTO [dbo].[FederalHolidays]
|
||||
(
|
||||
[HolidayDate],
|
||||
[HolidayName],
|
||||
[Year]
|
||||
)
|
||||
VALUES
|
||||
(
|
||||
'2025-01-01', -- Holiday date
|
||||
N'New Year''s Day', -- Holiday name
|
||||
2025 -- Year
|
||||
);
|
||||
GO
|
||||
|
||||
-------------------------------
|
||||
-- UPDATE - Modify Holiday
|
||||
-------------------------------
|
||||
UPDATE [dbo].[FederalHolidays]
|
||||
SET
|
||||
[HolidayName] = N'New Year''s Day (Observed)',
|
||||
[HolidayDate] = '2025-01-02'
|
||||
WHERE
|
||||
[Year] = 2025
|
||||
AND [HolidayName] = N'New Year''s Day';
|
||||
GO
|
||||
|
||||
-------------------------------
|
||||
-- DELETE - Remove Holiday
|
||||
-------------------------------
|
||||
DELETE FROM [dbo].[FederalHolidays]
|
||||
WHERE [Year] = 2025
|
||||
AND [HolidayName] = N'New Year''s Day (Observed)';
|
||||
GO
|
||||
|
||||
-------------------------------
|
||||
-- SELECT - View Holiday
|
||||
-------------------------------
|
||||
SELECT *
|
||||
FROM [dbo].[FederalHolidays]
|
||||
WHERE [Year] = 2025
|
||||
AND [HolidayName] = N'New Year''s Day';
|
||||
GO
|
||||
68
SqlAgent/02_Test/4_Job_Maintenance/JobControlDML.sql
Normal file
68
SqlAgent/02_Test/4_Job_Maintenance/JobControlDML.sql
Normal file
@ -0,0 +1,68 @@
|
||||
USE [TestDUTASJobSchedule];
|
||||
GO
|
||||
|
||||
/**************************************************************************************************
|
||||
-- FILE: JobControl.sql
|
||||
-- PURPOSE: Manage JobControl table - Create, Read, Update, Delete for a single job
|
||||
-- TABLE: dbo.JobControl
|
||||
**************************************************************************************************/
|
||||
|
||||
-------------------------------
|
||||
-- SELECT - View Job Record
|
||||
-------------------------------
|
||||
SELECT *
|
||||
FROM [dbo].[JobControl]
|
||||
WHERE [JobName] = N'PROD_DAILY_DTSBX215';
|
||||
GO
|
||||
|
||||
-------------------------------
|
||||
-- INSERT - Create Job Record
|
||||
-------------------------------
|
||||
INSERT INTO [dbo].[JobControl]
|
||||
(
|
||||
[JobName],
|
||||
[ScheduledStartTime],
|
||||
[SchedulerAction],
|
||||
[IsActive],
|
||||
[CreatedDate],
|
||||
[Frequency],
|
||||
[FrequencyPattern]
|
||||
)
|
||||
VALUES
|
||||
(
|
||||
N'PROD_DAILY_DTSBX215', -- Job name
|
||||
'14:00:00', -- 2 PM (24-hour format)
|
||||
N'CONTINUE', -- Action
|
||||
1, -- Active
|
||||
GETDATE(), -- Current system time
|
||||
N'DAILY', -- Frequency
|
||||
N'{"Frequency":"Daily","Days":["Mon","Tue","Wed","Thu","Fri"]}' -- JSON pattern
|
||||
);
|
||||
GO
|
||||
|
||||
-------------------------------
|
||||
-- UPDATE - Modify Job Record
|
||||
-------------------------------
|
||||
UPDATE [dbo].[JobControl]
|
||||
SET
|
||||
[ScheduledStartTime] = '15:00:00', -- Change to 3 PM
|
||||
[SchedulerAction] = 'STOP', -- Change to STOP
|
||||
[IsActive] = 0 -- Deactivate
|
||||
WHERE
|
||||
[JobName] = N'PROD_DAILY_DTSBX215';
|
||||
GO
|
||||
|
||||
-------------------------------
|
||||
-- DELETE - Remove Job Record
|
||||
-------------------------------
|
||||
DELETE FROM [dbo].[JobControl]
|
||||
WHERE [JobName] = N'PROD_DAILY_DTSBX215';
|
||||
GO
|
||||
|
||||
-------------------------------
|
||||
-- SELECT - View Job Record
|
||||
-------------------------------
|
||||
SELECT *
|
||||
FROM [dbo].[JobControl]
|
||||
WHERE [JobName] = N'PROD_DAILY_DTSBX215';
|
||||
GO
|
||||
56
SqlAgent/02_Test/4_Job_Maintenance/JobDependenciesDML.sql
Normal file
56
SqlAgent/02_Test/4_Job_Maintenance/JobDependenciesDML.sql
Normal file
@ -0,0 +1,56 @@
|
||||
USE [TestDUTASJobSchedule];
|
||||
GO
|
||||
|
||||
/**************************************************************************************************
|
||||
-- FILE: JobDependencies.sql
|
||||
-- PURPOSE: Manage JobDependencies table - Create, Read, Update, Delete for a single dependency
|
||||
-- TABLE: dbo.JobDependencies
|
||||
**************************************************************************************************/
|
||||
|
||||
-------------------------------
|
||||
-- SELECT - View Dependency
|
||||
-------------------------------
|
||||
SELECT *
|
||||
FROM [dbo].[JobDependencies]
|
||||
WHERE [JobName] = N'PROD_DAILY_DTSGSID0';
|
||||
GO
|
||||
|
||||
-------------------------------
|
||||
-- INSERT - Create Dependency
|
||||
-------------------------------
|
||||
INSERT INTO [dbo].[JobDependencies]
|
||||
(
|
||||
[JobName],
|
||||
[PredecessorJobName]
|
||||
)
|
||||
VALUES
|
||||
(
|
||||
N'PROD_DAILY_DTSGSID0', -- Dependent job
|
||||
N'PROD_DAILY_DTSBX305' -- Predecessor job
|
||||
);
|
||||
GO
|
||||
|
||||
-------------------------------
|
||||
-- UPDATE - Modify Dependency
|
||||
-------------------------------
|
||||
UPDATE [dbo].[JobDependencies]
|
||||
SET [PredecessorJobName] = N'PROD_DAILY_DTSBX215'
|
||||
WHERE [JobName] = N'PROD_DAILY_DTSGSID0'
|
||||
AND [PredecessorJobName] = N'PROD_DAILY_DTSBX305';
|
||||
GO
|
||||
|
||||
-------------------------------
|
||||
-- DELETE - Remove Dependency
|
||||
-------------------------------
|
||||
DELETE FROM [dbo].[JobDependencies]
|
||||
WHERE [JobName] = N'PROD_DAILY_DTSGSID0'
|
||||
AND [PredecessorJobName] = N'PROD_DAILY_DTSBX215';
|
||||
GO
|
||||
|
||||
-------------------------------
|
||||
-- SELECT - View Dependency
|
||||
-------------------------------
|
||||
SELECT *
|
||||
FROM [dbo].[JobDependencies]
|
||||
WHERE [JobName] = N'PROD_DAILY_DTSGSID0';
|
||||
GO
|
||||
118
SqlAgent/02_Test/5_PowerShell_Scripts/Execute-RemoteJob.ps1
Normal file
118
SqlAgent/02_Test/5_PowerShell_Scripts/Execute-RemoteJob.ps1
Normal file
@ -0,0 +1,118 @@
|
||||
param(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string]$JobName,
|
||||
|
||||
```
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateSet('DEVL', 'TEST', 'PROD')]
|
||||
[string]$Env
|
||||
```
|
||||
|
||||
)
|
||||
|
||||
try {
|
||||
Write-Host "Starting job $JobName in environment $Env at $(Get-Date)"
|
||||
|
||||
```
|
||||
# ------------------------------------------------------------
|
||||
# Extract JCL from JobName (ENV_FREQUENCY_JOB pattern)
|
||||
# ------------------------------------------------------------
|
||||
$JCL = $JobName -replace '^[A-Z]+_[A-Z]+_', ''
|
||||
Write-Host "Extracted JCL: $JCL from JobName: $JobName" -ForegroundColor Yellow
|
||||
|
||||
# ------------------------------------------------------------
|
||||
# Environment-specific configuration
|
||||
# ------------------------------------------------------------
|
||||
switch ($Env.ToUpper()) {
|
||||
'DEVL' {
|
||||
$ComputerName = '10.57.110.120'
|
||||
$DatabaseName = 'DevlDUTASJobSchedule'
|
||||
$SqlServer = 'DOES_DUTAS-SQL1'
|
||||
}
|
||||
'TEST' {
|
||||
$ComputerName = '10.57.110.141'
|
||||
$DatabaseName = 'TestDUTASJobSchedule'
|
||||
$SqlServer = 'DOES_DUTAS-SQL1'
|
||||
}
|
||||
'PROD' {
|
||||
$ComputerName = '10.57.111.120'
|
||||
$DatabaseName = 'ProdDUTASJobSchedule'
|
||||
$SqlServer = 'DOES_DUTAS-SQL'
|
||||
}
|
||||
default {
|
||||
throw "Invalid environment specified: $Env"
|
||||
}
|
||||
}
|
||||
|
||||
# ------------------------------------------------------------
|
||||
# Remote credentials (consider secure vault in production)
|
||||
# ------------------------------------------------------------
|
||||
$username = "DUTASSQLAdminP@does.dcgov.priv"
|
||||
$password = "4ho@3Pr&Xof8" | ConvertTo-SecureString -AsPlainText -Force
|
||||
$credential = New-Object System.Management.Automation.PSCredential($username, $password)
|
||||
|
||||
# ------------------------------------------------------------
|
||||
# Execute job remotely using the extracted JCL
|
||||
# ------------------------------------------------------------
|
||||
Write-Host "Connecting to remote server $ComputerName ..."
|
||||
$exitCode = Invoke-Command -ComputerName $ComputerName `
|
||||
-Credential $credential `
|
||||
-Authentication CredSSP `
|
||||
-SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck) `
|
||||
-ArgumentList $JobName, $JCL, $Env `
|
||||
-ScriptBlock {
|
||||
param($JobName, $JCL, $Env)
|
||||
Write-Host "Remote execution - JobName: $JobName, JCL: $JCL, Env: $Env"
|
||||
|
||||
$process = Start-Process -FilePath "powershell.exe" `
|
||||
-ArgumentList "-ExecutionPolicy Bypass -File `"E:\PSScript\RCSubmit-Job.ps1`" -JobName `"$JCL`" -Env `"$Env`"" `
|
||||
-NoNewWindow -Wait -PassThru
|
||||
return $process.ExitCode
|
||||
}
|
||||
|
||||
if (-not $exitCode) { $exitCode = 1 } # Guard for null/false exit codes
|
||||
|
||||
if ($exitCode -eq 0) {
|
||||
Write-Host "Job $JobName (JCL: $JCL) completed successfully at $(Get-Date)" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Error "Job $JobName (JCL: $JCL) failed with exit code: $exitCode"
|
||||
}
|
||||
```
|
||||
|
||||
}
|
||||
catch {
|
||||
Write-Error "Job $JobName failed with error: $($_.Exception.Message)"
|
||||
$exitCode = 1
|
||||
}
|
||||
finally {
|
||||
# ------------------------------------------------------------
|
||||
# Log exit code to the appropriate database
|
||||
# ------------------------------------------------------------
|
||||
try {
|
||||
$connectionString = "Server=$SqlServer;Database=$DatabaseName;Trusted_Connection=true;"
|
||||
$connection = New-Object System.Data.SqlClient.SqlConnection($connectionString)
|
||||
$connection.Open()
|
||||
|
||||
```
|
||||
$command = $connection.CreateCommand()
|
||||
$command.CommandText = @"
|
||||
INSERT INTO dbo.JobExitCodes (JobName, RunDate, ExitCode, RecordedTime)
|
||||
VALUES ('$JobName', CAST(GETDATE() AS DATE), $exitCode, GETDATE());
|
||||
```
|
||||
|
||||
"@
|
||||
$command.ExecuteNonQuery()
|
||||
$connection.Close()
|
||||
|
||||
```
|
||||
Write-Host "Exit code $exitCode logged to database $DatabaseName on $SqlServer for job $JobName"
|
||||
}
|
||||
catch {
|
||||
$errorMsg = $_.Exception.Message
|
||||
Write-Host "Failed to log exit code to database $DatabaseName on $SqlServer`: $errorMsg"
|
||||
}
|
||||
```
|
||||
|
||||
}
|
||||
|
||||
exit $exitCode
|
||||
@ -27,7 +27,7 @@ $summary = @()
|
||||
|
||||
try {
|
||||
$syslogPath = $output | Select-String -Pattern "SYSLOG=\[(.*)\]" | ForEach-Object { $_.Matches.Groups[1].Value }
|
||||
|
||||
|
||||
if ($syslogPath -and (Test-Path $syslogPath)) {
|
||||
$syslogFolder = Split-Path $syslogPath -Parent
|
||||
$content = Get-Content $syslogPath -ErrorAction Stop
|
||||
@ -65,17 +65,16 @@ if ($successConditions | Where-Object { & $_ }) {
|
||||
} else {
|
||||
Write-Host "Job $JobName failed with code $exitCode" -ForegroundColor Red
|
||||
$isSuccess = 1
|
||||
# Only use error if we don't have a proper summary
|
||||
if ($summary.Count -eq 0) {
|
||||
$summary = @($Error[0])
|
||||
}
|
||||
}
|
||||
|
||||
# --- Email Notification ---
|
||||
# --- Email Notification Settings ---
|
||||
$smtpServer = "smtp4.dc.gov"
|
||||
$fromEmail = "Dutas@dc.gov"
|
||||
$toEmail = "neerajk@innovaconsulting.com"
|
||||
$ccEmail = @("neerajk@innovaconsulting.com")
|
||||
$fromEmail = "Dutas@dc.gov"
|
||||
$toEmail = "zarath.lalputan@dc.gov"
|
||||
$ccEmail = @("srujani.chandragiri@dc.gov", "neerajk@innovaconsulting.com")
|
||||
|
||||
$emailBody = @"
|
||||
<pre style='font-family: Courier, monospace;'>
|
||||
@ -84,25 +83,58 @@ DUTAS Batch Job: $JobName Execution Report
|
||||
$($summary -join "`n")
|
||||
==========================================
|
||||
|
||||
Return Code: $exitCode
|
||||
Status: $(if ($isSuccess -eq 0) { "SUCCESS" } else { "FAILED" })
|
||||
JOBLOG Path: $syslogFolder
|
||||
Return Code : $exitCode
|
||||
Status : $(if ($isSuccess -eq 0) { "SUCCESS" } else { "FAILED" })
|
||||
JOBLOG Path : $syslogFolder
|
||||
</pre>
|
||||
"@
|
||||
|
||||
# --- Attachment Setup ---
|
||||
# Automatically attach MSGLOG.txt if it exists in the JOBLOG folder
|
||||
$attachmentPath = Join-Path $syslogFolder "MSGLOG.txt"
|
||||
|
||||
$sendParams = @{
|
||||
SmtpServer = $smtpServer
|
||||
Port = 25
|
||||
From = $fromEmail
|
||||
To = $toEmail
|
||||
Cc = $ccEmail
|
||||
Subject = "DUTAS-[$Env] Job Status: $JobName - $(if ($isSuccess -eq 0) { 'SUCCESS' } else { 'FAILED' })"
|
||||
Body = $emailBody
|
||||
BodyAsHtml = $true
|
||||
}
|
||||
|
||||
if (Test-Path $attachmentPath) {
|
||||
$sendParams['Attachments'] = $attachmentPath
|
||||
Write-Host "Attachment found: $attachmentPath" -ForegroundColor Cyan
|
||||
} else {
|
||||
Write-Host "No MSGLOG.txt found at $syslogFolder" -ForegroundColor Yellow
|
||||
}
|
||||
|
||||
# --- Conditional Email Sending ---
|
||||
# ===================================================================================
|
||||
# EMAIL SEND CONDITION TOGGLE:
|
||||
# By default, email will ALWAYS be sent whether the job succeeded or failed.
|
||||
# To send email ONLY if the job fails, uncomment the next line:
|
||||
# if ($isSuccess -eq 1) {
|
||||
# ===================================================================================
|
||||
|
||||
try {
|
||||
Send-MailMessage -SmtpServer $smtpServer -Port 25 `
|
||||
-From $fromEmail -To $toEmail -Cc $ccEmail `
|
||||
-Subject "DUTAS-[$Env] Job Status: $JobName - $(if ($isSuccess -eq 0) { 'SUCCESS' } else { 'FAILED' })" `
|
||||
-Body $emailBody -BodyAsHtml
|
||||
Write-Host "Preparing to send email to $toEmail..." -ForegroundColor Yellow
|
||||
Send-MailMessage @sendParams
|
||||
Write-Host "Email sent successfully to $toEmail" -ForegroundColor Cyan
|
||||
}
|
||||
catch {
|
||||
Write-Host "Failed to send email: $($_.Exception.Message)" -ForegroundColor Yellow
|
||||
}
|
||||
|
||||
# ===================================================================================
|
||||
# End of email send condition block (only required if you uncomment the IF above)
|
||||
# } # End Email send condition
|
||||
# ===================================================================================
|
||||
|
||||
Write-Host "`n---------------------------------------------"
|
||||
Write-Host "Job: $JobName | ExitCode: $exitCode | Status: $(if ($isSuccess -eq 0) { 'SUCCESS' } else { 'FAILED' })"
|
||||
Write-Host "---------------------------------------------`n"
|
||||
|
||||
exit $isSuccess
|
||||
exit $isSuccess
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user