2.0 KiB
2.0 KiB
Postgres + pg_cron scheduled shift reminders
- Run migrations
- Apply
supabase/migrations/20260318_add_scheduled_notifications.sql(and other pending migrations) to your Supabase DB.
- Enable pg_cron (requires Supabase project admin)
- If your Supabase tier supports it, enable the
pg_cronextension. - Example (run as a privileged role):
CREATE EXTENSION IF NOT EXISTS pg_cron;
SELECT cron.schedule('shift_reminders_every_min', '*/1 * * * *',
SELECT public.enqueue_due_shift_notifications(););
- Deploy processor Edge Function
-
Add
supabase/functions/process_scheduled_notifications/to your Supabase functions and deploy with the following required env vars:SUPABASE_URLSUPABASE_SERVICE_ROLE_KEYSEND_FCM_URL(the HTTP URL for your existingsend_fcmfunction, e.g., https://.functions.supabase.co/send_fcm)- Optional:
PROCESSOR_BATCH_SIZE(default 50)
-
You can deploy via
supabase functions deploy process_scheduled_notificationsor using your CI.
- Scheduling / triggering processor
- Option A (recommended): Use
pg_cronto only enqueue rows, and configure a small interval GitHub Actions or Cloud Scheduler to call the Edge Function endpoint (POST) every minute. This keeps sending out of the DB. - Option B: Use
pg_cronto call the Edge Function HTTP endpoint directly ifpg_httpis available (not recommended unless approved).
- Verification
- Insert a test
duty_schedulesrecord 15 minutes ahead and runSELECT public.enqueue_due_shift_notifications();manually — confirmscheduled_notificationsrow appears. - Call the Edge Function (
supabase functions invoke process_scheduled_notifications --project <id>) or POST to its URL — confirmsend_fcmreceives payload andscheduled_notificationsrow becomes processed.
- Monitoring
- Watch
cron.job_run_detailsfor cron runs andscheduled_notificationsrows withretry_count> 3. - Inspect
notification_pushestable to ensure deduplication works.