import { IInputProvider, ILogger } from '../../types'; import { ICoreAmqpMessage } from '../types/amqp-types'; import AmqpCacoon, { ConsumeMessage, ChannelWrapper } from 'amqp-cacoon'; export interface ICoreInputAmqpProviderOptions { ackMode?: 'auto' | 'callback'; requeueOnNack?: boolean; ackCallback?: (rulesEngineOutput: { facts: any; error: unknown; }, msg: ICoreAmqpMessage | undefined, channel: { ack: (options: { allUpTo?: boolean; }) => void; nack: (options: { requeue?: boolean; allUpTo?: boolean; }) => void; }) => Promise; inputContextCallback?: (msg: ConsumeMessage) => void; } /** * Rule Input Provider Amqp * * This class wires up the AmqpCacoon to a rule input provider. * * Usage: * 1. Instantiate the class and pass in the instantiated amqpCacoon and logger * 2. call registerHandler to register the input callback * 3. When input comes in, the handler registered in step 2 will be called * **/ export default class CoreInputAmqp implements IInputProvider { private handler; private alreadyRegistered; private logger?; private amqpCacoon; private amqpQueue; private options; /** * constructor * * This function sets class level variables. * * @param amqpCacoon - an instance of AMQP Cacoon which will manage all AMQP communications. * @param amqpQueue - a string with the name of the queue to consume from. * @param logger - a logger instance to use for logging. * @param options - options for the behavior of the provider. **/ constructor(amqpCacoon: AmqpCacoon, amqpQueue: string, logger: ILogger | undefined, options: ICoreInputAmqpProviderOptions); /** * registerHandler * * Does this by... * 1. Points the passed in applyInputCb to a class instance handler * 2. If this is the first call then we register an amqpCacoon consumer which in turn * registers an amqpHandler function that will receive all new AMQP messages. * * @param applyInputCb - a handler that will be called when there is input. It should be passed input and context. * @returns Promise **/ registerInput(applyInputCb: (input: any, context: any) => Promise): Promise; /** * amqpHandler * * This is the function that we register with the amqpCacoon. * This function loops through the handlers registered and passes * the amqp message to our local registered handlers as a message. * * Note tha this INPUT provider adds a SPECIFIC object into facts with property amqpMessage. * amqpMessage in turn has three objects: * - amqpMessageContent: a string containing the AMQP message content. * - amqpMessageFields: an object, the amqp fields object. * - amqpMessageProperties: an object, the amqp properties object. * * @param channel: the active/open AMQP channel (inside a ChannelWrapper from node-amqp-connection-manager) to consume from. * @param msg: object - the message object, ConsumeMessage type. * @return Promise **/ amqpHandler(channel: ChannelWrapper, msg: ConsumeMessage): Promise; }