简体   繁体   English

如何从 bull js 中删除延迟的工作?

[英]how to remove a delayed job from bull js?

I am new to Bull and my use case is to run a job after 10 sec, for that, I am using the below code我是 Bull 的新手,我的用例是在 10 秒后运行一个作业,为此,我使用下面的代码

  const options = {
    delay: 10000, // in ms
    jobId: myCustomUUID,
  };

  myQueue.add(someRandomData, options);

after adding it to the queue, now after a few sec let's say 4 sec, i want to remove the job from the queue as it is no longer required due to some condition, how can I achieve it.将其添加到队列后,现在几秒后,假设是 4 秒,我想从队列中删除该作业,因为由于某些情况不再需要它,我该如何实现它。 I know there is job.remove().我知道有 job.remove()。 but how to use it for a given jobId.但是如何将它用于给定的 jobId。 can someone please help me with it.有人可以帮我吗?

// first find the job by Id
const job = await promotionEndQueue.getJob(data.id);
// then remove the job
await job?.remove();

to do that automatically you can setup on this way: https://github.com/OptimalBits/bull/blob/HEAD/REFERENCE.md#user-content-queueadd要自动执行此操作,您可以通过以下方式进行设置: https://github.com/OptimalBits/bull/blob/HEAD/REFERENCE.md#user-content-queueadd

Assuming you have a queue and some model.假设您有一个队列和一些 model。

const jobOptions = {
        removeOnComplete: true,
        removeOnFail: true
    }
this.queue.add(model, jobOptions)

I think this is best solution:我认为这是最好的解决方案:

this.queue.add('key', {data}, {removeOnComplete: true, removeOnFail: true})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM