简体   繁体   English

没有http端点的Sails.js工作节点

[英]Sails.js worker node without http endpoint

I am building a sails app that uses a RabbitMQ do delegate some tasks from the web requests to a worker node. 我正在构建一个使用RabbitMQ的sails应用程序,将Web请求中的一些任务委派给工作节点。 This is pretty much the pattern described in https://devcenter.heroku.com/articles/background-jobs-queueing and https://github.com/heroku-examples/node-articles-nlp . 这几乎是https://devcenter.heroku.com/articles/background-jobs-queueinghttps://github.com/heroku-examples/node-articles-nlp中描述的模式。

While I could do a sails.lift() in the worker node, it seems that it would be better to skip the http endpoint (express) and some grunt tasks (bower/frontend dependencies download, less, web resources copy to .tmp, ...). 虽然我可以在工作节点中执行sails.lift(),但似乎最好跳过http端点(快速)和一些grunt任务(bower / frontend依赖下载,更少,web资源复制到.tmp, ...)。

Is there any way to achieve that? 有没有办法实现这一目标?

Thanks! 谢谢!

Edit 编辑

I need sails in my worker so I can use the waterline ORM and the common services that are defined and exposed in sails. 我需要在我的工作人员中使用帆,这样我就可以使用水线ORM以及在帆中定义和暴露的公共服务。

If you want to use the Sails ORM without the webserver and other web related components, you can use Sails Hooks to configure a minimal application 如果要在没有Web服务器和其他Web相关组件的情况下使用Sails ORM,可以使用Sails Hook配置最小的应用程序

I wrote a full blog post about how I got background tasks working with SailsJS and Kue , but here's the main hooks part: 我写了一篇关于如何使用SailsJS和Kue进行后台任务的完整博客文章,但这里是主要的钩子部分:

require('sails').load({
    hooks: {
        blueprints: false,
        controllers: false,
        cors: false,
        csrf: false,
        grunt: false,
        http: false,
        i18n: false,
        logger: false,
        //orm: leave default hook
        policies: false,
        pubsub: false,
        request: false,
        responses: false,
        //services: leave default hook,
        session: false,
        sockets: false,
        views: false
    }
}, function(err, app){

    //You can access all your SailsJS Models and Services here
    User.findOne(1).then(function(user){
        console.log(user)
    })
})

What exactly would you benefit from using sails.js in your worker node? 在工作节点中使用sails.js会对您有什么好处? Sails.js is a web framework, you're not using your worker for the web, at least not directly. Sails.js是一个Web框架,您不是将您的工作者用于Web,至少不是直接使用。 Sails.js isn't what you're looking for. Sails.js不是你想要的。 MVC isn't going to benefit you in this case, but you can definitely take from its paradigm. 在这种情况下,MVC不会让你受益,但你绝对可以从它的范例中获益。

I haven't used RabbitMQ with node.js yet, and I typically prefer redis as a message broker. 我还没有将RabbitMQ用于node.js,我通常更喜欢redis作为消息代理。 I've done something similar using kue . 我用kue做了类似的事。 Kue is really geared towards this kind of task, and you can essentially define jobs much like you'd define a route in Express. Kue真的面向这种任务,你基本上可以定义工作,就像你在Express中定义路线一样。 So you definitely could make a controller to structure your logic, however Sails.js isn't the right tool. 所以你绝对可以制作一个控制器来构建你的逻辑,但是Sails.js不是正确的工具。

If you're decision for using Sails.js is just because of its generator you can definitely get your hands dirty with grunt and yeoman. 如果你决定使用Sails.js只是因为它的生成器,你绝对可以用咕噜咕噜和自耕农来弄脏你。 Wouldn't be very difficult. 不会很困难。 Another concept is to just integrate your workers into your web nodes, and just limit how many jobs run on each worker. 另一个概念是将您的工作人员集成到您的Web节点中,并限制每个工作人员运行的作业数量。 Kue supports this, and I've had good luck with this, you just have to make sure you're not doing lots of processing or any processing that may take a long time as you might start timing out on that web node. Kue支持这一点,而且我对此有好运,你只需要确保你没有进行大量处理或任何可能需要很长时间的处理,因为你可能会开始在该网络节点上超时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM