Skip to content

Monitor Supabase Cron Jobs

Supabase Cron records each run in the cron.job_run_details table. To get an alert when a run fails or the job stops running, ping a Chronos monitor from the same SQL job with the pg_net extension.

  1. Create a monitor with the same cron expression as the Supabase Cron job, and set its timezone to UTC. Supabase Cron runs in UTC, and a monitor’s timezone defaults to your workspace’s default timezone. If you changed cron.timezone, use that timezone instead.
  2. Enable the pg_net extension in your Supabase project.
  3. Store the ping URL in Supabase Vault. Run this once in the SQL editor, or add the secret under Vault in the Supabase dashboard:
select vault.create_secret('https://ping.chronos.sh/k3Jx9Qm2TnV7bRw4YcZ8dHfL', 'nightly_cleanup_ping_url');

Don’t put this statement in a migration file, since that commits the ping URL. Give each monitor its own secret, named after its job.

  1. Schedule the job with the work and the pings in one block. The block reads the ping URL from Vault, so it’s safe to commit:
select cron.schedule(
'nightly-cleanup',
'0 3 * * *',
$$
do $job$
declare
ping_url text := (
select decrypted_secret from vault.decrypted_secrets where name = 'nightly_cleanup_ping_url'
);
begin
delete from events where created_at < now() - interval '30 days';
perform net.http_get(
url := ping_url,
timeout_milliseconds := 10000
);
exception when others then
perform net.http_post(
url := ping_url || '/fail',
body := jsonb_build_object('error', SQLERRM),
timeout_milliseconds := 10000
);
end
$job$;
$$
);

pg_net sends requests only after the transaction commits, so the success ping goes out once the work is saved. When the work raises an error, the exception branch rolls the work back and queues the fail ping with the error message, which shows up in the down alert.

timeout_milliseconds gives each ping 10 seconds. A ping that takes longer shows as a timed-out row in net._http_response, and the monitor goes down after the grace period.

Run the block between $$ and $$ once in the SQL editor. The ping goes out when the statement commits, and the monitor moves from new to up. To test the alert, add raise exception 'test failure'; before the perform net.http_get line and run it again.