@goparrot/pubsub-event-bus
Version:
NestJS EventBus extension for RabbitMQ PubSub
78 lines • 3.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PubsubManager = void 0;
const tslib_1 = require("tslib");
const common_1 = require("@nestjs/common");
const RabbitManager = tslib_1.__importStar(require("amqp-connection-manager"));
const lodash_1 = require("lodash");
const provider_1 = require("../provider");
const configuration_1 = require("../utils/configuration");
class PubsubManager {
get connection() {
if (!this.connection$) {
throw new Error('Amqp connection has not been initialized');
}
return this.connection$;
}
get channelWrapper() {
if (!this.channelWrapper$) {
throw new Error('Amqp channel has not been initialized');
}
return this.channelWrapper$;
}
async setupChannel(_channel) { }
initConnectionIfRequired() {
var _a;
if (this.connection$) {
return;
}
const options = (0, lodash_1.cloneDeep)(this.connectionManagerOptions);
if (!((_a = options.connectionOptions) === null || _a === void 0 ? void 0 : _a.clientProperties.connection_name) && this.connectionName) {
const connectionName = `${this.connectionName}:${this.constructor.name.toLowerCase()}`;
(0, lodash_1.set)(options, 'connectionOptions.clientProperties.connection_name', connectionName);
}
this.connection$ = RabbitManager.connect(this.urls, options)
.on('connect', () => this.logger().log('Amqp connection established', this.constructor.name))
.on('disconnect', (arg) => this.logger().error(arg.err.message, undefined, this.constructor.name))
.on('connectFailed', (arg) => this.logger().error(arg.err.message, undefined, this.constructor.name))
.on('blocked', (arg) => this.logger().error(`Connection blocked, ${arg.reason}`, undefined, this.constructor.name))
.on('unblocked', () => this.logger().log('Connection unblocked', this.constructor.name));
}
initChannelIfRequired() {
if (this.channelWrapper$) {
return;
}
this.channelWrapper$ = this.connection
.createChannel({ json: true, setup: this.setupChannel.bind(this) })
.on('connect', () => this.logger().log(`Amqp channel created`, this.constructor.name))
.on('error', (err, { name }) => this.logger().error(`Amqp channel error: ${err.message}`, err.stack, name !== null && name !== void 0 ? name : this.constructor.name))
.on('close', () => this.logger().log(`Amqp channel closed`, this.constructor.name));
}
async onModuleDestroy() {
var _a, _b;
await ((_a = this.channelWrapper$) === null || _a === void 0 ? void 0 : _a.close());
await ((_b = this.connection$) === null || _b === void 0 ? void 0 : _b.close());
}
logger() {
return provider_1.LoggerProvider.logger;
}
}
exports.PubsubManager = PubsubManager;
tslib_1.__decorate([
(0, common_1.Inject)(configuration_1.CQRS_CONNECTION_NAME),
(0, common_1.Optional)(),
tslib_1.__metadata("design:type", String)
], PubsubManager.prototype, "connectionName", void 0);
tslib_1.__decorate([
(0, common_1.Inject)(configuration_1.CQRS_CONNECTION_URLS),
tslib_1.__metadata("design:type", Object)
], PubsubManager.prototype, "urls", void 0);
tslib_1.__decorate([
(0, common_1.Inject)(configuration_1.CQRS_EXCHANGE_CONFIG),
tslib_1.__metadata("design:type", Object)
], PubsubManager.prototype, "assertExchangeOptions", void 0);
tslib_1.__decorate([
(0, common_1.Inject)(configuration_1.CQRS_CONNECTION_MANAGER_OPTIONS),
tslib_1.__metadata("design:type", Object)
], PubsubManager.prototype, "connectionManagerOptions", void 0);
//# sourceMappingURL=PubsubManager.js.map