Node.js

Scheduling jobs on Node.js with agenda

There are many ways to schedule jobs in your application. A very common practice is to back our jobs with persistence. By doing so we will be informed in the future if the job did fail or succeed and when it should be the next execution.

Agenda is a light-weight job scheduling library for node.js. It is backed with mongodb. It is really simple to get started. The first thing we have to do is to configure the mongodb database connection sting.

var Agenda = require('agenda');

var connectionString = "127.0.0.1:27017/scheduled_jobs";
var agenda = new Agenda({db: { address: connectionString, collection: 'jobs' }});

Next step would be to specify jobs.

Suppose we have an EmailService like this

EmailService = {
        send:function(callback){
            console.log("sending email");
            callback();
        }
};

Then we shall define a job

agenda.define('send email', function(job, done) {
    EmailService.send(function(err,result) {
        if(err) {
            done(err);
        } else {
            done();  
        }
    });  
});

We just defined a job with agenda in a human way.

agenda.on('ready',function() {
 agenda.every('1 day','send email'); 
 agenda.start();
});

Once we defined the jobs we need to set the time interval that the agenda instance will look up for new jobs.

agenda.processEvery('1 minute');

By querying to our mongodb database we are able to receive our job status.

>db.jobs.find({})
{ "_id" : ObjectId("5767110c779be08d4e1b3109"), "name" : "send email", "type" : "single", "data" : null, "priority" : 0, "repeatInterval" : "1 day", "repeatTimezone" : null, "lastModifiedBy" : null, "nextRunAt" : ISODate("2016-06-20T21:39:24.931Z"), "lockedAt" : null, "lastRunAt" : ISODate("2016-06-19T21:39:24.931Z"), "lastFinishedAt" : ISODate("2016-06-19T21:39:24.932Z") }

Agenda is pretty featureful. For example you can use it with cron format too.

For example running our job every minute

agenda.on('ready',function() {
 agenda.every('* * * * *','send email');
 agenda.start();
});

Other cool features is retrieving jobs in a mongodb query format and modifying them.

agenda.jobs({name: 'send email'}, function(err, jobs) {
});

Last but not least agenda-ui is a great tool for visualizing agenda jobs.

Overall agenda is my personal favorite when it comes to adding josb to my node.js application. It is backed by mongodb and easy to configure. I believe that one of its main strengths is giving you good control over your jobs.

Reference: Scheduling jobs on Node.js with agenda from our WCG partner Emmanouil Gkatziouras at the gkatzioura blog.

Emmanouil Gkatziouras

He is a versatile software engineer with experience in a wide variety of applications/services.He is enthusiastic about new projects, embracing new technologies, and getting to know people in the field of software.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button