Question

Am Using Agenda plugin as Scheduler (along with 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
  • There is atleast a 2 second delay in invoking the scheduled

Some light would be appretiated

Was it helpful?

Solution

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

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);
    });

OTHER TIPS

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

2 Run at 57:13

3 Run at 57:28

4 Run at 57:43

5 Run at 57:58

6 Run at 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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top