UNPKG

@imqueue/core

Version:

Simple JSON-based messaging queue for inter service communication

195 lines (194 loc) 6.24 kB
/*! * Clustered messaging queue over Redis implementation * * I'm Queue Software Project * Copyright (C) 2025 imqueue.com <support@imqueue.com> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * * If you want to use this code in a closed source (commercial) project, you can * purchase a proprietary commercial license. Please contact us at * <support@imqueue.com> to get commercial licensing options. */ import { EventEmitter } from 'events'; import { EventMap, ILogger, IMessageQueue, IMessageQueueConnection, IMQMode, IMQOptions, IServerInput, JsonObject, RedisQueue } from '.'; interface ClusterServer extends IMessageQueueConnection { imq?: RedisQueue; } /** * Class ClusteredRedisQueue * Implements the possibility to scale queues horizontally between several * redis instances. */ export declare class ClusteredRedisQueue implements IMessageQueue, EventEmitter<EventMap> { name: string; /** * Logger instance associated with this queue instance * * @type {ILogger} */ logger: ILogger; /** * RedisQueue instances collection * * @type {RedisQueue[]} */ private imqs; /** * Options associated with this queue instance * * @type {IMQOptions} */ private readonly options; /** * Part of options without cluster definitions - which are generic for * RedisQueue instances * * @type {IMQOptions} */ private readonly mqOptions; /** * Cluster servers option definitions * * @type {IMessageQueueConnection[]} */ private servers; /** * Current queue index (round-robin) * * @type {number} */ private currentQueue; /** * Total length of RedisQueue instances * * @type {number} */ private imqLength; /** * Template EventEmitter instance used to replicate queue EventEmitters when * dynamically modifying the cluster * @type {EventEmitter} * @private */ private readonly templateEmitter; /** * Cluster EventEmitter instance used to notify about changes of * cluster servers * @type {EventEmitter} * @private */ private readonly clusterEmitter; private state; private initializedClusters; /** * Class constructor * * @constructor * @param {string} name * @param {Partial<IMQOptions>} options * @param {IMQMode} [mode] */ constructor(name: string, options?: Partial<IMQOptions>, mode?: IMQMode); /** * Starts the messaging queue. * Supposed to be an async function. * * @returns {Promise<ClusteredRedisQueue>} */ start(): Promise<ClusteredRedisQueue>; /** * Stops the queue (should stop handling queue messages). * Supposed to be an async function. * * @returns {Promise<ClusteredRedisQueue>} */ stop(): Promise<ClusteredRedisQueue>; /** * Sends a message to given queue name with the given data. * Supposed to be an async function. * * @param {string} toQueue - queue name to which message should be sent to * @param {JsonObject} message - message data * @param {number} [delay] - if specified, a message will be handled in the * target queue after a specified period of time in milliseconds. * @param {(err: Error) => void} [errorHandler] - callback called only when * internal error occurs during message send execution. * @returns {Promise<string>} - message identifier */ send(toQueue: string, message: JsonObject, delay?: number, errorHandler?: (err: Error) => void): Promise<string>; /** * Safely destroys the current queue, unregistered all set event * listeners and connections. * Supposed to be an async function. * * @returns {Promise<void>} */ destroy(): Promise<void>; /** * Clears queue data in queue host application. * Supposed to be an async function. * * @returns {Promise<ClusteredRedisQueue>} */ clear(): Promise<ClusteredRedisQueue>; queueLength(): Promise<number>; /** * Batch imq action processing on all registered imqs at once * * @access private * @param {string} action * @param {string} message * @return {Promise<this>} */ private batch; on(...args: any[]): this; off(...args: any[]): this; once(...args: any[]): this; addListener(...args: any[]): this; removeListener(...args: any[]): this; removeAllListeners(...args: any[]): this; prependListener(...args: any[]): this; prependOnceListener(...args: any[]): this; setMaxListeners(...args: any[]): this; listeners(...args: any[]): any[]; rawListeners(...args: any[]): any[]; getMaxListeners(): number; emit(...args: any[]): boolean; eventNames(...args: any[]): any; listenerCount(...args: any[]): any; publish(data: JsonObject, toName?: string): Promise<void>; subscribe(channel: string, handler: (data: JsonObject) => any): Promise<void>; unsubscribe(): Promise<void>; /** * Adds new servers to the cluster * * @param {IServerInput} server * @returns {void} */ protected addServer(server: IServerInput): ClusterServer; /** * Removes server from the cluster * * @param {IServerInput} server * @returns {void} */ protected removeServer(server: IServerInput): void; private addServerWithQueueInitializing; private eventEmitters; private initializeQueue; private findServer; private static matchServers; } export {};