Commit c5e0e02
authored
fix: prevent cron from permanently dying after handler errors (#16219)
When any error occurs during a cron job, the cron stops firing
permanently and never recovers until the process is restarted.
This happens because of a bug in croner v9 combined with missing error
handling in the cron setup. Croner's `protect: true` option sets an
internal `blocking` flag before running the handler and only clears it
after the handler completes. In v9, if the handler throws, the flag is
never cleared because there's no `try/finally` around it. Every future
cron sees `blocking = true` and skips, so the cron is dead forever.
Even without the croner bug, the error from the handler was propagating
as an unhandled promise, which can crash the process.
One example where this affected us was if there is a db error while
running the job. Even if that error is temporary and would be resolved
during the next run, the cron job would no longer run until the process
is restarted.
## Two changes fix this:
- Bumped croner from v9 to v10, which wraps the handler in `try/finally`
so the `blocking` flag is always cleared regardless of whether the
handler throws. This was a confirmed bug in v9
- Added the `catch` option to the Cron constructor, which tells croner
to catch handler errors and pass them to a callback instead of letting
them escape as unhandled rejections. The callback logs the error via
`payload.logger.error` so it's visible in application logs.
---
- To see the specific tasks where the Asana app for GitHub is being
used, see below:
- https://app.asana.com/0/0/12139838362660321 parent 54189e1 commit c5e0e02
6 files changed
Lines changed: 12248 additions & 4263 deletions
File tree
- packages/payload
- src
- bin
- queues/operations/handleSchedules
- test/queues
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
107 | 107 | | |
108 | 108 | | |
109 | 109 | | |
110 | | - | |
| 110 | + | |
111 | 111 | | |
112 | 112 | | |
113 | 113 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
| 46 | + | |
| 47 | + | |
46 | 48 | | |
47 | 49 | | |
48 | 50 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
754 | 754 | | |
755 | 755 | | |
756 | 756 | | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
757 | 760 | | |
758 | 761 | | |
| 762 | + | |
| 763 | + | |
759 | 764 | | |
760 | 765 | | |
761 | 766 | | |
| |||
Lines changed: 4 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
141 | 141 | | |
142 | 142 | | |
143 | 143 | | |
144 | | - | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
145 | 148 | | |
146 | 149 | | |
147 | 150 | | |
| |||
0 commit comments