bull icon indicating copy to clipboard operation
bull copied to clipboard

[Question] How to run a repeatable job immediate?

Open tiaod opened this issue 5 years ago • 10 comments

Agenda has a option to skip the immediate run: https://github.com/agenda/agenda#manually-working-with-a-job

options.skipImmediate: true | false (default) Setting this true will skip the immediate run. The first run will occur only in configured interval.

In Bull, a repeatable job will skip the immediate run by default. How to not skip the immediate run?

tiaod avatar Mar 11 '19 09:03 tiaod

you could add one normal job and that will execute as soon as possible, not sure if that does what you want?

manast avatar Mar 11 '19 19:03 manast

I'm trying to add some repeatable job when server started. eg refreshAccessToken removeOldUsers etc. Bull is smart enough not to add the same repeatable job if the repeat options are the same, so I can restart my server as many as I want without worried about runing the repeatable job too often. If I add one normal job and that will execute as soon as possible when server startup, it will run every time when I restart my server.

tiaod avatar Mar 12 '19 00:03 tiaod

I am surprised there's no way to do that natively. I also need to start repeatable job right away and then repeat it with cron interval. How to do that safely?

bdrtsky avatar Jul 30 '19 20:07 bdrtsky

I'm working on it but it's not simple to do, I even identified a "bug" when you do queue.add(repeatableJob) then you do a job.promote(), the job is reschedule 2x times window later not one.

HugoPoi avatar Aug 10 '19 16:08 HugoPoi

So just to clarify why repeatable jobs work they way they work: the repeatable interval is moved so that the repetition always happen in the exact timestamp. Imagine dividing the time into evenly spaced bins, and the repetitions can only happen on those bins. The reason for this is to avoid all kind of hazards that could otherwise happen. By doing it this way, and having a specific jobId for every bin, we guarantee that jobs will only be processed once per repetition no matter what.

manast avatar Aug 11 '19 08:08 manast

So in my PR I just set the first timestamp to the previous iteration not the next. So behavior will be the same except the first job will be not space out of the good time amount of the second job.

HugoPoi avatar Aug 11 '19 09:08 HugoPoi

You should close this issue, as this is pretty easily possible:

queue.add(null, { repeat: false })

niftylettuce avatar May 26 '20 06:05 niftylettuce

Also for anyone interested, I have a pretty advanced queue setup with Bull in https://forwardemail.net source code at https://github.com/forwardemail/forwardemail.net (based off my work with https://lad.js.org and https://github.com/ladjs/bull).

niftylettuce avatar May 26 '20 06:05 niftylettuce

You should close this issue, as this is pretty easily possible:

queue.add(null, { repeat: false })

Can you elaborate your response to the problem ? And please don't post ads on issues.

HugoPoi avatar May 26 '20 10:05 HugoPoi

It's not an ad, it's literally a link to how we solved this. Look at the source code for Forward Email:

https://github.com/forwardemail/forwardemail.net/blob/master/bull.js#L44-L52

Here you can see we have a recurring job that properly is emptied on boot, then added as a recurring job, and then added as a normal job through the code I shared here https://github.com/OptimalBits/bull/issues/1239#issuecomment-633826857

niftylettuce avatar May 27 '20 04:05 niftylettuce

Any updates on this?

Feel like this is a pretty big issue.

delivey avatar Feb 06 '23 17:02 delivey

@delivey this is possible in BullMQ using the "immediate" option in repeat: https://api.docs.bullmq.io/interfaces/RepeatOptions.html#immediately We are not backporting new features to Bull anymore.

manast avatar Feb 06 '23 18:02 manast

what happened to the "immediate" option?

dberardo-com avatar Jul 19 '23 14:07 dberardo-com

The link has changed: https://api.docs.bullmq.io/interfaces/v4.RepeatOptions.html#immediately

manast avatar Jul 20 '23 16:07 manast