Skip to content

Commit

Permalink
GITBOOK-202: change request with no subject merged in GitBook
Browse files Browse the repository at this point in the history
  • Loading branch information
manast authored and gitbook-bot committed Oct 3, 2024
1 parent 8faaf4c commit 37903fb
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
3 changes: 2 additions & 1 deletion docs/gitbook/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
* [Debouncing](guide/jobs/debouncing.md)
* [Delayed](guide/jobs/delayed.md)
* [Repeatable](guide/jobs/repeatable.md)
* [Job Scheduler](guide/jobs/job-scheduler.md)
* [Job Scheduler](guide/jobs/job-scheduler/README.md)
* [Repeat Strategies](guide/jobs/job-scheduler/repeat-strategies.md)
* [Prioritized](guide/jobs/prioritized.md)
* [Removing jobs](guide/jobs/removing-job.md)
* [Stalled](guide/jobs/stalled.md)
Expand Down
File renamed without changes.
62 changes: 62 additions & 0 deletions docs/gitbook/guide/jobs/job-scheduler/repeat-strategies.md
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 },
);
```

0 comments on commit 37903fb

Please sign in to comment.