<commit_before>var amqp = require('amqplib');

var amqpUrl, amqpConnection, intervalID;

function connect(_amqpUrl) {
  amqpUrl = amqpUrl || _amqpUrl || process.env.AMQP_URL || 'amqp://localhost';

  return amqp.connect(amqpUrl)
  .then(function (_connection) {
    amqpConnection = _connection;

    _connection.on('close', reconnect);
    _connection.on('error', reconnect);

    intervalID = clearInterval(intervalID);

    return _connection.createChannel()
    .then(function (_channel) {
      return _channel;
    });
  });
}<commit_msg>Add createChannel and recreateChannel and handle channel error and close event<commit_after>var amqp = require('amqplib');

var amqpUrl, amqpConnection, intervalID;

function connect(_amqpUrl) {
  amqpUrl = amqpUrl || _amqpUrl || process.env.AMQP_URL || 'amqp://localhost';

  return amqp.connect(amqpUrl)
  .then(function (_connection) {
    amqpConnection = _connection;

    _connection.on('close', reconnect);
    _connection.on('error', reconnect);

    intervalID = clearInterval(intervalID);

    return createChannel();
  });
}

function createChannel() {
  if (amqpConnection) {
    return amqpConnection.createChannel()
    .then(function (_channel) {
      _channel.on('close', recreateChannel);
      _channel.on('error', recreateChannel);

      intervalID = clearInterval(intervalID);

      return _channel;
    });
  } else {
    reconnect();
  }
}

function recreateChannel() {
  if (!intervalID) {
    intervalID = setInterval(createChannel, 1000);
  }
}
{'fraction_non_alphanumeric': 0.12928759894459102, 'fraction_numerical': 0.002638522427440633, 'mean_word_length': 4.159863945578231, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3577190', 'n_tokens_mistral': 483, 'n_tokens_neox': 461, 'n_words': 95}