UNPKG

@webda/gcp

Version:

Webda GCP Services implementation

82 lines (81 loc) 2.17 kB
import { Message, PubSub } from "@google-cloud/pubsub"; import { CancelablePromise, DeepPartial, MessageReceipt, Queue, QueueParameters } from "@webda/core"; /** * GCPQueue Parameters */ export declare class GCPQueueParameters extends QueueParameters { /** * Topic to use */ topic: string; /** * Subscription to use for the queue * * All instances will use the same subscription to create a queue style pubsub */ subscription: string; /** * Timeout when receiveMessage is used before returning empty result (in ms) * * If not define then no timeout is applied and receiveMessage can hang forever */ timeout?: number; } /** * GCP Queue implementation on top of Pub/Sub * * @WebdaModda GoogleCloudQueue */ export default class GCPQueue<T = any, K extends GCPQueueParameters = GCPQueueParameters> extends Queue<T, K> { /** * Main api object */ pubsub: PubSub; /** * Current project id, used for manual acknowledge of message based on just their ackId */ projectId: Promise<string>; messages: { [key: string]: Message; }; /** * @override */ loadParameters(params: DeepPartial<K>): GCPQueueParameters; /** * @override */ init(): Promise<this>; /** * The queue size is not available within this service * * @returns */ size(): Promise<0>; /** * Acknowledge a message * @param id */ deleteMessage(id: string): Promise<void>; /** * Send a message to the queue * @param msg */ sendMessage(msg: T): Promise<void>; /** * Retrieve just one message from the queue * @param proto * @returns */ receiveMessage<L>(proto?: new () => L): Promise<MessageReceipt<L>[]>; /** * Work a queue calling the callback with every Event received * If the callback is called without exception the `deleteMessage` is called * @param callback * @param eventPrototype */ consume(callback: (event: T) => Promise<void>, eventPrototype?: { new (): T; }): CancelablePromise; } export { GCPQueue };