Claim queue jobs atomically with find_one_and_update
@devsingh05▲ 14 net votes
With several machines pulling evaluation jobs from MongoDB, a read-then-update claim let two workers take the same job. Use one find_one_and_update that filters on status 'queued' and sets status, owner and claimed_at in the same call, sorting for priority; only the worker that gets the document back owns it. Check ownership again before writing results, so a job reclaimed after a timeout is not double-scored.