UNPKG

@nestjstools/messaging

Version:

Simplifies asynchronous and synchronous message handling with support for buses, handlers, channels, and consumers. Build scalable, decoupled applications with ease and reliability.

95 lines 6 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DistributedConsumer = void 0; const common_1 = require("@nestjs/common"); const core_1 = require("@nestjs/core"); const service_1 = require("../dependency-injection/service"); const channel_registry_1 = require("../channel/channel.registry"); const in_memory_channel_1 = require("../channel/in-memory.channel"); const decorator_1 = require("../dependency-injection/decorator"); const consumer_message_mediator_1 = require("./consumer-message-mediator"); const default_message_options_1 = require("../message/default-message-options"); const consumer_dispatched_message_error_1 = require("./consumer-dispatched-message-error"); const sealed_routing_message_1 = require("../message/sealed-routing-message"); const log_1 = require("../logger/log"); const handlers_exception_1 = require("../exception/handlers.exception"); const exception_listener_handler_1 = require("../exception-listener/exception-listener-handler"); const exception_context_1 = require("../exception-listener/exception-context"); let DistributedConsumer = class DistributedConsumer { constructor(messageBus, channelRegistry, exceptionListenerHandler, logger, discoveryService) { this.messageBus = messageBus; this.channelRegistry = channelRegistry; this.exceptionListenerHandler = exceptionListenerHandler; this.logger = logger; this.discoveryService = discoveryService; } async run() { for (const channel of this.channelRegistry.getAll()) { if (channel instanceof in_memory_channel_1.InMemoryChannel || false === channel.config.enableConsumer) { continue; } const channelConsumer = this.discoveryService .getProviders() .filter((provider) => { if (!provider.metatype) { return false; } return Reflect.hasMetadata(decorator_1.MESSAGE_CONSUMER_METADATA, provider.metatype); }) .filter((consumer) => Reflect.getMetadata(decorator_1.MESSAGE_CONSUMER_METADATA, consumer.metatype) .name === channel.constructor.name); if (channelConsumer.length !== 1) { throw new Error(`Consumer for channel ${channel.constructor.name} does not found`); } const mediator = new consumer_message_mediator_1.ConsumerMessageMediator(); const consumer = channelConsumer[0].instance; await consumer.consume(mediator, channel); mediator.listen().subscribe(async (consumerMessage) => { try { this.logger.debug(log_1.Log.create(`[${channel.config.name}] Message handled with routing key: [${consumerMessage.routingKey}]`, { message: JSON.stringify(consumerMessage.message), })); const middlewares = channel.config .middlewares; const routingMessage = new sealed_routing_message_1.SealedRoutingMessage(consumerMessage.message, consumerMessage.routingKey).createWithOptions(new default_message_options_1.DefaultMessageOptions(middlewares, channel.config?.avoidErrorsForNotExistedHandlers ?? true, channel.config.normalizer)); await this.messageBus.dispatch(routingMessage); } catch (e) { await consumer.onError(new consumer_dispatched_message_error_1.ConsumerDispatchedMessageError(consumerMessage, e), channel); if (!(e instanceof handlers_exception_1.HandlersException)) { this.logger.error(log_1.Log.create(`Some error occurred in channel [${channel.config.name}]`, { error: e instanceof Error ? e.message : String(e), message: JSON.stringify(consumerMessage.message), routingKey: consumerMessage.routingKey, })); } await this.exceptionListenerHandler.handleError(new exception_context_1.ExceptionContext(e, channel.config.name, consumerMessage.message, consumerMessage.routingKey)); } }); this.logger.log(`Consumer for channel [${channel.config.name}] is ready to handle messages`); } } }; exports.DistributedConsumer = DistributedConsumer; exports.DistributedConsumer = DistributedConsumer = __decorate([ __param(0, (0, common_1.Inject)(service_1.Service.DEFAULT_MESSAGE_BUS)), __param(1, (0, common_1.Inject)(service_1.Service.CHANNEL_REGISTRY)), __param(2, (0, common_1.Inject)(service_1.Service.EXCEPTION_LISTENER_HANDLER)), __param(3, (0, common_1.Inject)(service_1.Service.LOGGER)), __metadata("design:paramtypes", [Object, channel_registry_1.ChannelRegistry, exception_listener_handler_1.ExceptionListenerHandler, Object, core_1.DiscoveryService]) ], DistributedConsumer); //# sourceMappingURL=distributed.consumer.js.map