Search code examples
node.jssails.jswaterlineforeverforever-monitor

Starting a scheduling service in sails.js with forever from within sails with access to all waterline models


I have a standalone scheduling service set to execute some logic every 1 hour, I want to start this service with forever right after sails start and I am not sure what's the best way to do that.

// services/Scheduler.js
sails.load(function() {
  setInterval( logicFn , config.schedulingInterval);
});

Sails can execute bootstrap logic in the config.bootstrap module and I'll be using the forever-monitor node module \

var forever = require('forever-monitor'),
    scheduler = new (forever.Monitor)( schedulerPath, {
      max: 20,
      silent: true,
      args: []
    });

module.exports.bootstrap = function(cb) {
  scheduler.start();
  cb();
};

What if the service failed and restarted for whatever reason would it have access to all waterline models again, how to ensure it works as intended every time?


Solution

  • as brittonjb said in comments, a simple solution is to use the cron module for scheduling. You can specify a function for it to call at whatever interval you wish; this function could be defined within /config/bootstrap.js or it could be defined somewhere else (e.g. mail.dailyReminders() if you have a mail service with a dailyReminders method);