如何将心跳信号分离到其自己的线程以进行RabbitMQ连接

时间:2019-01-31 20:06:07

标签: node.js rabbitmq node-cluster node-amqplib

我有一个使用的RabbitMQ和的NodeJS做图象处理的处理。由于密集的任务,我想我有同样的问题,这里的链接https://github.com/squaremo/amqp.node/issues/261

我正在尝试弄清楚如何执行关于该问题的最新评论。

“是的,是的NodeJS单线程的,如果你使用线程很长一段时间做一些事情,什么都没发生。作为@michaelklishin表明,对于这个普遍问题的已知的解决方案是使用一个子进程,或群集模块。“

编辑:

我更新了代码下面的我怎样想可以与AMQP的连接管理器模块执行此操作的样品。现在,我使用一个全局变量来保存实际的消息以便确认。我猜有一个更好的方式来做到这一点。

//Used to be an example for how to keep the connection thread and the working thread separate
//in order to fix the issue of missing heartbeat intervals due to processing on the same thread

const cluster = require('cluster');
var amqp = require('amqp-connection-manager');
var config = require('./config.json');

var working_queue = "Test_Queue";

//DONT REALLY DO THIS
var rabbit_msg_data;


//******* CLUSTER SETUP *******
// This will spawn off the number of worker for this process.
if (cluster.isMaster) {
    console.log("Master "+process.pid+" is running");

    worker = cluster.fork();

    cluster.on('exit', (worker, code, signal) => {
        if(signal)
        {
            console.log("worker was killed by signal: "+signal);
            console.log(worker);
        }
        else if (code !== 0)
        {
            console.log("worker exited with error code: "+code);
            console.log(worker);
        }
        else
        {
            console.log("Worker "+worker.process.pid+" exited successfully");
            console.log(worker);
            //Not sure if this works this way or if I need to put this worker into variables
        }
    });

    //testing sending a message back and forth
    // setTimeout(function() {
    //  worker.send("I got a request!");
    // }, 1000);

    //******** RABBIT MQ CONNECTION **********

    // Create a connection manager to rabbitmq
    var connection = amqp.connect(config.rabbit_connections_arr, {json: true, heartbeatIntervalInSeconds: 2});
    connection.on('connect', function() {
        console.log('Connected to rabbitmq');
    });
    connection.on('disconnect', function(params) {
        console.log('Disconnected from rabbitmq:', params.err.stack);
    });

    // Set up a channel listening for messages in the queue.
    var channelWrapper_listening = connection.createChannel({
        setup: function(channel) {
            // `channel` here is a regular amqplib `ConfirmChannel`.
            return Promise.all([
                channel.assertQueue(working_queue, {durable: true}),
                channel.prefetch(1),
                channel.consume(working_queue, function(data){
                    rabbit_msg_data = data;
                    worker.send(data.content.toString());             
                }, requeue = false)
            ]);
        }
    });


    worker.on('message', function(msg){
        // console.log("Worker to Master (ack): ", msg.content.toString());
        console.log("Worker to Master (ack): ", msg);
        //console.log(msg.content.toString());
        channelWrapper_listening.ack(rabbit_msg_data);
    });



}
else //All worker processes (MAIN LOGIC)
{
    console.log("Worker "+process.pid+" started");

    process.on('message',function(msg){
        console.log("Master to Worker (working): ", msg);

        //send msg back when done working on it.
        setTimeout(function() {
            process.send(msg);
        }, 5000);
    });
}

0 个答案:

没有答案
相关问题