* Register (or re-register) a node-cron job for a schedule record. * * `node-cron` does not support the `L` (last day of month) token, while BullMQ / * cron-parser does. To keep both backends in sync we expand `L` → `28-31` for * node-cron's parser and add a runtime DOM filter so
(record: ScheduleRecord)
| 169 | * actually fire when they really are the last day of the current month. |
| 170 | */ |
| 171 | private _upsertCronJob(record: ScheduleRecord): void { |
| 172 | this._removeCronJob(record.id) |
| 173 | |
| 174 | const tz = record.timezone ?? 'UTC' |
| 175 | |
| 176 | const { expression: nodeCronExpression, hasL } = expandCronLForNodeCron(record.cronExpression) |
| 177 | |
| 178 | if (!cron.validate(nodeCronExpression)) { |
| 179 | logger.warn(`[ScheduleBeat]: Invalid cron expression for schedule ${record.id}: "${record.cronExpression}", skipping`) |
| 180 | return |
| 181 | } |
| 182 | |
| 183 | const task = cron.schedule( |
| 184 | nodeCronExpression, |
| 185 | () => { |
| 186 | // When the original expression used `L`, only fire on a real match |
| 187 | // (i.e. today's DOM in `tz` actually satisfies the original DOM field). |
| 188 | if (hasL && !cronDomMatchesNow(record.cronExpression, new Date(), tz)) { |
| 189 | logger.debug( |
| 190 | `[ScheduleBeat]: Skipping cron fire for schedule ${record.id} because today does not match original DOM field with L token` |
| 191 | ) |
| 192 | return |
| 193 | } |
| 194 | this._onCronFire(record.id).catch((err) => { |
| 195 | logger.error(`[ScheduleBeat]: Error firing schedule ${record.id}: ${err}`) |
| 196 | }) |
| 197 | }, |
| 198 | { timezone: tz } |
| 199 | ) |
| 200 | |
| 201 | this.cronJobs.set(record.id, task) |
| 202 | logger.debug( |
| 203 | `[ScheduleBeat]: Registered cron job for schedule ${record.id} ` + |
| 204 | `(${record.cronExpression}${hasL ? ` → ${nodeCronExpression}` : ''} ${tz})` |
| 205 | ) |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Stop and remove a node-cron job for a schedule record. |
no test coverage detected