Sails.js 后台处理循环,与连接无关 [英] Sails.js background processing loop, not related to connections

查看:24
本文介绍了Sails.js 后台处理循环,与连接无关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你有一个进程循环,你想在 setTimeout 延迟后连续运行(不管连接数),该代码将从哪里去执行?

If you have a process loop you want to run continuously after delays with setTimeout (regardless of connections) where would that code go and be executed from?

看起来代码会放在 services 目录中,但是我从哪里开始循环?我在 app.js 中尝试过,但是一旦升起帆就行不通了,它看起来不像

It looks like the code would go in the services directory, but where do I start the loop? I tried in the app.js, but that doesn't work once sails is lifted it doesn't look like

简单示例

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

推荐答案

您可以使用 config 文件夹中的 bootstrap.js (http://sailsjs.org/#!documentation/config.bootstrap):

You can use bootstrap.js in config folder (http://sailsjs.org/#!documentation/config.bootstrap):

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

这篇关于Sails.js 后台处理循环,与连接无关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