Skip to content

Instantly share code, notes, and snippets.

@samsondav
Created April 11, 2018 08:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samsondav/7491249c354c7e5699796441fe44e637 to your computer and use it in GitHub Desktop.
Save samsondav/7491249c354c7e5699796441fe44e637 to your computer and use it in GitHub Desktop.
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