UNPKG

@methodus/server

Version:
43 lines 2.24 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const container_1 = require("../../container"); const _1 = require("../../response/"); const amqp = container_1.Container.get('amqplib'); class AMQP { static connect(connectionOptions, forceReconnect) { return __awaiter(this, void 0, void 0, function* () { if (AMQP._connection && !forceReconnect) { //logger.log(this, 'reusing connection'); try { let connection = yield AMQP._connection; return connection; } catch (ex) { console.error(ex); } } try { console.log(`> connecting to amqp server at ${connectionOptions.amqp}`); let connectionString = `amqp://${connectionOptions.userName}:${connectionOptions.password}@${connectionOptions.amqp}` + '?heartbeat=30&router=' + connectionOptions.name; AMQP._connection = amqp.connect(connectionString); //first set the promise so theconnec function preseves a single connection AMQP._connection = yield AMQP._connection; //now resilve it AMQP._connection.on('error', () => { AMQP._connection = null; }); AMQP._connection.on('close', () => { AMQP._connection = null; }); return AMQP._connection; } catch (error) { throw (new _1.MethodError(error)); } }); } } exports.AMQP = AMQP; //# sourceMappingURL=amqp.js.map