6

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.

Travis Webb
  • 14,688
  • 7
  • 55
  • 109
Manuel Darveau
  • 4,585
  • 5
  • 26
  • 36

2 Answers2

16

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)
    })
})
Jason Kulatunga
  • 5,814
  • 1
  • 26
  • 50
-1

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.

tsturzl
  • 3,089
  • 2
  • 22
  • 35
  • Thanks for your reply. I forgot to mention that we use sails for the structure AND for the waterline ORM. I need sails in my worker so I can use the waterline ORM and the common services that are defined and exposed in sails. I edited my question. – Manuel Darveau Dec 09 '14 at 05:00
  • While it might not be the most efficient thing in the world, what happens if you'd like to port some waterline model code into a worker background process? Either you do it this way (little work), or you rebuild things from scratch using non Waterline. Effort... – Chris Houghton Mar 22 '15 at 14:41
  • 1
    Take that back - I suppose it'd be better to just `require` in Waterline directly without using Sails at all. – Chris Houghton Mar 22 '15 at 14:50
  • @ChrisHoughton Exactly. Waterline is a separate project. Its difficult to do because there isn't much documentation surrounding how to use waterline separately from sails, but I couldn't imagine that it would be any harder than trying to use a completely different ORM in its place. I just don't think that sails is a good design pattern for workers. Not to mention that I think http is a poor protocol choice for workers for more reasons than I care to mention(ask if you dare). – tsturzl Mar 23 '15 at 05:22