Skip to content

Commit

Permalink
fix: db schedule_cron_job now updates too
Browse files Browse the repository at this point in the history
If the cron string (`p_schedule`) is different than the currently scheduled cron job, `schedule_cron_job` now makes sure that the schedule is updated.
  • Loading branch information
tazlin committed Jun 10, 2024
1 parent 8782e95 commit 22135a3
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions sql_statements/cron/schedule_cron_job.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,27 @@ CREATE OR REPLACE PROCEDURE schedule_cron_job(
LANGUAGE plpgsql
AS $$
DECLARE
job_exists boolean;
existing_schedule TEXT;
existing_jobid INT;
BEGIN
SET search_path TO cron, public;
-- Check if the job already exists
SELECT EXISTS (
SELECT 1 FROM cron.job
WHERE command = format($CRON$ CALL %s(); $CRON$, p_stored_procedure)
) INTO job_exists;


-- Get the existing schedule and jobid for the stored procedure
SELECT schedule, jobid
INTO existing_schedule, existing_jobid
FROM cron.job
WHERE command = format($CRON$ CALL %s(); $CRON$, p_stored_procedure);

-- If the job exists and the schedules don't match, update it
IF FOUND AND existing_schedule <> p_schedule THEN
PERFORM cron.unschedule(existing_jobid);
PERFORM cron.schedule(p_schedule, format($CRON$ CALL %s(); $CRON$, p_stored_procedure));
RAISE NOTICE 'Cron job schedule updated successfully for stored procedure: %', p_stored_procedure;
-- If the job doesn't exist, schedule it
IF NOT job_exists THEN
ELSIF NOT FOUND THEN
PERFORM cron.schedule(p_schedule, format($CRON$ CALL %s(); $CRON$, p_stored_procedure));
RAISE NOTICE 'Cron job scheduled successfully for stored procedure: %', p_stored_procedure;
ELSE
RAISE NOTICE 'Cron job already exists for stored procedure: %. Skipping scheduling.', p_stored_procedure;
RAISE NOTICE 'Cron job already exists with the same schedule for stored procedure: %. Skipping scheduling.', p_stored_procedure;
END IF;
END $$;

0 comments on commit 22135a3

Please sign in to comment.