Code coverage report for lib/publisher.js

Statements: 100% (28 / 28)      Branches: 75% (3 / 4)      Functions: 100% (7 / 7)      Lines: 100% (28 / 28)      Ignored: none     

All files » lib/ » publisher.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  1 1 1 1   1 5   5 5 5         5   5 4   4   4 4     4 4 4         4       5 4 4 3   1       5 13     13       1  
"use strict";
var Promise = require('bluebird');
var amqp = require('amqplib');
var _ = require('lodash');
var util = require('util');
 
function Publisher(options, logOptions) {
  var log = require('logfilename')(__filename, logOptions);
 
  var _channel;
  var _options = options || {};
  _options = _.defaults(options, {
    type: 'direct',
    url: 'amqp://localhost'
  });
 
  log.info("Publisher options:", util.inspect(_options));
 
  Publisher.prototype.start = function() {
    log.info("start ", util.inspect(_options));
 
    return amqp.connect(_options.url)
      .then(function(conn) {
        log.info("connected to mq");
        return conn.createChannel();
      })
      .then(function(ch) {
        log.info("connected to channel");
        _channel = ch;
        return ch.assertExchange(_options.exchange, _options.type, {
          durable: true
        });
      })
      .then(function(res) {
        log.info("connected ", res);
      });
  };
 
  Publisher.prototype.stop = function() {
    log.info("stop");
    if (_channel) {
      return _channel.close();
    } else {
      return Promise.resolve();
    }
  };
 
  Publisher.prototype.publish = function(key, message) {
    log.info("publish exchange:%s, key:%s, message %s", _options.exchange,
      key,
      message);
    _channel.publish(_options.exchange, key, new Buffer(message));
  };
}
 
module.exports = Publisher;