Files
medreport_mrb2b/supabase/sql/supabase-cron-setup.md
2025-09-05 01:39:06 +03:00

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

  1. Supabase project with database access
  2. Your application deployed and accessible via a public URL
  3. JOBS_API_TOKEN environment 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/json
    • x-jobs-api-key: YOUR_JOBS_API_TOKEN
  • Body: JSON with metadata about the cron trigger

Security Considerations

  1. API Token: Store your JOBS_API_TOKEN securely and never commit it to version control
  2. Network Access: Ensure your Supabase instance can reach your deployed application
  3. Rate Limiting: The 15-minute interval should be appropriate for your use case

Troubleshooting

Job Not Running

  1. Check if extensions are enabled:

    select * from pg_extension where extname in ('pg_cron', 'pg_net');
    
  2. Verify job is active:

    select * from cron.job where jobname = 'sync-analysis-results-every-15-minutes';
    
  3. 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

  1. Verify your JOBS_API_TOKEN is correct
  2. 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:

  1. Create an Edge Function that calls your API
  2. Use Supabase's built-in cron triggers for Edge Functions
  3. This approach provides better logging and error handling

Contact your team lead for assistance with Edge Functions setup if needed.