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?

2

There are 2 best solutions below

1
On BEST ANSWER

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

0
On

Please please please, always share your sails.js version number! This is really important for people googling questions/answers!

There are many ways to go about doing this. However, for those that want the "sails.js" way, there are hooks for newer sails.js versions.

See this issue thread in github, specifically, after the issue gets closed some very helpful solutions get provided by some users. The latest is shared by "scott-wyatt", commented on Dec 28, 2014:

https://github.com/balderdashy/sails/issues/2092