bee-queue icon indicating copy to clipboard operation
bee-queue copied to clipboard

Repeat job?

Open ChrisRobston opened this issue 6 years ago • 4 comments

Hi. Is this possible to achieve repeatable job? Let's say to run a job every 5 minutes.

ChrisRobston avatar Aug 19 '18 13:08 ChrisRobston

You can create a task run every 5 minutes and write job in that.

kirito1121 avatar Jan 07 '19 04:01 kirito1121

Repeatable jobs aren't an explicitly supported feature of bee-queue. We might be willing to consider a pull-request, but it has rather deep implications for a bunch of assumptions we make about unidirectional state transitions.

@kirito1121 is right about being able to chain jobs off of each other.

skeggse avatar Apr 16 '20 20:04 skeggse

You can also have either the job 'succeeded' handler or the process function (handed Queue#process) do the work to submit a new (delayed job). But, there's a question of what component is in charge of ensuring the repeating job is always in play (and only one of them). You could use a customId for the job, and have then delete that job when it's done and create a new one with the same name, delayed. That way, there's one name for the repeating job...

hughsw avatar Apr 20 '20 14:04 hughsw

At first I've tried job.delayUntil(nextTime).save just inside process handler. It kinda worked but my queue kept saying that the job is detected as stalled. Looks like modifying the job while it is still being processed is not such a good idea (:

So I've come up with a workaround like this one instead:

const MY_JOB_ID = 'hello-world';

queue.process(async job => {

  // do some work
  await new Promise(resolve => setTimeout(resolve, 1000));

  // explicitly get job in order to receive its events
  const self = await queue.getJob(MY_JOB_ID);

  // reschedule on success
  self.once('succeeded', () => queue.createJob().setId(MY_JOB_ID).delayUntil(Date.now() + 10000).save());

});

// check if job is present
const job = await queue.getJob(MY_JOB_ID);

// create if not
if(!job)
  await queue.createJob().setId(MY_JOB_ID).save();

Here the new job (with the same id) is created after the old one is succeeded and removed (removeOnSuccess: true). Works for me good enough for now.

Gaen avatar Aug 06 '21 15:08 Gaen