← All guides

Guide · Prefect

Monitor Prefect with a dead man's switch

Prefect's own UI shows you a flow's state, but it won't tell you when the orchestrator itself stalls, an agent goes offline, or a scheduled deployment silently stops firing. A Mortemain check-in, wired in with a couple of state hooks, closes that gap.

1. Create a check

Create a check in Mortemain sized to the flow's schedule (plus a grace window for normal run time) and copy its unique ping URL, for example https://ping.mortemain.com/your-check-uuid.

2. Add state hooks to the flow

Prefect 2 and 3 flows accept on_completion and on_failure lists of hook functions. Ping the base URL when the flow completes, and /fail when it fails, so a crash alerts immediately instead of waiting for the schedule to lapse.

 flow.py
import httpx
from prefect import flow

URL = "https://ping.mortemain.com/your-check-uuid"

def notify_success(flow, flow_run, state):
    httpx.get(URL, timeout=10)

def notify_failure(flow, flow_run, state):
    httpx.get(URL + "/fail", timeout=10)

@flow(on_completion=[notify_success], on_failure=[notify_failure])
def nightly_etl():
    ...  # your tasks

State hooks run for the top-level flow run regardless of how many tasks it contains, so this is enough for most pipelines, no changes needed inside individual tasks.

3. Track run duration (optional)

Add an on_running hook to ping /start the moment the flow begins. Mortemain then reports how long each run took, on top of whether it happened at all.

 flow.py
def notify_start(flow, flow_run, state):
    httpx.get(URL + "/start", timeout=10)

@flow(
    on_running=[notify_start],
    on_completion=[notify_success],
    on_failure=[notify_failure],
)
def nightly_etl():
    ...

4. Deployments and work pools

State hooks fire the same way whether the flow runs locally, from a deployment on a schedule, or via a work pool and worker. That means one check covers the whole path: if the worker dies, the scheduler stalls, or the run itself fails, no check-in arrives and Mortemain alerts on the missed window.

Test it

Run the flow once, prefect deployment run or locally. The check flips to up within a second or two of the ping; force an exception to confirm on_failure triggers the down alert straight away.