|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright CERN and copyright holders of ALICE O2. This software is |
| 4 | + * distributed under the terms of the GNU General Public License v3 (GPL |
| 5 | + * Version 3), copied verbatim in the file "COPYING". |
| 6 | + * |
| 7 | + * See http://alice-o2.web.cern.ch/license for full licensing information. |
| 8 | + * |
| 9 | + * In applying this license CERN does not waive the privileges and immunities |
| 10 | + * granted to it by virtue of its status as an Intergovernmental Organization |
| 11 | + * or submit itself to any jurisdiction. |
| 12 | + */ |
| 13 | +const protobuf = require('protobufjs'); |
| 14 | +const path = require('node:path'); |
| 15 | +const { LogManager } = require('@aliceo2/web-ui'); |
| 16 | + |
| 17 | +const protoDir = path.resolve(__dirname, './../../protobuf/protos'); |
| 18 | +const root = protobuf.loadSync(path.resolve(protoDir, 'events.proto')); |
| 19 | +const EventMessage = root.lookupType('events.Event'); |
| 20 | + |
| 21 | +/** |
| 22 | + * @callback MessageReceivedCallback |
| 23 | + * @param {EventMessage} message received message |
| 24 | + * @return {Promise<void>} |
| 25 | + */ |
| 26 | + |
| 27 | +/** |
| 28 | + * Consumer that consume ECS event messages and pass them to previously-registered listeners |
| 29 | + * @author Martin Boulais <mboulais@cern.ch> |
| 30 | + * Until consumer is added in the common library, consumer was extracted from: |
| 31 | + * - https://github.com/AliceO2Group/Bookkeeping/blob/main/lib/server/kafka/AliEcsEventMessagesConsumer.js |
| 32 | + */ |
| 33 | +class AliEcsEventMessagesConsumer { |
| 34 | + /** |
| 35 | + * Constructor |
| 36 | + * |
| 37 | + * @param {import('kafkajs').Kafka} kafkaClient configured kafka client |
| 38 | + * @param {string} groupId the group id to use for the kafka consumer |
| 39 | + * @param {string[]} topics the list of topics to consume |
| 40 | + */ |
| 41 | + constructor(kafkaClient, groupId, topics) { |
| 42 | + this.consumer = kafkaClient.consumer({ groupId }); |
| 43 | + this._topics = topics; |
| 44 | + |
| 45 | + /** |
| 46 | + * @type {MessageReceivedCallback[]} |
| 47 | + * @private |
| 48 | + */ |
| 49 | + this._listeners = []; |
| 50 | + |
| 51 | + this._logger = LogManager.getLogger('cog/ecs-event-consumer'); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Register a listener to listen on event message being received |
| 56 | + * |
| 57 | + * Listeners are called all at once, not waiting for completion before calling the next ones, only errors are caught and logged |
| 58 | + * |
| 59 | + * @param {MessageReceivedCallback} listener the listener to register |
| 60 | + * @return {void} |
| 61 | + */ |
| 62 | + onMessageReceived(listener) { |
| 63 | + this._listeners.push(listener); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Start the kafka consumer |
| 68 | + * |
| 69 | + * @return {Promise<void>} Resolves once the consumer started to consume messages |
| 70 | + */ |
| 71 | + async start() { |
| 72 | + this._logger.infoMessage(`Started to listen on kafka topic ${this._topics}`); |
| 73 | + await this.consumer.connect(); |
| 74 | + await this.consumer.subscribe({ topics: this._topics }); |
| 75 | + await this.consumer.run({ |
| 76 | + eachMessage: async ({ message, topic }) => { |
| 77 | + const error = EventMessage.verify(message.value); |
| 78 | + if (error) { |
| 79 | + this._logger.errorMessage(`Received an invalid message on "${topic}" ${error}`); |
| 80 | + return; |
| 81 | + } |
| 82 | + await this._handleEvent( |
| 83 | + EventMessage.toObject( |
| 84 | + EventMessage.decode(message.value), |
| 85 | + { enums: String }, |
| 86 | + ) |
| 87 | + ); |
| 88 | + }, |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Call every registered listeners by passing the given message to it |
| 94 | + * |
| 95 | + * @param {EventMessage} message the message to pass to listeners |
| 96 | + * @return {void} |
| 97 | + */ |
| 98 | + async _handleEvent(message) { |
| 99 | + for (const listener of this._listeners) { |
| 100 | + try { |
| 101 | + await listener(message); |
| 102 | + } catch (error) { |
| 103 | + this._logger.errorMessage(`An error occurred when handling event: ${error.message}\n${error.stack}`); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +exports.AliEcsEventMessagesConsumer = AliEcsEventMessagesConsumer; |
0 commit comments