如何使用nodejs中的node-cron在不同的时间执行多个cron作业?

发布于 2025-01-14 08:04:07 字数 557 浏览 4 评论 0原文

我怎样才能在不同的时间执行这个?请帮忙解决这个问题。预先感谢...

const Cron = require('node-cron');
const Cron2 = require('node-cron');

var TaskOne = Cron.schedule('*/10 * * * *', async() => {
     
    //first job implemented here and its working fine  
    function1();
 });
 
 var TaskTwo = Cron2.schedule('*/11 * * * *', async() => {
     
    // Second job implemented here....
    //Why this block is not getting executed??????????????????????? 
    function2();    
}); 

我怎样才能在不同的时间执行这个? TaskTwo 没有被执行。调试器不会进入 TaskTwo。请帮忙解决这个问题。提前致谢...

How can I execute this with different timings? Please help on this. Thanks in Advance...

const Cron = require('node-cron');
const Cron2 = require('node-cron');

var TaskOne = Cron.schedule('*/10 * * * *', async() => {
     
    //first job implemented here and its working fine  
    function1();
 });
 
 var TaskTwo = Cron2.schedule('*/11 * * * *', async() => {
     
    // Second job implemented here....
    //Why this block is not getting executed??????????????????????? 
    function2();    
}); 

How can I execute this with different timings? TaskTwo is not getting executed. Debugger not goes into TaskTwo. Please help on this. Thanks in Advance...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪眸﹌ 2025-01-21 08:04:07

如果 node-cron 不是您唯一的偏好,那么您可以使用 https:// github.com/agenda/agenda

if node-cron is not the only prefrence for you then you can use https://github.com/agenda/agenda

三月梨花 2025-01-21 08:04:07

不需要两次 require 相同的包,因此您应该删除第二行的 Cron2 。因此, var TaskTwo = Cron2.schedule('*/11 * * * *', async() => { 行应更改为:

var TaskTwo = Cron.schedule('*/11 * * * *', async() => {

至于为什么第二个计划不起作用,它可能是因为您没有 start() 计划,如 https://github.com/node-cron/node-cron#start。因此,在脚本末尾添加以下代码可能会触发两个 cron 运行:

TaskOne.start();

TaskTwo.start();

There's no need to require the same package twice, so you should remove Cron2 on the second line. Thus the line var TaskTwo = Cron2.schedule('*/11 * * * *', async() => { should be changed to:

var TaskTwo = Cron.schedule('*/11 * * * *', async() => {

As for why the second schedule is not working, it might be because you didn't start() the schedule, as shown in https://github.com/node-cron/node-cron#start. So adding the code below at the end of your script might trigger both cron to run:

TaskOne.start();

TaskTwo.start();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文