Skip to content

Commit

Permalink
Merge pull request #893 from procrastinate-org/drop-old-sql-1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ewjoachim authored Jan 14, 2024
2 parents 74611fd + 2fca400 commit 5bb90c5
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 81 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP FUNCTION IF EXISTS procrastinate_finish_job(integer, procrastinate_job_status, timestamp with time zone, boolean);
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ DROP FUNCTION IF EXISTS procrastinate_finish_job(integer, procrastinate_job_stat
DROP FUNCTION IF EXISTS procrastinate_finish_job(integer, procrastinate_job_status);

-- https://github.com/procrastinate-org/procrastinate/pull/381
DROP FUNCTION IF EXISTS procrastinate_finish_job(integer, procrastinate_job_status, timestamp with time zone, boolean);
CREATE FUNCTION procrastinate_finish_job(job_id integer, end_status procrastinate_job_status, delete_job boolean) RETURNS void
CREATE OR REPLACE FUNCTION procrastinate_finish_job(job_id integer, end_status procrastinate_job_status, delete_job boolean) RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
Expand Down Expand Up @@ -37,3 +36,8 @@ BEGIN
END IF;
END;
$$;

-- https://github.com/procrastinate-org/procrastinate/pull/471
DROP FUNCTION IF EXISTS procrastinate_defer_periodic_job(character varying, character varying, character varying, character varying, bigint);

ALTER TABLE procrastinate_periodic_defers DROP COLUMN "queue_name";
2 changes: 1 addition & 1 deletion procrastinate/sql/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ WHERE id IN (

-- finish_job --
-- Finish a job, changing it from "doing" to "succeeded" or "failed"
SELECT procrastinate_finish_job(%(job_id)s, %(status)s, NULL, %(delete_job)s);
SELECT procrastinate_finish_job(%(job_id)s, %(status)s, %(delete_job)s);

-- retry_job --
-- Retry a job, changing it from "doing" to "todo"
Expand Down
97 changes: 25 additions & 72 deletions procrastinate/sql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ CREATE TABLE procrastinate_periodic_defers (
task_name character varying(128) NOT NULL,
defer_timestamp bigint,
job_id bigint REFERENCES procrastinate_jobs(id) NULL,
queue_name character varying(128) NULL,
periodic_id character varying(128) NOT NULL DEFAULT '',
CONSTRAINT procrastinate_periodic_defers_unique UNIQUE (task_name, periodic_id, defer_timestamp)
);
Expand Down Expand Up @@ -183,7 +182,7 @@ $$;
-- procrastinate_finish_job
-- the next_scheduled_at argument is kept for compatibility reasons, it is to be
-- removed after 1.0.0 is released
CREATE FUNCTION procrastinate_finish_job(job_id integer, end_status procrastinate_job_status, next_scheduled_at timestamp with time zone, delete_job boolean) RETURNS void
CREATE FUNCTION procrastinate_finish_job(job_id integer, end_status procrastinate_job_status, delete_job boolean) RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
Expand Down Expand Up @@ -336,80 +335,34 @@ CREATE TRIGGER procrastinate_trigger_delete_jobs


-- Old versions of functions, for backwards compatibility (to be removed
-- after 1.0.0)

CREATE FUNCTION procrastinate_finish_job(job_id integer, end_status procrastinate_job_status, next_scheduled_at timestamp with time zone) RETURNS void
LANGUAGE plpgsql
AS $$
BEGIN
UPDATE procrastinate_jobs
SET status = end_status,
attempts = attempts + 1,
scheduled_at = COALESCE(next_scheduled_at, scheduled_at)
WHERE id = job_id;
END;
$$;

CREATE FUNCTION procrastinate_finish_job(job_id integer, end_status procrastinate_job_status) RETURNS void
-- after 2.0.0)
-- procrastinate_finish_job
CREATE OR REPLACE FUNCTION procrastinate_finish_job(job_id integer, end_status procrastinate_job_status, next_scheduled_at timestamp with time zone, delete_job boolean) RETURNS void
LANGUAGE plpgsql
AS $$
BEGIN
UPDATE procrastinate_jobs
SET status = end_status,
attempts = attempts + 1
WHERE id = job_id;
END;
$$;

CREATE FUNCTION procrastinate_defer_periodic_job(
_queue_name character varying,
_lock character varying,
_queueing_lock character varying,
_task_name character varying,
_defer_timestamp bigint
) RETURNS bigint
LANGUAGE plpgsql
AS $$
DECLARE
_job_id bigint;
_defer_id bigint;
_job_id bigint;
BEGIN

INSERT
INTO procrastinate_periodic_defers (task_name, queue_name, defer_timestamp)
VALUES (_task_name, _queue_name, _defer_timestamp)
ON CONFLICT DO NOTHING
RETURNING id into _defer_id;

IF _defer_id IS NULL THEN
RETURN NULL;
IF end_status NOT IN ('succeeded', 'failed') THEN
RAISE 'End status should be either "succeeded" or "failed" (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,
attempts =
CASE
WHEN status = '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;

UPDATE procrastinate_periodic_defers
SET job_id = procrastinate_defer_job(
_queue_name,
_task_name,
_lock,
_queueing_lock,
('{"timestamp": ' || _defer_timestamp || '}')::jsonb,
NULL
)
WHERE id = _defer_id
RETURNING job_id INTO _job_id;

DELETE
FROM procrastinate_periodic_defers
USING (
SELECT id
FROM procrastinate_periodic_defers
WHERE procrastinate_periodic_defers.task_name = _task_name
AND procrastinate_periodic_defers.queue_name = _queue_name
AND procrastinate_periodic_defers.defer_timestamp < _defer_timestamp
ORDER BY id
FOR UPDATE
) to_delete
WHERE procrastinate_periodic_defers.id = to_delete.id;

RETURN _job_id;
END;
$$;

0 comments on commit 5bb90c5

Please sign in to comment.