Repository navigation
fix(db-*): atomic job claiming for drizzle and mongodb in 3.x (#18565) - #18709
JessRynkar merged 10 commits into
Conversation
…dcms#18565) - In @payloadcms/db-mongodb, use findOneAndUpdate for single job claims (limit: 1) and maintain query filters for multi-job candidate claiming. - In @payloadcms/drizzle, perform atomic single-pass row updates guarded by eq(table.processing, false) and return only newly claimed rows. - Eliminates race conditions and null reading 'log' crashes across concurrent runners in 3.x without requiring schema migrations. Closes payloadcms#18565
DanRibbens
left a comment
There was a problem hiding this comment.
Written with AI
Requesting changes on ddb73f4. A non-breaking v3 fix is possible, but this backport is incomplete and introduces a MongoDB regression.
-
Blocking: MongoDB batch claims leave jobs stuck without executing them. In
packages/db-mongodb/src/updateJobs.ts, lines 135–146,claimQueryretains the originalprocessing: falsepredicate.updateManychanges the matching jobs toprocessing: true, thenModel.find(claimQuery)excludes those same jobs. With the defaultlimit: 10, even a single runner can mark jobs as processing, return no jobs to execute, and leave them stranded. Reading back only by candidate IDs would reintroduce duplicate execution. For a schema-free v3 fix, claim each candidate with conditionalfindOneAndUpdateusing the original eligibility query and return only the documents those calls actually claimed. -
Concurrent
runByIDcalls remain unprotected. The caller still uses an unconditional update by ID. MongoDB matches only the ID, and Drizzle's optimized ID path returns before reaching the new conditional-claim branch. #17441 also changed the caller to use a conditional claim; the adapter changes alone do not cover this path. Please add an atomic processing-state guard while deliberately preserving v3's other existingrunByIDbehavior, rather than copying all of v4's eligibility restrictions without a compatibility assessment. -
The v3 hook/depth paths bypass these adapter changes.
queues/utilities/updateJob.tsusespayload.updatewhenjobs.runHooksis true orjobs.depthis nonzero. Those paths need an explicit solution or a clearly documented limitation; silently removing or bypassing hooks/population would change supported v3 behavior.
Please add database-backed regression coverage for default/greater-than-one batch limits, competing queue runners, concurrent runByID, and completion cleanup on MongoDB, PostgreSQL, and SQLite. Copying #17441's limit: 1 polling test alone would miss the MongoDB batch regression above. The hook/depth paths also need coverage if included in the concurrency guarantee.
There is no need to backport #17441's breaking changes wholesale: Drizzle's conditional UPDATE ... RETURNING can identify the jobs it claimed without a token, and MongoDB can do the same with per-candidate findOneAndUpdate. This avoids adding processingToken to the jobs schema or requiring SQL migrations. Keep the optional custom-adapter fallback for v3 rather than making updateJobs mandatory; scope the concurrency guarantee accordingly.
Validation: source comparison against #17441 and execution of this PR's MongoDB adapter function with mocked database operations confirmed the batch read-back failure and repeated ID claims. Database-backed integration tests were not run as part of this review.
- db-mongodb: claim batch candidates with per-candidate conditional findOneAndUpdate and return only the documents actually claimed (fixes jobs stuck as processing) - drizzle: drop non-existent processingToken from claim detection - runByID: atomically claim only when the job is not already processing - runHooks/depth: claim through the adapter first, then run payload.update on the claimed IDs so hooks and population still apply - add regression tests for batch limits, competing runners, concurrent runByID and completion cleanup, with and without runHooks
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the
|
|
Written with AI Follow-up review of I would keep changes requested because the hook/depth path still needs work:
Validation: source review and execution of the actual updated helper with mocked database/API operations reproduced both the thrown-hook and bulk-error cases leaving |
…tion semantics - Release claimed jobs back to processing: false when payload.update throws or reports per-document hook errors - Preserve originalDoc/previousDoc processing: false during pre-claimed hook updates - Add integration coverage for hook transitions, hook failures, and nonzero jobs.depth population
…tion semantics - Release claimed jobs back to processing: false when payload.update throws or reports per-document hook errors - Preserve originalDoc/previousDoc processing: false during pre-claimed hook updates - Add integration coverage for hook transitions, hook failures, and nonzero jobs.depth population
…tion semantics - Release claimed jobs back to processing: false when payload.update throws or reports per-document hook errors - Preserve originalDoc/previousDoc processing: false during pre-claimed hook updates - Add integration coverage for hook transitions, hook failures, and nonzero jobs.depth population
|
Hi @DanRibbens, Thank you for the detailed follow-up review! The three points have been addressed in the latest updates (
All database-backed integration test suites (MongoDB, PostgreSQL, SQLite) and queue suites pass cleanly in CI. Ready for your final re-review whenever you have a moment. |
|
🚀 This is included in version v3.94.0 |
Summary of Changes
@payloadcms/db-mongodb(updateJobs.ts):Model.findOneAndUpdate(query, updateData, ...)forlimit === 1.query(processing: false) in$and: [query, { _id: { $in: candidateIDs } }]during batch claiming (limit > 1) so concurrent workers do not overwrite each other's claims.@payloadcms/drizzle(updateJobs.ts):and(inArray(table.id, jobIDs), eq(table.processing, false))returning only newly claimed rows whenisInitialJobClaim(data).Root Cause / Technical Context
In Payload 3.x (
v3.90.2), job claiming uses a non-atomic two-phasefindfollowed byupdateManyper row without concurrency guards:processing: false.deleteJobOnComplete: true, the faster runner finishes and deletes the job row. The slower runner then attempts to update the deleted row, resulting inTypeError: Cannot read properties of null (reading 'log')and HTTP 500 errors.Verification & Testing
limit: 1) atomicfindOneAndUpdateand multi-runner candidate filtering.Closes #18565