import { Collection, Filter, ObjectId, WithId } from 'mongodb'; import { ErrorEvents } from './ErrorEvents'; import { PromiseTracker } from './PromiseTracker'; import { Timeout } from './Timeout'; import { TypedEventEmitter } from './TypedEventEmitter'; import { WithOptionalObjectId } from './WithOptionalObjectId'; export interface ConsumerOptions { /** * MongoDB filter for messages to be consumed. */ filter?: Filter; /** * Consumer group. * Defaults to the collection name. */ group?: string; /** * Maximum number of messages to be consumed concurrently for this Consumer. * Defaults to 1. */ concurrency?: number; /** * Minimum number of seconds to hide messages from other consumers after receiving. * Set at least to 2x of the maximum workload time for message consumption. * Defaults to 2 seconds. */ visibilityTimeoutSeconds?: number; /** * Minimum number of seconds before a published message may be consumed. * Defaults to zero. */ visibilityDelaySeconds?: number; /** * Maximum number of seconds to poll for past messages to consume. * Defaults to 3600 seconds (1 hour). */ maxVisibilitySeconds?: number; /** * Maximum number of retries per message. * Defaults to 1. */ maxRetries?: number; /** * Polling interval in milliseconds. * Defaults to 1000 milliseconds. */ pollMs?: number; /** * Maximum follow-up polling delay in milliseconds. * Consumers will fast-poll while concurrency is not reached, * and after a message is successfully consumed. * Defaults to 10 milliseconds. */ fastPollMs?: number; } export interface ConsumerEvents extends ErrorEvents { deadLetter: (err: Error, message: WithId, group: string) => void; drained: (group: string) => void; } export type ConsumerCallback = (message: WithId, context: ConsumerCallbackContext) => void | Promise; export interface ConsumerCallbackContext { retries: number; retry: (seconds?: number) => Promise; } export interface ConsumerMetadata { _c?: Record; } export declare class Consumer extends TypedEventEmitter> { protected collection: Collection; protected filter: Filter; protected group: string; protected concurrency: number; protected visibilityTimeoutSeconds: number; protected visibilityDelaySeconds: number; protected maxVisibilitySeconds: number; protected maxRetries: number; protected pollMs: number; protected fastPollMs: number; protected callback: ConsumerCallback; protected visibilityKey: string; protected retryKey: string; protected ackKey: string; protected nextTimeout: Timeout; protected seekTimeout: Timeout; protected pending: number; protected minId: ObjectId; protected promises: PromiseTracker; protected closed: boolean; constructor(collection: Collection, callback: ConsumerCallback, options?: ConsumerOptions); /** * Starts the consumer. * * - Consumes future and past matching messages. * - Per `group`, each matching message is consumed by at most one consumer. * - Events are consumed at-least-once per `group`. * - Order of message consumption is not guaranteed. * - Will write metadata to messages under `message._c..*`. * - The underlying messages collection must only use auto-generated MongoDB Object IDs. * * See constructor options for details. */ start(): Promise; /** * Prevent the specified message ID from being consumed * for the specified number of seconds (from now). * Can be used for implementing e.g. exponentional backoff. */ hide(messageId: ObjectId, seconds: number): Promise; /** * Waits until the consumer is drained, * i.e. it could not receive any consumable message. */ drain(timeoutMs?: number): Promise; close(): Promise; next(): Promise; protected receive(): Promise | null>; protected ack(message: WithId): Promise; seek(): Promise; }