Skip to content

Instantly share code, notes, and snippets.

@crookm
Created February 7, 2018 04:52
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 crookm/45b0b5aab6e3b579f474e13a2566428b to your computer and use it in GitHub Desktop.
Save crookm/45b0b5aab6e3b579f474e13a2566428b to your computer and use it in GitHub Desktop.
Atomic selection of work that prevents race conditions
UPDATE work
SET
`worker_id` = ?,
`worker_claimedAt` = NOW()
WHERE
(`worker_id` IS NULL
OR `worker_claimedAt` < DATE_SUB(NOW(), INTERVAL 30 SECOND))
AND `active` = 1
ORDER BY `worker_lastWork` DESC
LIMIT 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment