Node JS议程调度程序——缺少计划任务

时间:2023-02-01 02:13:32

Am Using Agenda plugin as Scheduler (along with express)

使用议程插件作为调度器(连同express)

This is my code

这是我的代码

var express = require('express');
var Agenda = require('agenda');


var agenda = new Agenda({db: { address: 'localhost:27017/express'}});
var app= express();

app.get('/notify', function(req,res){
    res.type('text/plain');
    var message = req.param('message');
    agenda.now('send reminder', {data:message});    

    res.send(message);  
});

    agenda.define('send reminder', function(job,done){
        console.log(job.attrs.data.data);
    });


agenda.start();
app.listen(80);

These are the problems am facing

这些都是目前面临的问题

  • Not all messages are getting printed
  • 并不是所有的消息都被打印出来了。
  • Some times the message is getting printed more than once.
  • 有时信息会被多次打印。
  • After 10 calls (approx), the scheduler stops forever
  • 在10次调用之后(大约),调度程序将永远停止
  • There is atleast a 2 second delay in invoking the scheduled
  • 在调用计划时至少有2秒的延迟

Some light would be appretiated

可以欣赏一些光线

2 个解决方案

#1


2  

I set up a test with Agenda as follows

我设置了一个测试议程如下

var agenda = new Agenda();
agenda.database(config.db.host + ':' + config.db.port + '/' + config.db.name, 'jobs')
var i = 0;
agenda.define('some job', function(job) {
i++;
console.log( i+ " Run at " + new Date().getMinutes() + ":" + new Date().getSeconds());
});
agenda.every('10 seconds', 'some job');
agenda.start();

Output is as follows -

输出如下-

1 Run at 56:59

1在56:59

2 Run at 57:13

2在57:13

3 Run at 57:28

3在57:28

4 Run at 57:43

4运行57:43

5 Run at 57:58

5在57:58

6 Run at 58:13

6在58:13

and so on...

等等……

I left the job running for 3 hrs and I still see it running. I haven't seen any of your first three observations. I do see a delay of ~5s for each run as you can see by the second print. This could be because of mongo latency due to the remote database I am hitting and / or due to log printing delay. I have used Agenda before and I don't think you should use it if you need to execute your job at a precision of seconds.

我离开了工作3个小时,但我仍然看到它在运行。我还没有看到你前三次观察中的任何一次。我确实看到了每一次运行的延迟,正如您可以看到的第二版。这可能是因为我正在访问的远程数据库和/或日志打印延迟导致的mongo延迟。我以前用过日程安排,如果你需要精确地执行任务,我认为你不应该用它。

As an aside, why would you use a scheduler for a now event, won't you just use console.log() when you need to log? If you need to do something else async, isn't it a simple call.

顺便说一句,为什么要为now事件使用调度程序,难道在需要日志时不使用console.log()吗?如果您需要进行其他异步调用,这不是一个简单的调用吗?

#2


2  

The problem in my code was , I forgot to call done() method

我的代码中的问题是,我忘记调用done()方法了。

It should be either

它应该是

agenda.define('send reminder', function(job,done){
        console.log(job.attrs.data.data);
done();

    });

or

agenda.define('send reminder', function(job){
        console.log(job.attrs.data.data);
    });

#1


2  

I set up a test with Agenda as follows

我设置了一个测试议程如下

var agenda = new Agenda();
agenda.database(config.db.host + ':' + config.db.port + '/' + config.db.name, 'jobs')
var i = 0;
agenda.define('some job', function(job) {
i++;
console.log( i+ " Run at " + new Date().getMinutes() + ":" + new Date().getSeconds());
});
agenda.every('10 seconds', 'some job');
agenda.start();

Output is as follows -

输出如下-

1 Run at 56:59

1在56:59

2 Run at 57:13

2在57:13

3 Run at 57:28

3在57:28

4 Run at 57:43

4运行57:43

5 Run at 57:58

5在57:58

6 Run at 58:13

6在58:13

and so on...

等等……

I left the job running for 3 hrs and I still see it running. I haven't seen any of your first three observations. I do see a delay of ~5s for each run as you can see by the second print. This could be because of mongo latency due to the remote database I am hitting and / or due to log printing delay. I have used Agenda before and I don't think you should use it if you need to execute your job at a precision of seconds.

我离开了工作3个小时,但我仍然看到它在运行。我还没有看到你前三次观察中的任何一次。我确实看到了每一次运行的延迟,正如您可以看到的第二版。这可能是因为我正在访问的远程数据库和/或日志打印延迟导致的mongo延迟。我以前用过日程安排,如果你需要精确地执行任务,我认为你不应该用它。

As an aside, why would you use a scheduler for a now event, won't you just use console.log() when you need to log? If you need to do something else async, isn't it a simple call.

顺便说一句,为什么要为now事件使用调度程序,难道在需要日志时不使用console.log()吗?如果您需要进行其他异步调用,这不是一个简单的调用吗?

#2


2  

The problem in my code was , I forgot to call done() method

我的代码中的问题是,我忘记调用done()方法了。

It should be either

它应该是

agenda.define('send reminder', function(job,done){
        console.log(job.attrs.data.data);
done();

    });

or

agenda.define('send reminder', function(job){
        console.log(job.attrs.data.data);
    });