Repository navigation
fix(cron): don't wedge a job when the lastStartDate write fails - #345
Merged
omegascorp merged 1 commit intoAug 28, 2026
Merged
Conversation
runCronJob wrote lastStartDate to Mongo before entering the try/catch, so a failed write threw past the handler and past handleCronJobCompletion. Since tickCronJobs runs jobs in a fire-and-forget forEach, the rejection went unhandled, isRunning stayed true, and the job was skipped every tick until its timeout elapsed. Move the write inside the try so a failed write is captured, the run is marked errored, and isRunning is reset like any other failure. Fixes #338
Contributor
Author
|
Hi! Gentle nudge on this one whenever you have some bandwidth. It's a small, self-contained fix ( |
omegascorp
self-requested a review
August 28, 2026 10:03
This was referenced Sep 8, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #338
runCronJobsetstate.isRunning = trueand then wrotelastStartDateto Mongo before entering the try/catch:If that write rejects, the throw skips both the handler and
handleCronJobCompletion, soisRunningis never reset.tickCronJobsruns jobs in a fire-and-forgetforEach(async ...), so the rejection isn't awaited or caught anywhere and just becomes an unhandled promise rejection. The job then hits theif (state.isRunning) returnguard on every tick and stays wedged untilstartTs + timeoutpasses, which is the timeout-recovery path, not normal operation.Moving the
upsertOneinside the existing try makes a failed write behave like any other failure: it's captured, the run is marked errored, andisRunningis reset so the next interval reschedules normally. The success path is unchanged (the write still happens before the handler).Added a regression test to
jobs.test.tsthat rejectsupsertOneand asserts the error is captured and the transaction ends aserror. It fails onmain(the rejection escapes,captureErroris never called) and passes with the fix. Fullcron/jobs.test.tssuite is green (8 passed).Note
Low Risk
Small, well-tested reliability fix that only relocates an existing write into the existing error path; success behavior is unchanged.
Overview
Prevents cron jobs from getting stuck when the
lastStartDateMongo write fails.Moves the
upsertOneinrunCronJobinside the existing try/catch so a rejected write is captured, the run is marked errored, andisRunningis reset viahandleCronJobCompletioninstead of leaving the job wedged until timeout.Adds a regression test that rejects
upsertOneand asserts the error is captured and the transaction ends aserror.Reviewed by Cursor Bugbot for commit af2c46a. Bugbot is set up for automated code reviews on this repo. Configure here.