Query icon
OutputValues icon
Log icon
If icon
SlackIncomingWebhook icon
Pause icon
Loop icon
Sleep icon
LoopUntil icon
Fail icon
Schedule icon

SQL Server Agent Failed Job Recovery Guard

Flag allowlisted Agent jobs disabled or last failed in msdb, alert Slack, then re-enable and re-run them behind an approval, then re-read sysjobhistory.

Categories
DataInfrastructure

SQL Server Agent jobs are the instance's scheduled maintenance: full backups, log backups, index rebuilds, statistics updates, integrity checks. When one of them stops running the instance stops being protected. Because the job simply does not run there is no error anywhere to see. A job gets disabled during an incident at 2am and nobody re-enables it. A job's last run failed on a full disk and no alert was wired up. Either way the only visible effect is the slow accumulation of whatever the job was preventing: a database with no recent backup, a transaction log that never truncates, fragmentation that never gets rebuilt. The gap is usually found during the next incident, which is the worst time to find it.

This blueprint reads msdb.dbo.sysjobs for the enabled flag and msdb.dbo.sysjobhistory for the last job outcome and flags the jobs that are either disabled or whose last run failed. It flags only the jobs you name on an allowlist, the real scheduled maintenance that must stay live, so a job that was disabled on purpose and left off the list is never touched. It alerts Slack. When you allow it and a human approves the run, it re-enables the disabled jobs with sp_update_job, starts each one now with sp_start_job, polls sysjobactivity until the runs finish, then reads sysjobhistory again and fails the run if any targeted job is still disabled or its fresh run failed again.

This blueprint was created by zkasuran.

Safety

  • Only jobs named on the managed_jobs allowlist are ever flagged, re-enabled or started. A disabled job that is not on the list is left exactly as it is, because the flow cannot know it was not disabled on purpose.
  • Read-only by default. dry_run is true and auto_remediate is false, so a normal run only reads msdb and alerts.
  • A write happens only when auto_remediate is true, dry_run is false and a human resumes the Pause with approved: true. A pause resumed without an answer reads as a refusal.
  • Re-running a maintenance job has real side effects. A backup job writes a backup, an index rebuild takes locks, an integrity check reads the whole database. The approval gate exists so the sequence never starts in a busy window on its own.
  • The remediation loop has concurrencyLimit: 1, so two sp_start_job calls never overlap.
  • A re-run that fails again is treated as a real finding, not a flow bug. The re-read surfaces the fresh failure and fails the run honestly rather than retrying blindly, so the underlying cause gets looked at.
  • sp_start_job is asynchronous. The flow waits for the started jobs to finish in sysjobactivity before it judges them, so it never reads a stale outcome from before the re-run.

How it works

  1. audit (io.kestra.plugin.jdbc.sqlserver.Query, read-only) joins sysjobs to the allowlist with STRING_SPLIT, reads the last job level outcome (step_id = 0) from sysjobhistory with an OUTER APPLY and keeps the jobs that are disabled or, with include_failed on, last failed. Each row carries its own sp_update_job and sp_start_job statement, built in T-SQL with the job name escaped.
  2. targets (io.kestra.plugin.core.output.OutputValues) collects the per job plan and the escaped name list for the re-reads. summary (io.kestra.plugin.core.log.Log) logs each flagged job with its enabled flag and last outcome.
  3. route (io.kestra.plugin.core.flow.If):
    • clean: logs that every managed job is enabled and last succeeded or that the allowlist is empty.
    • findings: alert posts the flagged jobs to Slack.
      • remediate_gate runs only when auto_remediate is true and dry_run is false.
      • approval_gate (io.kestra.plugin.core.flow.Pause) holds the run until a human resumes it.
      • remediate (io.kestra.plugin.core.flow.Loop, concurrencyLimit: 1) runs sp_update_job @enabled = 1 then sp_start_job for each flagged job.
      • settle (io.kestra.plugin.core.flow.Sleep) waits briefly for Agent to pick up the runs, then wait_for_jobs (io.kestra.plugin.core.flow.LoopUntil) polls sysjobactivity until no targeted job is still executing.
      • after_state reads the enabled flag and latest outcome back for every touched job and state_log logs the before and after side by side.
      • reverify returns only the jobs still disabled or not succeeding. verify_result fails the run through io.kestra.plugin.core.execution.Fail if there are any.
      • declined logs the findings untouched when the approval was refused.
  4. errors posts a Slack alert when the audit cannot run, a statement is rejected, a job does not finish in time or a targeted job is still disabled or failing.

What you get

  • A read of the one thing normal monitoring never shows: a maintenance job that is not running, told apart from one that ran and failed.
  • An allowlist so the flow acts only on the jobs you declared must be live. A job disabled on purpose stays disabled.
  • A fix that re-enables and actually runs each job, so you find out now whether it works, not at the next backup window.
  • A second read of sysjobhistory that fails the run when a job is still disabled or its fresh run failed again, so a job that cannot recover is never reported as fixed.
  • A Slack alert on the findings and a separate alert on failure.

Who it's for

  • DBAs who want to know that every backup, log backup, index and integrity job is enabled and succeeding, without opening Agent on each instance.
  • Platform teams who had an incident, disabled a job to get through it and need a standing check that catches the one nobody re-enabled.
  • On-call engineers who want a job failure to surface and get re-run under control, with the re-run failing loudly if the cause is still there.
  • Anyone evaluating the SQL Server plugin who wants an example that drives msdb stored procedures and waits on sysjobactivity behind an approval.

