Creating a Celery worker using node.js

馋奶兔 提交于 2019-11-29 06:08:00

问题


Using node-celery, we can enable node to push Celery jobs to the task queue. How can we allow node to be a Celery worker and consume the queue?


回答1:


For Celery if the end point is amqp. Checkout Celery.js Github any node process started as amqp consumer would work fine. For every other self.conf.backend_type types you can have varied consumer. Following example is merely for amqp.

One such example. The message below may be the Celery task object.

var amqp = require('amqp');
var connection = amqp.createConnection({ host: "localhost", port: 5672 });
connection.on('ready', function () {
  connection.queue("my_celery_queue", function(queue){
    queue.bind('#'); 
    queue.subscribe(function (message) {
      //eat your Celery work here
    })
  })
})



回答2:


Here is an approach from the Celery doc, by exposing REST api:

http://docs.celeryproject.org/en/latest/faq.html#is-celery-multilingual

Also, there’s another way to be language-independent, and that’s to use REST tasks, instead of your tasks being functions, they’re URLs. With this information you can even create simple web servers that enable preloading of code. Simply expose an endpoint that performs an operation, and create a task that just performs an HTTP request to that endpoint.

some example: http://ask.github.io/celery/cookbook/remote-tasks.html



来源:https://stackoverflow.com/questions/21211848/creating-a-celery-worker-using-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!