This commit is contained in:
2025-09-05 01:39:06 +03:00
parent 84216c3ced
commit 0cf04b4f55
29 changed files with 52428 additions and 133 deletions

View File

@@ -0,0 +1,64 @@
-- Enable required extensions for cron jobs and HTTP requests
create extension if not exists pg_cron;
create extension if not exists pg_net;
-- Enable the cron extension to be used in all databases
-- This needs to be run with superuser privileges or via Supabase Dashboard
grant usage on schema cron to postgres;
grant all privileges on all tables in schema cron to postgres;
-- Function to safely schedule the cron job with environment variables
-- This approach allows for easier management and avoids hardcoding sensitive values
create or replace function schedule_sync_analysis_results_cron(
api_url text,
api_token text
) returns text as $$
declare
job_exists boolean;
begin
-- Check if job already exists
select exists(
select 1 from cron.job
where jobname = 'sync-analysis-results-every-15-minutes'
) into job_exists;
-- If job exists, unschedule it first
if job_exists then
perform cron.unschedule('sync-analysis-results-every-15-minutes');
end if;
-- Schedule the new job
perform cron.schedule(
'sync-analysis-results-every-15-minutes',
'*/15 * * * *', -- Every 15 minutes
format(
'select net.http_post(url := ''%s/api/job/sync-analysis-results'', headers := jsonb_build_object(''Content-Type'', ''application/json'', ''x-jobs-api-key'', ''%s''), body := jsonb_build_object(''triggered_by'', ''supabase_cron'', ''timestamp'', now())) as request_id;',
api_url, api_token
)
);
return 'Cron job scheduled successfully';
end;
$$ language plpgsql;
-- Example usage (replace with your actual values):
-- select schedule_sync_analysis_results_cron(
-- 'https://your-domain.com',
-- 'your-jobs-api-token-here'
-- );
-- Utility queries for managing the cron job:
-- 1. Check if the job is scheduled
-- select * from cron.job where jobname = 'sync-analysis-results-every-15-minutes';
-- 2. 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;
-- 3. Unschedule the job if needed
-- select cron.unschedule('sync-analysis-results-every-15-minutes');
-- 4. Check all scheduled jobs
-- select jobid, schedule, active, jobname from cron.job;

View File

@@ -0,0 +1,122 @@
# 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:
```sql
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:
```sql
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:
```sql
select * from cron.job where jobname = 'sync-analysis-results-every-15-minutes';
```
## Management Commands
### View Job Execution History
```sql
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
```sql
select jobid, schedule, active, jobname from cron.job;
```
### Unschedule the Job
```sql
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:
```sql
select * from pg_extension where extname in ('pg_cron', 'pg_net');
```
2. Verify job is active:
```sql
select * from cron.job where jobname = 'sync-analysis-results-every-15-minutes';
```
3. Check for execution errors:
```sql
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:
```bash
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.