-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GITBOOK-202: change request with no subject merged in GitBook
- Loading branch information
1 parent
8faaf4c
commit 37903fb
Showing
3 changed files
with
64 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
File renamed without changes.
62 changes: 62 additions & 0 deletions
62
docs/gitbook/guide/jobs/job-scheduler/repeat-strategies.md
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Repeat Strategies | ||
|
||
By default, we are using [cron-parser](https://www.npmjs.com/package/cron-parser) as the default repeat strategy for cron expressions. | ||
|
||
It is possible to define a different strategy to schedule repeatable jobs. The idea is that the repeat strategy, based on a pattern and the latest job's milliseconds, return the next desired timestamp. Although not used in the following example, you could have different behaviours on your repeat strategies based on the current job's name if you want to. However not that **only** **one** repeatStrategy can be defined for a given queue. | ||
|
||
For example we can create a custom one for [RRULE](https://jkbrzt.github.io/rrule/) like this: | ||
|
||
```typescript | ||
import { Queue, QueueScheduler, Worker } from 'bullmq'; | ||
import { rrulestr } from 'rrule'; | ||
|
||
const settings = { | ||
repeatStrategy: (millis: number, opts: RepeatOptions, _jobName: string) => { | ||
const currentDate = | ||
opts.startDate && new Date(opts.startDate) > new Date(millis) | ||
? new Date(opts.startDate) | ||
: new Date(millis); | ||
|
||
const rrule = rrulestr(opts.pattern); | ||
|
||
if (rrule.origOptions.count && !rrule.origOptions.dtstart) { | ||
throw new Error('DTSTART must be defined to use COUNT with rrule'); | ||
} | ||
|
||
const next_occurrence = rrule.after(currentDate, false); | ||
return next_occurrence?.getTime(); | ||
}, | ||
}; | ||
|
||
const myQueue = new Queue('Paint', { settings }); | ||
|
||
// Repeat job every 10 seconds | ||
await myQueue.upsertJobScheduler( | ||
'collibris', | ||
{ | ||
pattern: 'RRULE:FREQ=SECONDLY;INTERVAL=10;WKST=MO', | ||
}, | ||
{ | ||
data: { color: 'green' }, | ||
}, | ||
); | ||
|
||
// Repeat job every 20 seconds | ||
await myQueue.upsertJobScheduler( | ||
'pingeons', | ||
{ | ||
pattern: 'RRULE:FREQ=SECONDLY;INTERVAL=20;WKST=MO', | ||
}, | ||
{ | ||
data: { color: 'gray' } | ||
}, | ||
); | ||
|
||
const worker = new Worker( | ||
'Paint', | ||
async () => { | ||
doSomething(); | ||
}, | ||
{ settings }, | ||
); | ||
``` |