Why orchestrate this with Kestra

The hard part is not re-enabling a job, it is knowing which jobs matter, doing it under control and proving it worked. Blanket enabling every disabled job would turn jobs back on that someone disabled for a reason. Running a maintenance job blind can drop a heavy rebuild into a busy afternoon. Trusting that sp_start_job succeeded says nothing, because it only requests the run. Kestra reads the real state from msdb first, acts only on an allowlist, gates the change behind two inputs and a human approval, runs the jobs one at a time, waits for them to finish, then reads sysjobhistory again and fails loudly if a job is still disabled or failing. The Slack alert says what was found and whether anything changed. The run history keeps every before and after.

Tested end to end

On Kestra 2.0.5 against SQL Server 2022 (16.0.4295.3) on Linux with MSSQL_AGENT_ENABLED=True, a local HTTP sink standing in for Slack. Four Agent jobs were seeded, each with one target server and one step. w34sqljob_backup_ok is enabled, its step succeeds and it was run once (healthy control, must never be flagged). w34sqljob_index_disabled has a step that waits six seconds then succeeds and was then disabled. w34sqljob_integrity_fail is enabled with a step that always RAISERRORs and was run once so its last outcome is a real failure. w34sqljob_adhoc_disabled is disabled and left off the allowlist (disabled on purpose control). Every number below was read back with sqlcmd against msdb.dbo.sysjobs and sysjobhistory, not from the flow log.

Run Inputs Result
1 allowlist backup_ok,index_disabled,integrity_fail, defaults 2 flagged: index_disabled enabled=0, integrity_fail last=Failed. backup_ok not flagged. Slack said "No change applied". sqlcmd confirmed index_disabled still disabled
2 same, auto_remediate: true, dry_run: true still 2 flagged, still "No change applied". The dry run blocked the write with auto_remediate on
3 same, auto_remediate: true, dry_run: false, pause resumed approved: false declined logged the findings. sqlcmd confirmed index_disabled still disabled, integrity_fail still last=Failed
4 allowlist backup_ok,index_disabled, auto_remediate: true, dry_run: false, approved: true index_disabled re-enabled and run, the poll waited one iteration for the six second step, fresh outcome Succeeded. reverify returned 0 rows. sqlcmd: index_disabled enabled 0 to 1, last NeverRun to Succeeded
5 allowlist backup_ok only 0 flagged, clean branch. A healthy allowlisted job is never flagged
6 allowlist backup_ok,index_disabled,integrity_fail, auto_remediate: true, dry_run: false, approved: true integrity_fail re-run, fresh outcome Failed again. reverify returned it and remediation_failed failed the run. The errors block posted to Slack
7 allowlist empty 0 flagged, clean branch said the allowlist is empty
8 unreachable host audit failed on the TCP connection and the errors block posted to Slack

Across every run w34sqljob_adhoc_disabled stayed enabled = 0 and was never read or touched, which proves the allowlist gates the action. w34sqljob_backup_ok stayed enabled = 1 with last Succeeded throughout, which proves the audit does not flag a healthy job.

Prerequisites

  • A SQL Server instance you can reach over JDBC with SQL Server Agent running (MSSQL_AGENT_ENABLED=True on the Linux image).
  • A login that can read msdb.dbo.sysjobs, sysjobhistory and sysjobactivity and that can run sp_update_job and sp_start_job. Membership of SQLAgentOperatorRole in msdb is enough, as is sysadmin.
  • A Slack incoming webhook.

Secrets

  • SQLSERVER_PASSWORD: password for the login in user. The password input defaults to this secret.
  • SLACK_WEBHOOK_URL: Slack incoming webhook, used by alert and the errors block.
  • In Kestra OSS, provide each one as a base64-encoded environment variable prefixed SECRET_.

Inputs

Input Default Purpose
url jdbc:sqlserver://localhost:1433 SQL Server instance to check
user sa SQL Server login
password {{ secret('SQLSERVER_PASSWORD') }} Password for the login
managed_jobs empty Comma separated allowlist of job names that must stay live
include_failed true Also flag an allowlisted job whose last run failed
max_wait PT3M How long to wait for started jobs to finish
auto_remediate false Allow the statements to run
dry_run true Must be false for a statement to run

Quick start

  1. Add the SQLSERVER_PASSWORD and SLACK_WEBHOOK_URL secrets.
  2. Import the flow, set url and user, then set managed_jobs to the exact names of the backup, log backup, index and integrity jobs that must stay enabled.
  3. Run it once with the defaults and read which managed jobs are disabled or last failed.
  4. Run with auto_remediate: true and dry_run: false and approve the pause to re-enable and run them. Read the re-verify result.
  5. Enable the daily trigger, which always runs in safe mode.

How to extend

  • Build the allowlist from a naming convention instead of a fixed list, by matching sysjobs.name against a LIKE pattern in the audit, so a new maintenance job is covered the day it is created.
  • Flag a job whose schedule is disabled, not only the job, by reading sysschedules.enabled through sysjobschedules.
  • Flag a job that has not run in longer than its interval, by reading the next and last run from sysjobschedules and sysjobhistory, so a job that silently stopped firing is caught too.
  • Post the failing step's message from sysjobhistory to Slack so an on-call engineer sees why a job failed without opening the server.
  • Raise max_wait for an instance whose maintenance jobs run for many minutes, so the poll does not give up before a long rebuild finishes.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.