简体   繁体   English

如何在nodejs中使用带有bulmq包的Worker在队列中每分钟运行一次cron作业?

[英]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.

相关问题 如何在nodejs中创建一个每30天运行一次的cron作业 - How to create a cron job to run every 30 days in nodejs 在Node.js中运行Cron作业 - Run cron job in nodejs 我的 cron 作业只运行一次,我希望它每分钟运行一次 - My cron job only runs once, I would like it to run every minute 如何在将来的某个日期时间使用 Nodejs 中的 Agenda 运行类似 cron 的作业 - How do I run cron-like job at certain date time in the future using Agenda in Nodejs 议程作业库,如何在每个月的最后一天午夜或一天的最后一刻执行 cron - Agenda job library, How to execute cron every last day of the month at mid night or last minute of the day NodeJS“ Cron”软件包-计划作业,每20秒运行一次 - NodeJS 'Cron' Package - schedule job which runs after every 20 seconds 如何使用 Firebase 运行 cron 作业? - How to run cron job with Firebase? 可以查询队列或添加属性以检查具有特定属性的作业是否已存在于 BULLMQ 库中的队列中 - It is possible to query a queue or add a propertie to check if a job with certain property already exists on queue in BULLMQ library 每秒使用cron作业执行脚本的替代方法? - Alternatives to executing a script using cron job every second? Promise 在 NodeJS 的 Cron Job 中不起作用 - Promise is not working in Cron Job in NodeJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM