|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const { Component, AsyncEmitter } = require("merapi"); |
| 4 | +const pack = require("../package"); |
| 5 | + |
| 6 | +class ServicePubQueueRabbit extends Component.mixin(AsyncEmitter) { |
| 7 | + |
| 8 | + constructor(config, logger, injector, amqp, servicePubQueue) { |
| 9 | + super(); |
| 10 | + |
| 11 | + this.config = config; |
| 12 | + this.logger = logger; |
| 13 | + this.injector = injector; |
| 14 | + this.amqp = amqp; |
| 15 | + this.servicePubQueue = servicePubQueue; |
| 16 | + |
| 17 | + this.SERVICE_NAME = config.default("name", "unnamed-service"); |
| 18 | + this.VERSION = pack.version; |
| 19 | + |
| 20 | + this._status = "ok"; |
| 21 | + this._initializing = false; |
| 22 | + this._connection = null; |
| 23 | + this._channels = {}; |
| 24 | + this._registry = {}; |
| 25 | + this._namespace = config.default("service.rabbit.namespace", "default"); |
| 26 | + |
| 27 | + this.servicePubQueue.on("triggerQueue", this.publishEvent.bind(this)); |
| 28 | + this.amqp.on("connected", () => { |
| 29 | + this.init(); |
| 30 | + }); |
| 31 | + } |
| 32 | + |
| 33 | + *initialize() { |
| 34 | + if (this.amqp.getConnection()) |
| 35 | + this.init(); |
| 36 | + } |
| 37 | + |
| 38 | + *init() { |
| 39 | + if (this._initializing) return; |
| 40 | + this._initializing = true; |
| 41 | + |
| 42 | + this._connection = this.amqp.getConnection(); |
| 43 | + |
| 44 | + let desc = this.config.default("service.queue.publish", {}); |
| 45 | + |
| 46 | + for (let service in desc) { |
| 47 | + for (let event in desc[service]) { |
| 48 | + this.createPublisher(service, event); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + Object.assign(this._registry, this.config.default("service.registry", {})); |
| 53 | + |
| 54 | + this._initializing = false; |
| 55 | + } |
| 56 | + |
| 57 | + publishEvent(service, event, payload) { |
| 58 | + let channel = this._channels[service] && this._channels[service][event]; |
| 59 | + if (channel) { |
| 60 | + let queueName = `${this._namespace}.queue.${service}.${event}`; |
| 61 | + let content = JSON.stringify(payload); |
| 62 | + channel.sendToQueue(queueName, Buffer.from(content), { persistent: true }); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + *createPublisher(service, event) { |
| 67 | + let channel = yield this._connection.createChannel(); |
| 68 | + let queueName = `${this._namespace}.queue.${service}.${event}`; |
| 69 | + |
| 70 | + if (!this._channels[service]) |
| 71 | + this._channels[service] = {}; |
| 72 | + |
| 73 | + this._channels[service][event] = channel; |
| 74 | + |
| 75 | + yield channel.assertQueue(queueName, { durable: true }); |
| 76 | + } |
| 77 | + |
| 78 | + info() { |
| 79 | + return { |
| 80 | + version: this.VERSION, |
| 81 | + status: this._status |
| 82 | + }; |
| 83 | + } |
| 84 | + |
| 85 | + status() { |
| 86 | + return this._status; |
| 87 | + } |
| 88 | + |
| 89 | + extension() { |
| 90 | + return {}; |
| 91 | + } |
| 92 | + |
| 93 | + *mount() { } |
| 94 | + |
| 95 | + *unmount() { } |
| 96 | +} |
| 97 | + |
| 98 | +module.exports = ServicePubQueueRabbit; |
0 commit comments