[英]How to run cron job every minute in Queue using Workers with bullmq package in nodejs?
import { Queue, Worker, Job } from 'bullmq';
const myQueue = new Queue('firstjob', { connection: redis });
import { autorunJobs } from './processes/job.process';
async function addQueue() {
await myQueue.add(
'autoclosejob',
{
repeat: {
cron: '* * * * *'
},
},
);
}
addQueue();
const worker = new Worker('firstjob', async (job: Job) => {
console.info('Started.');
await autorunJobs();
console.info('Closed.');
//return true;
}, { connection: redis });
worker.run();
When I run this code, it executes successfully at once.当我运行此代码时,它立即成功执行。 But after I wait for the worker to run it again in the next minute but it doesn't.
但是在我等待工人在下一分钟再次运行它之后,它没有。 What I see is only one-time console.log output.
我看到的只是一次性的 console.log 输出。
Following are one-time output logs:以下是一次性输出日志:
Tue, 10 May 2022 12:21:12 GMT | IDM Service | INFO | Started.
dta = "some data"
Tue, 10 May 2022 12:21:12 GMT | IDM Service | INFO | Closed.
I want this output after every minute.我希望每分钟都有这个输出。
What I am doing wrong?我做错了什么?
So the important thing here is to ask yourself what exactly a repeatable job is.所以这里重要的是问问自己什么是可重复的工作。 This will solve your issue.
这将解决您的问题。 To answer that question in short:
简而言之,回答这个问题:
A repeatable job is in essence a delayed job.可重复的工作本质上是延迟的工作。 What does this entail?
这意味着什么? A delayed job needs a QueueScheduler, not a normal Queue.
延迟的作业需要 QueueScheduler,而不是普通的 Queue。 That's most likely why your job does not repeat after the first time.
这很可能是您的工作在第一次之后没有重复的原因。
Docs that provide more info: https://docs.bullmq.io/guide/jobs/repeatable提供更多信息的文档: https ://docs.bullmq.io/guide/jobs/repeatable
I hope this answers your question and for people who may run into the same issue in the future.我希望这能回答您的问题以及将来可能遇到相同问题的人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.