UNPKG

gaia-rabbitmq

Version:

RabbitMQ wrapper for gaia workflow system

154 lines (114 loc) 5.52 kB
'use strict'; var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; } var _amqp = require('amqp'); var _amqp2 = _interopRequireDefault(_amqp); var _events = require('events'); var _events2 = _interopRequireDefault(_events); var _debug = require('debug'); var _debug2 = _interopRequireDefault(_debug); var _nodeUuid = require('node-uuid'); var _nodeUuid2 = _interopRequireDefault(_nodeUuid); var _clone = require('clone'); var _clone2 = _interopRequireDefault(_clone); var _async = require('async'); var _async2 = _interopRequireDefault(_async); var DEFAULT_HOST = 'localhost'; var DEFAULT_PORT = 5672; var log = (0, _debug2['default'])('gaia:wf:GaiaRabbitMQ'); var GaiaRabbitMQ = (function (_events$EventEmitter) { function GaiaRabbitMQ(exchange, opts) { _classCallCheck(this, GaiaRabbitMQ); _get(Object.getPrototypeOf(GaiaRabbitMQ.prototype), 'constructor', this).call(this); var conOpts = (0, _clone2['default'])(opts, true, 1); this.exchange = '' + exchange; this.queues = []; conOpts.host = conOpts.host || DEFAULT_HOST; conOpts.port = conOpts.port || DEFAULT_PORT; this.conOpts = conOpts; this.connected = false; this.on('ready', (function () { log(exchange + ' ready'); }).bind(this)); this.on('close', function () { log(exchange + ' closed'); }); } _inherits(GaiaRabbitMQ, _events$EventEmitter); _createClass(GaiaRabbitMQ, [{ key: 'connect', value: function connect() { var _this = this; this.connection = _amqp2['default'].createConnection(this.conOpts, {}); this.connection.on('error', (function (err) { log('ConnectionFailed: ' + err); _this.emit('failed', new Error('ConnectionFailed')); }).bind(this)); this.connection.on('close', function () { log('closed'); }); this.connection.on('ready', (function () { _this.connected = true; _this.emit('open'); _this.connection.exchange(_this.exchange, {}, (function (e) { e.on('error', function (err) { log('exchangError: ' + err); }); _this.exc = e; _this.emit('ready'); }).bind(_this)); }).bind(this)); } }, { key: 'publish', value: function publish(router, msg) { log(this.exchange + '-' + router + ' publish: ' + JSON.stringify(msg)); this.exc.publish(router, msg); } }, { key: 'subscribe', value: function subscribe(router, fn) { var _this2 = this; if (typeof router !== 'string' || typeof fn !== 'function') { return false; } var queue = 'q-' + this.exchange + '-' + _nodeUuid2['default'].v4(); this.connection.queue(queue, {}, (function (q) { log(_this2.exchange + ' use ' + queue + ': bind ' + router); _this2.queues.push(q); q.bind(_this2.exchange, router); q.subscribe(fn); log(_this2.exchange + ' subscribed ' + router); }).bind(this)); } }, { key: 'cleanup', value: function cleanup() { if (this.connected) { this.exc.destroy(); this.queues.forEach(function (q) { q.destroy(); }); } this.emit('cleanup'); } }, { key: 'close', value: function close(err) { if (this.connected) { this.exc.destroy(); this.queues.forEach(function (q) { q.destroy(); }); } this.connection.disconnect(); this.connected = false; } }]); return GaiaRabbitMQ; })(_events2['default'].EventEmitter); module.exports = GaiaRabbitMQ; //# sourceMappingURL=../lib/GaiaRabbitMQ.js.map