-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #402 from procrastinate-org/periodic-lock-400
- Loading branch information
Showing
6 changed files
with
76 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
procrastinate/sql/migrations/00.19.00_02_null_locks_excluded.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
DROP FUNCTION IF EXISTS procrastinate_fetch_job(target_queue_names character varying[]); | ||
|
||
CREATE FUNCTION procrastinate_fetch_job(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.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; | ||
$$; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters