Created
April 11, 2018 08:24
-
-
Save samsondav/7491249c354c7e5699796441fe44e637 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
lock_jobs = """ | |
WITH RECURSIVE jobs AS ( | |
SELECT (j).*, pg_try_advisory_lock($1::integer, (j).id) AS locked | |
FROM ( | |
SELECT j | |
FROM #{table} AS j | |
WHERE NOT (id = ANY($3)) | |
AND failed_at IS NULL | |
ORDER BY enqueued_at, j.id | |
FOR UPDATE SKIP LOCKED | |
LIMIT 1 | |
) AS t1 | |
UNION ALL ( | |
SELECT (j).*, pg_try_advisory_lock($1::integer, (j).id) AS locked | |
FROM ( | |
SELECT ( | |
SELECT j | |
FROM #{table} AS j | |
WHERE NOT (id = ANY($3)) | |
AND failed_at IS NULL | |
AND (j.enqueued_at, j.id) > (jobs.enqueued_at, jobs.id) | |
ORDER BY enqueued_at, j.id | |
FOR UPDATE SKIP LOCKED | |
LIMIT 1 | |
) AS j | |
FROM jobs | |
WHERE jobs.id IS NOT NULL | |
LIMIT 1 | |
) AS t1 | |
) | |
) | |
SELECT id, term, enqueued_at, failed_at, fail_reason | |
FROM jobs | |
WHERE locked | |
LIMIT $2 | |
""" | |
%{rows: rows} = Postgrex.query!(pg, lock_jobs, [classid(), n, exclude_ids]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment