RabbitMQ Worker 崩溃问题

Posted

技术标签:

【中文标题】RabbitMQ Worker 崩溃问题【英文标题】:RabbitMQ Worker Crash Issue 【发布时间】:2015-04-12 03:38:00 【问题描述】:

我的 NodeJS rabbitmq worker 经常抛出如下异常:

events.js:72
        throw er; // Unhandled 'error' event

    Error: PRECONDITION_FAILED - unknown delivery tag 3
        at Queue._onMethod (/Users/Rana/WebstormProjects/TS/ts-worker/node_modules/amqp/amqp.js:1720:15)
        at Queue.Channel._onChannelMethod (/Users/Rana/WebstormProjects/TS/ts-worker/node_modules/amqp/amqp.js:1365:14)
        at Connection._onMethod (/Users/Rana/WebstormProjects/TS/ts-worker/node_modules/amqp/amqp.js:922:28)
        at AMQPParser.parser.onMethod (/Users/Rana/WebstormProjects/TS/ts-worker/node_modules/amqp/amqp.js:797:12)
        at AMQPParser._parseMethodFrame (/Users/Rana/WebstormProjects/TS/ts-worker/node_modules/amqp/amqp.js:442:10)
        at frameEnd (/Users/Rana/WebstormProjects/TS/ts-worker/node_modules/amqp/amqp.js:187:16)
        at frame (/Users/Rana/WebstormProjects/TS/ts-worker/node_modules/amqp/amqp.js:172:14)
        at AMQPParser.header [as parse] (/Users/Rana/WebstormProjects/TS/ts-worker/node_modules/amqp/amqp.js:159:14)
        at AMQPParser.execute (/Users/Rana/WebstormProjects/TS/ts-worker/node_modules/amqp/amqp.js:231:21)
        at Connection.<anonymous> (/Users/Rana/WebstormProjects/TS/ts-worker/node_modules/amqp/amqp.js:837:12)
        at Connection.emit (events.js:95:17)
        at Connection.<anonymous> (_stream_readable.js:765:14)
        at Connection.emit (events.js:92:17)
        at emitReadable_ (_stream_readable.js:427:10)
        at emitReadable (_stream_readable.js:423:5)
        at readableAddChunk (_stream_readable.js:166:9)
        at Connection.Readable.push (_stream_readable.js:128:10)

我的工作代码如下所示:

    connection.queue('task-queue-name', autoDelete: false, durable: true, function(queue)
       queue.subscribe(ack: true, prefetchCount: 1, function(params)
               //custom code
               queue.shift();
      );
  );

有没有人知道导致问题的我可能遗漏了什么?谢谢。

【问题讨论】:

【参考方案1】:

有(至少)两个原因我能想到为什么会发生这种情况:

您为同一条消息多次致电queue.shift() 如果您在同一个 Node 进程中的同一个队列上有多个侦听器

您可以单独确认消息,而不是使用shift

queue.subscribe( OPTS , function (message, headers, deliveryInfo, messageObject) 
  messageObject.acknowledge(false);
  // or: messageObject.reject(true|false);
);

见the documentation 和this issue。

【讨论】:

以上是关于RabbitMQ Worker 崩溃问题的主要内容,如果未能解决你的问题,请参考以下文章

重试丢失或失败的任务(Celery、Django 和 RabbitMQ)

干货实战-RabbitMQ的消息高可用和确认消费

干货实战-RabbitMQ的消息高可用和确认消费

卸载并重新安装 RabbitMQ 后无法启动 RabbitMq 服务

Laravel Queue Worker、RabbitMQ 和远程生成的运行作业

rabbitmq配置