UNPKG

@citrineos/util

Version:

The OCPP util module which supplies helpful utilities like cache and queue connectors, etc.

72 lines (71 loc) 2.91 kB
import * as amqplib from 'amqplib'; import { ILogObj, Logger } from 'tslog'; import { AbstractMessageHandler, CallAction, ICache, IModule, SystemConfig } from '@citrineos/base'; /** * Implementation of a {@link IMessageHandler} using RabbitMQ as the underlying transport. */ export declare class RabbitMqReceiver extends AbstractMessageHandler { /** * Constants */ private static readonly QUEUE_PREFIX; private static readonly CACHE_PREFIX; private static readonly RECONNECT_DELAY; /** * Fields */ protected _cache: ICache; protected _connection?: amqplib.Connection; protected _channel?: amqplib.Channel; private _reconnecting; private _abortReconnectController?; constructor(config: SystemConfig, logger?: Logger<ILogObj>, module?: IModule, cache?: ICache); initConnection(): Promise<any>; /** * Methods */ /** * Binds queue to an exchange given identifier and optional actions and filter. * Note: Due to the nature of AMQP 0-9-1 model, if you need to filter for the identifier, you **MUST** provide it in the filter object. * * @param {string} identifier - The identifier of the channel to subscribe to. * @param {CallAction[]} actions - Optional. An array of actions to filter the messages. * @param {{ [k: string]: string; }} filter - Optional. An object representing the filter to apply on the messages. * @return {Promise<boolean>} A promise that resolves to true if the subscription is successful, false otherwise. */ subscribe(identifier: string, actions?: CallAction[], filter?: { [k: string]: string; }): Promise<boolean>; unsubscribe(identifier: string): Promise<boolean>; shutdown(): Promise<void>; /** * Protected Methods */ /** * Connect to RabbitMQ with retry logic. * This method will keep trying to connect until successful, unless aborted. * * @param {AbortSignal} [abortSignal] - Optional abort signal to stop retrying. * @return {Promise<amqplib.Channel>} A promise that resolves to the AMQP channel. */ protected _connectWithRetry(abortSignal?: AbortSignal): Promise<amqplib.Channel>; /** * Setup listeners for connection and channel events. * This will handle disconnections and errors. * Ensures listeners are not attached multiple times to the same connection. */ private _setupConnectionListeners; /** * Handle RabbitMQ disconnection. * This method will attempt to reconnect to RabbitMQ when the connection is lost. * Debounces concurrent reconnects. */ private _handleDisconnect; /** * Underlying RabbitMQ message handler. * * @param message The AMQPMessage to process * @param channel */ protected _onMessage(message: amqplib.ConsumeMessage | null, channel: amqplib.Channel): Promise<void>; }