简体   繁体   中英

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. 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 .

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, ...).

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.

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

I wrote a full blog post about how I got background tasks working with SailsJS and Kue , but here's the main hooks part:

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 is a web framework, you're not using your worker for the web, at least not directly. Sails.js isn't what you're looking for. MVC isn't going to benefit you in this case, but you can definitely take from its paradigm.

I haven't used RabbitMQ with node.js yet, and I typically prefer redis as a message broker. I've done something similar using 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. So you definitely could make a controller to structure your logic, however Sails.js isn't the right tool.

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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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