tjmehta / coworkers

A RabbitMQ Microservice Framework in Node.js
MIT License
609 stars 36 forks source link

uncaughtException ECONNRESET #50

Closed melalj closed 8 years ago

melalj commented 8 years ago

I'm starting an amq connection and exchange following this code:

const consumer = require('./consumer'); // contain all the logic for queue consuming/coworkers
const config = require('../config');
const logger = require('./utils/logger');

let amq;
let retryCount = 0;
function start() {
  amq = consumer.init();

  return amq.connect(config.amqUrl)
  .then(() => {
    amq.connection.on('error', (e) => {
      logger.error(`Error connection AMQ - ${e.message}`);
      start();
    });
    return amq.consumerChannel.assertExchange(config.consumeExchangeName, 'direct');
  })
  .then(() => {
    return amq.consumerChannel.bindQueue(config.consumeQueueName, config.consumeExchangeName,
      'direct');
  })
  .then(() => {
    return amq.publisherChannel.assertExchange(config.publishExchangeName, 'direct');
  })
  .then(() => {
    logger.info('AMQ Started');
  })
  .catch(err => {
    if (retryCount < 6) {
      retryCount++;
      logger.error(`Error connecting to AMQ - ${retryCount} attempt`);
      return setTimeout(start, retryCount * 1000);
    }
    logger.error(err);
    return process.exit(1);
  });
}

start();
/* then some code to handle gracefully SIGINT, SIGQUITL, and SIGTERM */
error: uncaughtException: "app.connection" unexpectedly errored: read ECONNRESET date=Wed Sep 21 2016 23:41:30 GMT+0000 (UTC), pid=450, uid=0, gid=0, cwd=/app, execPath=/usr/local/bin/node, version=v4.4.7, argv=[/usr/local/bin/node, /app/index.js], rss=54706176, heapTotal=34252128, heapUsed=22584648, loadavg=[0.32421875, 0.15966796875, 0.111328125], uptime=23586, trace=[column=11, file=util.js, function=exports._errnoException, line=873, method=_errnoException, native=false, column=26, file=net.js, function=TCP.onread, line=557, method=onread, native=false], stack=[Error: "app.connection" unexpectedly errored: read ECONNRESET,     at exports._errnoException (util.js:873:11),     at TCP.onread (net.js:557:26)]

I looked at the documentation and it seems that the following lines would catch the exception:

    amq.connection.on('error', (e) => {
      logger.error(`Error connection AMQ - ${e.message}`);
      start();
    });

But it doesn't seem that this code is catching any error.

Any idea about what's happening?

tjmehta commented 8 years ago

Reading this now.

tjmehta commented 8 years ago

Just talked to @melalj via chat.

He wanted the ability to attempt reconnects on connection errors.

Coworkers was designed to crash upon receiving any connection or channel errors. This is because the coworkers application could be left in an irrecoverable state. Coworkers expects that you have a process manager (or container manager) that will restart/substitute the process when it crashes.

See where errors are thrown: https://github.com/tjmehta/coworkers/blob/master/lib/rabbit-utils/create-app-connection.js#L59 https://github.com/tjmehta/coworkers/blob/master/lib/rabbit-utils/create-app-channel.js#L79