Scheduling jobs on a Sails.js application

In one of my projects there was the need to put scheduled tasks on my Sails.js application.
Agenda and node-schedule are the tools of my choice when scheduling jobs on a node.js app. What we are gona cover is adding scheduling to our Sails.js application using node-schedule and agenda.

To get started let’s create our application

sails new SailsScheduling
cd SailsScheduling

My approach to use node-schedule is to add some configuration on the bootstrap.js file.

npm install node-schedule --save

We will add a service to our Sails.js application. Services on a Sails.js application reside on the api/services/ path.

Suppose that we implement a service that will send emails

 * Created by gkatzioura on 6/20/16.

var send = function (text,callback) {"Should send text: "+text)

module.exports =  {
  send: send

Then we add our job triggering code on bootstrap.js.

 * Bootstrap
 * (sails.config.bootstrap)
 * An asynchronous bootstrap function that runs before your Sails app gets lifted.
 * This gives you an opportunity to set up your data model, run jobs, or perform some special logic.
 * For more information on bootstrapping your app, check out:
var scheduler = require('node-schedule');

module.exports.bootstrap = function(cb) {

  // It's very important to trigger this callback method when you are finished
  // with the bootstrap!  (otherwise your server will never lift, since it's waiting on the bootstrap)

  var emailService = EmailService;

  var minuteJob  = scheduler.scheduleJob('* * * * *', function(){
    EmailService.send("Random text",function (err, result) {"Job executed")


The next example would use agenda. Instead of rolling out our own configuration we will use sails-hook-jobs which integrates wonderfully to our sails application as a grunt task.

npm install mongodb@~1.4 --save
npm install sails-hook-jobs --save

We need mongodb 1.4 version for mongo-skin.

Agenda is backed by mongodb.
For docker users you can issue

docker run --name some-mongo -d mongo

and have a mongodb server up and running.

Next step is creating the file config/jobs.js containing the configuration.

 * Default jobs configuration
 * (
 * For more information using jobs in your app, check out:
 */ = {

  // Where are jobs files
  "jobsDirectory": "api/jobs",

  // agenda configuration. 
  // for more details about configuration,
  // check
  "db": { 
    "address"    : "localhost:27017/jobs",
    "collection" : "agendaJobs" 
  "name": "process name",
  "processEvery": "10 seconds",
  "maxConcurrency": 20,
  "defaultConcurrency": 5,
  "defaultLockLifetime": 10000

Next step is to create the directory jobs on our api folder.
In order to add a job we should create a javascript source file on the api/jobs folder.
You file should have the ending Job.js. Pay special attention to this, you do not want to spend hours on figuring out what went wrong like I did.

Our job would send an email every five minutes.

module.exports = function(agenda) {
  var job = {

    frequency: 'every 5 minutes',
    run: function(job, done) {
      EmailService.send("Test email",function (err,result) {

        if(err) {
          sails.log.error("Job was not executed properly");
        } else {
"Agenda job was executed");
  return job;

All in all there are definitely more tools out there for Sails.js scheduling.
My personal choice is agenda, due to its approach on managing your jobs and integrating as a sails task.

You can find the source code on github.


One thought on “Scheduling jobs on a Sails.js application

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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