3.3 KiB
3.3 KiB
Supabase Cron Job Setup for sync-analysis-results
This document explains how to set up a Supabase cron job to automatically call the sync-analysis-results API endpoint every 15 minutes.
Prerequisites
- Supabase project with database access
- Your application deployed and accessible via a public URL
JOBS_API_TOKENenvironment variable configured
Setup Steps
1. Enable Required Extensions
First, enable the required PostgreSQL extensions in your Supabase project:
create extension if not exists pg_cron;
create extension if not exists pg_net;
You can run this either:
- In the Supabase Dashboard → Database → SQL Editor
- Or deploy the migration file:
supabase/migrations/setup_sync_analysis_results_cron.sql
2. Schedule the Cron Job
Use the helper function to schedule the cron job with your specific configuration:
select schedule_sync_analysis_results_cron(
'https://your-actual-domain.com', -- Replace with your API URL
'your-actual-jobs-api-token' -- Replace with your JOBS_API_TOKEN
);
3. Verify the Setup
Check if the job was scheduled successfully:
select * from cron.job where jobname = 'sync-analysis-results-every-15-minutes';
Management Commands
View Job Execution History
select * from cron.job_run_details
where jobid = (select jobid from cron.job where jobname = 'sync-analysis-results-every-15-minutes')
order by start_time desc limit 10;
Check All Scheduled Jobs
select jobid, schedule, active, jobname from cron.job;
Unschedule the Job
select cron.unschedule('sync-analysis-results-every-15-minutes');
Configuration Details
- Schedule:
*/15 * * * *(every 15 minutes) - HTTP Method: POST
- Headers:
Content-Type: application/jsonx-jobs-api-key: YOUR_JOBS_API_TOKEN
- Body: JSON with metadata about the cron trigger
Security Considerations
- API Token: Store your
JOBS_API_TOKENsecurely and never commit it to version control - Network Access: Ensure your Supabase instance can reach your deployed application
- Rate Limiting: The 15-minute interval should be appropriate for your use case
Troubleshooting
Job Not Running
-
Check if extensions are enabled:
select * from pg_extension where extname in ('pg_cron', 'pg_net'); -
Verify job is active:
select * from cron.job where jobname = 'sync-analysis-results-every-15-minutes'; -
Check for execution errors:
select * from cron.job_run_details where jobid = (select jobid from cron.job where jobname = 'sync-analysis-results-every-15-minutes') and status = 'failed' order by start_time desc;
API Authentication Issues
- Verify your
JOBS_API_TOKENis correct - Test the API endpoint manually:
curl -X POST https://your-domain.com/api/job/sync-analysis-results \ -H "Content-Type: application/json" \ -H "x-jobs-api-key: YOUR_JOBS_API_TOKEN"
Alternative: Using Supabase Edge Functions
If you prefer using Supabase Edge Functions instead of pg_cron, you can:
- Create an Edge Function that calls your API
- Use Supabase's built-in cron triggers for Edge Functions
- This approach provides better logging and error handling
Contact your team lead for assistance with Edge Functions setup if needed.