Skip to content

Instantly share code, notes, and snippets.

@PascalAnimateur
Created February 1, 2016 00:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PascalAnimateur/3d145205152e4637c078 to your computer and use it in GitHub Desktop.
Save PascalAnimateur/3d145205152e4637c078 to your computer and use it in GitHub Desktop.
Background process in Sails.js
module.exports.bootstrap = function(cb) {
sails.on('lifted', function() {
var someService = require('../api/services/SomeService.js');
// Start background process of SomeService.
someService.startBackgroundProcess();
});
// 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)
cb();
};
module.exports = {
shouldRun: true,
startBackgroundProcess: function(){
if(this.shouldRun){
console.log('Doing stuff...');
setTimeout(this.startBackgroundProcess, 1000);
} else {
this.stopBackgroundProcess();
}
},
stopBackgroundProcess: function(){
console.log('Stopped doing stuff.')
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment