Listoca

Scheduled work

pg_cron schedules in the database that call Edge Functions through pg_net, with retries kept in tables.

There is no job worker. Recurring work is a pg_cron schedule in a migration. A schedule either runs SQL directly or calls an Edge Function with pg_net. Work that has to survive a failing provider waits in a table (an "outbox"), and the function takes a batch, counts the attempt and retries later.

The schedules

ScheduleWhenWhat
send-queued-emailevery minutesends due rows of email_outbox; up to 5 attempts with backoff
cleanup-email-outboxdaily, 03:27 UTCdeletes sent emails after 7 days, failed ones after 30
cleanup-activitydaily, 03:17 UTCdeletes old user activity
reconcile-entitlementsdaily, 04:43 UTCre-fetches entitlements that end around now (Payments)
deliver-notificationsevery minutedelivers what the insert trigger missed (Notifications)
send-digestshourly, at :05sends the daily notification summaries that are due
check-push-receiptsevery 30 minutesreads Expo's receipts and removes tokens of devices that are gone
cleanup-notificationsdaily, 03:41 UTCdeletes notifications older than 90 days

The schedules that call a function only do so while there is work (where exists (…)), so an idle database makes no requests.

How a schedule calls a function

select cron.schedule(
  'send-queued-email',
  '* * * * *',
  $$
    select net.http_post(
      url := private.function_url('send-queued-email'),
      headers := private.function_headers(),
      body := '{}'::jsonb
    )
    where exists (select 1 from public.email_outbox where sent_at is null and send_after <= now());
  $$
);

private.function_url and private.function_headers read the functions' base URL and the cron secret from Vault (functions_url, cron_secret; the local seed sets both). The function checks the secret first (requireSecret).

Adding one

  1. A handler in packages/functions/src/handlers/<name>.ts that starts with requireSecret(request, deps.env.CRON_SECRET), registered in handlers/index.ts and in supabase/config.toml (verify_jwt = false).
  2. If it works through rows, a claim_… function that takes a batch with for update skip locked and counts the attempt, so overlapping runs never do one twice.
  3. The schedule in a migration.
  4. A *.db.test.ts that calls the handler directly, wrapped in withPausedJobs([...]) so the real schedule doesn't take the test's rows first.

Locally

pg_cron runs in the local stack too. select * from cron.job_run_details order by start_time desc shows recent runs; the function logs are in docker logs supabase_edge_runtime_listoca.

On this page