Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | 3x 3x 5x 5x 5x 2x 1x 1x 6x 1x 3x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x | /** * @module Consumer */ const Middleware = require('../middlewares/wrapper'); const { v4 } = require('uuid'); /** * Consumer */ class Consumer { /** * посилання на amqp-manager * {@link ???} AmqpManager*/ #manager /** * @property {URL} connection - посилання на з'єднання з amqp */ #connection; /** * @property {Object} client - UUID клієнта генерується під час створення екземпляра класу */ #client; /** * @property {Object} options - налаштування Публікувальника повідомлень */ #options; /** * @property {Object} channel - посилання на екземпляр каналу */ #channel; /** * @property {Object} queue - черга для обробки повідомлень */ #queue; /** * @property {URL} middleware - посилання на ланцюжок оброблення повыдомлень * {@link use} * {@link execute} (../middlewares/wrapper.js) */ #middleware; /** * створює екземпляр отримувача * @param manager - примірник amqp-manager {@link ???}(./amqp-manager.js) * @param options - налаштування отримувача {@link options} * @property client - {@see client} * */ constructor(manager, options) { this.#manager = manager; this.#client = v4(); this.#options = options; } /** гетер для з'єднання з amqp * @returns {Object} - {@see connection} * */ get connection() { return this.#connection; } /** * гетер для UUID клієнта * @returns {Object} - {@see client} */ get client() { return this.#client; } /** * гетер для налаштування Публікувальника повідомлень * @returns {Object} - {@see client} */ get options() { return this.#options; } /** * гетер для посилання на екземпляр каналу * @returns {URL} - {@see client} */ get channel() { return this.#channel; } /** * гетер для черги для обробки повідомлень * @returns {Object} - {@see queue} */ get queue() { return this.#queue; } /** * гетер для посилання на ланцюжок оброблення повыдомлень * @returns {URL} - {@see client} */ get middleware() { return this.#middleware; } /** * Ініціалізація Consumer-залежностей */ async moduleInit() { this.#connection = await this.#manager.getConnection(this.#client, this.#options.amqp); this.#channel = await this.#connection.createChannel(); await this.#channel.assertExchange( this.#options.queue.exchange.name, this.#options.queue.exchange.mode, this.#options.queue.exchange.options, ); const assertion = await this.#channel.assertQueue( this.#options.queue.name, this.#options.queue.options, ); this.#queue = assertion.queue; Iif(!Number.isNaN(Number.parseInt(this.#options.queue.options.prefetch))) this.#channel.prefetch(this.#options.queue.options.prefetch) await this.#channel.bindQueue( this.#queue, this.#options.queue.exchange.name, '', ); this.#middleware = new Middleware(); } /** * Додає callback до ланцюжка оброблення повідомлень {@link middleware} * @param callback * */ use(callback) { this.#middleware.use(callback); return this; } /** * Запускає ланцюжок оброблення повідомлень */ async start() { try { await this.#channel.consume( this.#queue, async (msg) => { const ack = () => this.#channel.ack(msg); const nack = () => this.#channel.nack(msg); try { await this.#middleware.execute({ content: msg.content, ack, nack }); } catch (e) { throw e; } }, this.#options.queue.options ); } catch (err) { throw err; } } /** * Закриває канал по клієнту */ async close(callback) { await this.#channel.close(); Iif( callback) { await callback(this.#client); } else { this.#manager.closeConnection(this.#client) } } } module.exports = Consumer; |