Skip to content

Commit

Permalink
Fix pre/post migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
medihack committed Oct 19, 2024
1 parent 236d4c2 commit 6418d8f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@
-- This is a pre-migration script.
-- - Whenever we recreate an immutable object (function, trigger, indexes), we
-- will suffix its name with a version number.
--
-- Add an 'abort_requested' column to the procrastinate_jobs table

-- Add an 'abort_requested' column to the procrastinate_jobs table
ALTER TABLE procrastinate_jobs ADD COLUMN abort_requested boolean DEFAULT false NOT NULL;

-- Create a new enum type without 'aborting'
CREATE TYPE procrastinate_job_status_v1 AS ENUM (
'todo',
'doing',
'succeeded',
'failed',
'cancelled',
'aborted'
);

-- Set abort requested flag on all jobs with 'aborting' status
UPDATE procrastinate_jobs SET abort_requested = true WHERE status = 'aborting';

Expand Down Expand Up @@ -108,7 +117,7 @@ CREATE OR REPLACE TRIGGER procrastinate_jobs_notify_queue_job_inserted_v1
EXECUTE PROCEDURE procrastinate_notify_queue_job_inserted_v1();


-- Create the new versions for the functions
-- Create the new versioned functions (without aborting state)
CREATE FUNCTION procrastinate_fetch_job_v1(
target_queue_names character varying[]
)
Expand Down
115 changes: 2 additions & 113 deletions procrastinate/sql/migrations/03.00.00_50_post_cancel_notification.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- These are old versions of function, that we needed
-- to keep around for backwards compatibility. We can now safely drop them.
-- These are old versions of functions, that we needed to keep around for
-- backwards compatibility. We can now safely drop them.
DROP FUNCTION IF EXISTS procrastinate_finish_job(
integer,
procrastinate_job_status,
Expand Down Expand Up @@ -42,16 +42,6 @@ UPDATE procrastinate_jobs SET abort_requested = true WHERE status = 'aborting';
DROP TRIGGER IF EXISTS procrastinate_trigger_sync_abort_requested_with_status_v1 ON procrastinate_jobs;
DROP FUNCTION IF EXISTS procrastinate_sync_abort_requested_with_status_v1;

-- Create a new enum type without 'aborting'
CREATE TYPE procrastinate_job_status_v1 AS ENUM (
'todo',
'doing',
'succeeded',
'failed',
'cancelled',
'aborted'
);

-- We need to drop the default temporarily as otherwise DatatypeMismatch would occur
ALTER TABLE procrastinate_jobs ALTER COLUMN status DROP DEFAULT;

Expand Down Expand Up @@ -96,107 +86,6 @@ CREATE INDEX procrastinate_jobs_id_lock_idx ON procrastinate_jobs (id, lock) WHE
-- Drop the old type
DROP TYPE procrastinate_job_status;

-- Recreate and update the functions to use the new column
CREATE FUNCTION procrastinate_fetch_job_v1(
target_queue_names character varying[]
)
RETURNS procrastinate_jobs
LANGUAGE plpgsql
AS $$
DECLARE
found_jobs procrastinate_jobs;
BEGIN
WITH candidate AS (
SELECT jobs.*
FROM procrastinate_jobs AS jobs
WHERE
-- reject the job if its lock has earlier jobs
NOT EXISTS (
SELECT 1
FROM procrastinate_jobs AS earlier_jobs
WHERE
jobs.lock IS NOT NULL
AND earlier_jobs.lock = jobs.lock
AND earlier_jobs.status IN ('todo', 'doing')
AND earlier_jobs.id < jobs.id)
AND jobs.status = 'todo'
AND (target_queue_names IS NULL OR jobs.queue_name = ANY( target_queue_names ))
AND (jobs.scheduled_at IS NULL OR jobs.scheduled_at <= now())
ORDER BY jobs.priority DESC, jobs.id ASC LIMIT 1
FOR UPDATE OF jobs SKIP LOCKED
)
UPDATE procrastinate_jobs
SET status = 'doing'
FROM candidate
WHERE procrastinate_jobs.id = candidate.id
RETURNING procrastinate_jobs.* INTO found_jobs;

RETURN found_jobs;
END;
$$;

CREATE FUNCTION procrastinate_finish_job_v1(job_id bigint, end_status procrastinate_job_status_v1, delete_job boolean)
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
_job_id bigint;
BEGIN
IF end_status NOT IN ('succeeded', 'failed', 'aborted') THEN
RAISE 'End status should be either "succeeded", "failed" or "aborted" (job id: %)', job_id;
END IF;
IF delete_job THEN
DELETE FROM procrastinate_jobs
WHERE id = job_id AND status IN ('todo', 'doing')
RETURNING id INTO _job_id;
ELSE
UPDATE procrastinate_jobs
SET status = end_status,
abort_requested = false,
attempts = CASE status
WHEN 'doing' THEN attempts + 1 ELSE attempts
END
WHERE id = job_id AND status IN ('todo', 'doing')
RETURNING id INTO _job_id;
END IF;
IF _job_id IS NULL THEN
RAISE 'Job was not found or not in "doing" or "todo" status (job id: %)', job_id;
END IF;
END;
$$;

CREATE FUNCTION procrastinate_cancel_job_v1(job_id bigint, abort boolean, delete_job boolean)
RETURNS bigint
LANGUAGE plpgsql
AS $$
DECLARE
_job_id bigint;
BEGIN
IF delete_job THEN
DELETE FROM procrastinate_jobs
WHERE id = job_id AND status = 'todo'
RETURNING id INTO _job_id;
END IF;
IF _job_id IS NULL THEN
IF abort THEN
UPDATE procrastinate_jobs
SET abort_requested = true,
status = CASE status
WHEN 'todo' THEN 'cancelled'::procrastinate_job_status_v1 ELSE status
END
WHERE id = job_id AND status IN ('todo', 'doing')
RETURNING id INTO _job_id;
ELSE
UPDATE procrastinate_jobs
SET status = 'cancelled'::procrastinate_job_status_v1
WHERE id = job_id AND status = 'todo'
RETURNING id INTO _job_id;
END IF;
END IF;
RETURN _job_id;
END;
$$;

-- Recreate or rename the triggers & their associated functions

ALTER FUNCTION procrastinate_trigger_status_events_procedure_insert
Expand Down

0 comments on commit 6418d8f

Please sign in to comment.