UNPKG

@midwayjs/rabbitmq

Version:

Midway Framework for rabbitmq

125 lines 6.17 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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MidwayRabbitMQFramework = void 0; const core_1 = require("@midwayjs/core"); const mq_1 = require("./mq"); let MidwayRabbitMQFramework = class MidwayRabbitMQFramework extends core_1.BaseFramework { configure() { return this.configService.getConfiguration('rabbitmq'); } async applicationInitialize(options) { const traceService = this.applicationContext.get(core_1.MidwayTraceService); // Create a connection manager this.app = new mq_1.RabbitMQServer({ logger: this.logger, traceService, ...this.configurationOptions, }); } async run() { try { // init connection await this.app.connect(this.configurationOptions.url, this.configurationOptions.socketOptions); await this.loadSubscriber(); this.logger.info('Rabbitmq server start success'); } catch (error) { this.app.close(); throw error; } } async beforeStop() { await this.app.close(); } async loadSubscriber() { // create channel const subscriberModules = core_1.DecoratorManager.listModule(core_1.MS_CONSUMER_KEY, module => { const metadata = core_1.MetadataManager.getOwnMetadata(core_1.MS_CONSUMER_KEY, module); return metadata.type === core_1.MSListenerType.RABBITMQ; }); for (const module of subscriberModules) { const data = (0, core_1.listPropertyDataFromClass)(core_1.MS_CONSUMER_KEY, module); for (const methodBindListeners of data) { // 循环绑定的方法和监听的配置信息 for (const listenerOptions of methodBindListeners) { await this.app.createConsumer(listenerOptions, async (data, channel, channelWrapper) => { const ctx = { data, channel, queueName: listenerOptions.queueName, ack: data => { return channelWrapper.ack(data); }, }; const traceService = this.applicationContext.get(core_1.MidwayTraceService); const traceMetaResolver = this.configurationOptions ?.tracing?.meta; const traceEnabled = this.configurationOptions?.tracing?.enable !== false; const traceExtractor = this.configurationOptions?.tracing ?.extractor; const headersDefault = data?.properties?.headers ?? {}; const headers = typeof traceExtractor === 'function' ? traceExtractor({ ctx, request: data, custom: { queueName: listenerOptions.queueName }, }) : headersDefault; await traceService.runWithEntrySpan(`rabbitmq ${listenerOptions.queueName}`, { enable: traceEnabled, carrier: headers ?? headersDefault, attributes: { 'midway.protocol': 'rabbitmq', 'midway.rabbitmq.queue': listenerOptions.queueName, }, meta: traceMetaResolver, metaArgs: { ctx, carrier: headers ?? headersDefault, request: data, custom: { queueName: listenerOptions.queueName, }, }, }, async () => { this.app.createAnonymousContext(ctx); const isPassed = await this.app .getFramework() .runGuard(ctx, module, listenerOptions.propertyKey); if (!isPassed) { throw new core_1.MidwayInvokeForbiddenError(listenerOptions.propertyKey, module); } const ins = await ctx.requestContext.getAsync(module); const fn = await this.applyMiddleware(async (ctx) => { return await ins[listenerOptions.propertyKey].call(ins, data); }); try { const result = await fn(ctx); if (result) { return channelWrapper.ack(data); } } catch (error) { this.logger.error(error); } }); }); } } } } getFrameworkName() { return 'rabbitmq'; } }; exports.MidwayRabbitMQFramework = MidwayRabbitMQFramework; exports.MidwayRabbitMQFramework = MidwayRabbitMQFramework = __decorate([ (0, core_1.Framework)() ], MidwayRabbitMQFramework); //# sourceMappingURL=framework.js.map