Skip to content

Commit 1796fe6

Browse files
Merge pull request #91 from oliver-oloughlin/patch/catch-cron-errors
catch errors that occur within exitOn and setInterval callbacks + upd…
2 parents 4597cf1 + 69091c1 commit 1796fe6

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -704,11 +704,11 @@ db.listenQueue(async (data) => {
704704
### cron()
705705

706706
Create a cron job that will run on interval, either indefinitely or until an
707-
exit condition is met. Interval defaults to 1 second if not set. Like with queue
708-
listeners, there can be multiple cron jobs defined.
707+
exit condition is met. Interval defaults to 1 hour if not set. Like with queue
708+
listeners, multiple cron jobs can be created.
709709

710710
```ts
711-
// Will repeat indefinitely with 1 second interval
711+
// Will repeat indefinitely with 1 hour interval
712712
db.cron(() => console.log("Hello World!"))
713713

714714
// First job starts with a 10 second delay, after that there is a 5 second delay between jobs
@@ -722,8 +722,8 @@ db.cron(() => console.log("I terminate after running 10 times"), {
722722
// If this is set it will override the fixed interval
723723
setInterval: ({ count }) => count * 500
724724

725-
// Count starts at 0 and is given before the current job is run
726-
exit: ({ count }) => count === 10,
725+
// Count starts at 0, exitOn is run before the current job
726+
exitOn: ({ count }) => count === 10,
727727
})
728728
```
729729

src/kvdex.ts

+28-4
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,17 @@ export function kvdex<const T extends SchemaDefinition>(
9393

9494
// Add queue listener
9595
kv.listenQueue(async (msg) => {
96+
// Parse queue message
9697
const parsed = parseQueueMessage(msg)
9798
if (!parsed.ok) {
9899
return
99100
}
100101

102+
// Find correct queue handlers
101103
const { __data__, __handlerId__ } = parsed.msg
102104
const handlers = queueHandlers.get(__handlerId__)
103105

106+
// Run queue handlers
104107
await allFulfilled(handlers?.map((handler) => handler(__data__)) ?? [])
105108
})
106109
}
@@ -343,6 +346,7 @@ export class KvDex<const T extends Schema<SchemaDefinition>> {
343346
*
344347
* @param job - Work that will be run for each job interval.
345348
* @param options - Cron options.
349+
* @returns Cron job ID.
346350
*/
347351
async cron(
348352
job: (msg: CronMessage) => unknown,
@@ -379,16 +383,33 @@ export class KvDex<const T extends Schema<SchemaDefinition>> {
379383
// Add cron job listener
380384
this.listenQueue<CronMessage>(async (msg) => {
381385
// Check if exit criteria is met, end repeating cron job if true
382-
const exit = await options?.exitOn?.(msg) ?? false
386+
let exit = false
387+
try {
388+
exit = await options?.exitOn?.(msg) ?? false
389+
} catch (e) {
390+
console.error(
391+
`An error was caught while running exitOn callback for cron job {ID = ${id}}`,
392+
e,
393+
)
394+
}
395+
383396
if (exit) {
384397
await options?.onExit?.(msg)
385398
return
386399
}
387400

388401
// Set the next interval
389-
const interval = options?.setInterval
390-
? await options?.setInterval!(msg)
391-
: options?.interval ?? DEFAULT_CRON_INTERVAL
402+
let interval = DEFAULT_CRON_INTERVAL
403+
try {
404+
interval = options?.setInterval
405+
? await options?.setInterval!(msg)
406+
: options?.interval ?? DEFAULT_CRON_INTERVAL
407+
} catch (e) {
408+
console.error(
409+
`An error was caught while running setInterval callback for cron job {ID = ${id}}`,
410+
e,
411+
)
412+
}
392413

393414
await allFulfilled([
394415
// Enqueue next job
@@ -411,6 +432,9 @@ export class KvDex<const T extends Schema<SchemaDefinition>> {
411432
isFirstJob: true,
412433
enqueueTimestamp: new Date(),
413434
}, options?.startDelay)
435+
436+
// Return the cron job id
437+
return id
414438
}
415439
}
416440

0 commit comments

Comments
 (0)