Code coverage report for lib/subscriber.js

Statements: 100% (51 / 51)      Branches: 100% (4 / 4)      Functions: 100% (14 / 14)      Lines: 100% (51 / 51)      Ignored: none     

All files » lib/ » subscriber.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96  1 1 1 1 1   1 7 7 7 7   7         7   7 4     7 5 5   5 5     5 5 5         5 5         5 5 5       5 5     5       7 6 6 5   1       7 12 12     7 1 1     7 6 6 5   1 1       1 14 14       1  
'use strict';
var Promise = require('bluebird');
var amqp = require('amqplib');
var _ = require('lodash');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
 
function Subscriber(options, logOptions) {
  var log = require('logfilename')(__filename, logOptions);
  var _channel;
  var _queue;
  var eventEmitter = new EventEmitter();
 
  var _options = _.defaults(options, {
    type: 'direct',
    url: 'amqp://localhost'
  });
 
  log.info('Subscriber options:', util.inspect(options));
 
  Subscriber.prototype.getEventEmitter = function() {
    return eventEmitter;
  };
 
  Subscriber.prototype.start = function() {
    log.info('start');
    return amqp.connect(_options.url)
      .then(function(conn) {
        log.info('createChannel');
        return conn.createChannel();
      })
      .then(function(channel) {
        _channel = channel;
        log.info('assertExchange ', _options.exchange);
        return channel.assertExchange(_options.exchange, _options.type, {
          durable: true
        });
      })
      .then(function() {
        log.info('assertQueue name: ', _options.queueName);
        return _channel.assertQueue(_options.queueName, {
          exclusive: false
        });
      })
      .then(function(res) {
        log.info('bind queue %s, key: %s', res.queue, _options.key);
        _queue = res.queue;
        return _channel.bindQueue(_queue, _options.exchange, _options.key);
      })
      .then(function() {
        //TODO ack
        _channel.prefetch(1);
        return _channel.consume(_queue, onIncomingMessage);
      })
      .then(function() {
        log.info('started');
      });
  };
 
  Subscriber.prototype.stop = function() {
    log.info('stop');
    if (_channel) {
      return _channel.close();
    } else {
      return Promise.resolve();
    }
  };
 
  Subscriber.prototype.ack = function(message) {
    log.debug('ack');
    _channel.ack(message);
  };
 
  Subscriber.prototype.nack = function(message) {
    log.debug('nack');
    _channel.nack(message);
  };
 
  Subscriber.prototype.purgeQueue = function() {
    log.info('purgeQueue ', _queue);
    if (_channel) {
      return _channel.purgeQueue(_queue);
    } else {
      log.error('purgeQueue: channel not opened');
      return Promise.resolve();
    }
  };
 
  function onIncomingMessage(message) {
    log.debug('onIncomingMessage ', message.fields);
    eventEmitter.emit('message', message);
  }
}
 
module.exports = Subscriber;