如果您有一个进程循环,您希望在使用setTimeout(无论连接)延迟后连续运行该代码将从哪里执行?
看起来代码会进入services目录,但是我在哪里开始循环?我在app.js中尝试过,但是一旦风帆抬起它就不起作用了,它看起来不像
简单示例
// MyFoo.js
module.exports = {
shouldFoo: true,
doFoo: function(){
if(this.shouldFoo){
console.log('fooing ...');
setTimeout(foo, 1000);
} else {
this.shutdownFoo();
}
},
shutdownFoo: function(){
// finish up process
}
}
然后我在哪里放置:
var fooer = require('./api/services/MyFoo.js');
fooer.doFoo();
答案 0 :(得分:3)
您可以在bootstrap.js
文件夹(http://sailsjs.org/#!documentation/config.bootstrap)中使用config
:
module.exports.bootstrap = function (cb) {
var fooer = require('./api/services/MyFoo.js');
fooer.doFoo();
// 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();
};
答案 1 :(得分:0)
扩展bredikhin ansnwer。您可以向bootstrap.js
文件夹中的config
添加任何功能,以便在服务器上启动它。
但是如果你需要使用waterline
db访问权限,你需要在运行之前等待它解除。
module.exports.bootstrap = function(cb) {
sails.on('lifted', function(){
//Process to calculate virtualTags and update on Tags Model
VirtualTag.loopAnalytics();
});
// 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();
};