Files
medreport_mrb2b/local-sync/test-cron-setup.md
2025-09-05 01:39:06 +03:00

4.1 KiB

Testing the Supabase Cron Job Setup

This guide provides step-by-step instructions to test your Supabase cron job configuration.

Quick Setup Commands

1. Deploy the Migration (Option A)

If you want to use the migration approach:

# Make sure you're connected to your Supabase project
npm run supabase:deploy

Then manually update the migration file with your actual values before deploying.

Use the SQL Editor in Supabase Dashboard:

  1. Go to your Supabase Dashboard → Database → SQL Editor
  2. Copy and paste the content from supabase/sql/setup-cron-job.sql
  3. Run the SQL to create the function
  4. Then execute the schedule function with your actual values:
select schedule_sync_analysis_results_cron(
  'https://your-production-domain.com',  -- Your actual API URL
  'your-actual-jobs-api-token'           -- Your actual JOBS_API_TOKEN
);

Testing Steps

1. Verify Extensions are Enabled

select * from pg_extension where extname in ('pg_cron', 'pg_net');

Expected result: Both pg_cron and pg_net should be listed.

2. Check Job is Scheduled

select * from cron.job where jobname = 'sync-analysis-results-every-15-minutes';

Expected result: One row with your job details, active should be true.

3. Test API Endpoint Manually

Before relying on the cron job, test your 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" \
  -v

Expected result: Status 200 with success message.

4. Monitor Job Execution

Wait for the job to run (up to 15 minutes), then check execution history:

select 
  job_run_details.*,
  job.jobname
from cron.job_run_details 
join cron.job on job.jobid = job_run_details.jobid
where job.jobname = 'sync-analysis-results-every-15-minutes'
order by start_time desc 
limit 5;

5. Check Application Logs

Monitor your application logs to see if the API calls are being received and processed successfully.

Environment Variables Required

Make sure these environment variables are set in your production environment:

  • JOBS_API_TOKEN - The API key for authenticating job requests
  • All other environment variables required by your sync-analysis-results handler

Common Issues and Solutions

Issue 1: Job Not Appearing

Problem: Job doesn't appear in cron.job table.

Solution:

  • Check if you have sufficient permissions
  • Ensure extensions are enabled
  • Try running the schedule function again

Issue 2: Job Scheduled but Not Running

Problem: Job appears in table but no execution history.

Solutions:

  • Check if active is true in cron.job table
  • Verify cron schedule format is correct
  • Check Supabase logs for any cron-related errors

Issue 3: HTTP Requests Failing

Problem: Job runs but API calls fail.

Solutions:

  • Test API endpoint manually with curl
  • Verify API URL is correct and accessible from Supabase
  • Check if JOBS_API_TOKEN is correct
  • Ensure your application is deployed and running

Issue 4: Authentication Errors

Problem: Getting 401 Unauthorized responses.

Solutions:

  • Verify x-jobs-api-key header is included
  • Check that JOBS_API_TOKEN matches between cron job and application
  • Ensure the header name is exactly x-jobs-api-key (case-sensitive)

Cleanup Commands

If you need to remove the cron job:

-- Unschedule the job
select cron.unschedule('sync-analysis-results-every-15-minutes');

-- Drop the helper function (optional)
drop function if exists schedule_sync_analysis_results_cron(text, text);

Next Steps

Once the cron job is working:

  1. Remove any old instrumentation.ts cron logic if it exists
  2. Monitor the job performance and adjust interval if needed
  3. Set up alerting for failed job executions
  4. Consider adding more detailed logging to your API endpoint

Support

If you encounter issues:

  1. Check the troubleshooting section in docs/supabase-cron-setup.md
  2. Review Supabase documentation for pg_cron and pg_net
  3. Contact your team for deployment-specific configuration details