/** * Hooks system for observability (OpenTelemetry, DataDog, custom metrics) * * @example * ```typescript * import { trace } from '@opentelemetry/api'; * * const client = new FlashQ({ * hooks: { * onPush: (ctx) => { * ctx.span = trace.getTracer('flashq').startSpan('flashq.push', { * attributes: { queue: ctx.queue, priority: ctx.options?.priority } * }); * }, * onPushComplete: (ctx) => ctx.span?.end(), * onPushError: (ctx, error) => { * ctx.span?.recordException(error); * ctx.span?.end(); * }, * } * }); * ``` */ import type { Job, PushOptions } from './types'; /** Context passed to hooks - can store custom data like spans */ export interface HookContext { /** Operation start time */ startTime: number; /** Request ID for correlation */ requestId?: string; /** Custom data storage (e.g., OpenTelemetry span) */ [key: string]: unknown; } /** Push operation context */ export interface PushHookContext extends HookContext { queue: string; data: unknown; options?: PushOptions; /** Set after push completes */ job?: Job; } /** Pull operation context */ export interface PullHookContext extends HookContext { queue: string; timeout?: number; /** Set after pull completes */ job?: Job | null; } /** Ack operation context */ export interface AckHookContext extends HookContext { jobId: number; result?: unknown; } /** Fail operation context */ export interface FailHookContext extends HookContext { jobId: number; error?: string; } /** Process operation context (Worker) */ export interface ProcessHookContext extends HookContext { job: Job; workerId: number; /** Set after processing completes */ result?: unknown; /** Set if processing fails */ error?: Error; } /** Batch push context */ export interface BatchPushHookContext extends HookContext { queue: string; count: number; /** Set after batch completes */ ids?: number[]; failedCount?: number; } /** Batch pull context */ export interface BatchPullHookContext extends HookContext { queue: string; count: number; timeout?: number; /** Set after batch completes */ jobs?: Job[]; } /** Connection event context */ export interface ConnectionHookContext extends HookContext { host: string; port: number; event: 'connect' | 'disconnect' | 'reconnecting' | 'reconnected' | 'error'; error?: Error; attempt?: number; } /** * Hook definitions for FlashQ client */ export interface ClientHooks { /** Called before pushing a job */ onPush?: Hook; /** Called after push completes successfully */ onPushComplete?: Hook; /** Called if push fails */ onPushError?: ErrorHook; /** Called before pulling a job */ onPull?: Hook; /** Called after pull completes (job may be null on timeout) */ onPullComplete?: Hook; /** Called if pull fails */ onPullError?: ErrorHook; /** Called before acknowledging a job */ onAck?: Hook; /** Called after ack completes */ onAckComplete?: Hook; /** Called if ack fails */ onAckError?: ErrorHook; /** Called before failing a job */ onFail?: Hook; /** Called after fail completes */ onFailComplete?: Hook; /** Called if fail operation errors */ onFailError?: ErrorHook; /** Called before batch push */ onBatchPush?: Hook; /** Called after batch push completes */ onBatchPushComplete?: Hook; /** Called if batch push fails */ onBatchPushError?: ErrorHook; /** Called before batch pull */ onBatchPull?: Hook; /** Called after batch pull completes */ onBatchPullComplete?: Hook; /** Called if batch pull fails */ onBatchPullError?: ErrorHook; /** Called on connection events */ onConnection?: Hook; } /** * Hook definitions for FlashQ worker */ export interface WorkerHooks { /** Called before processing a job */ onProcess?: Hook; /** Called after job processed successfully */ onProcessComplete?: Hook; /** Called if job processing fails */ onProcessError?: ErrorHook; } /** Error hook type */ export type ErrorHook = (ctx: T, error: Error) => void | Promise; /** Standard hook type */ export type Hook = (ctx: T) => void | Promise; /** * Helper to safely call a hook */ export declare function callHook(hook: Hook | undefined, ctx: T): Promise; /** * Helper to safely call an error hook */ export declare function callErrorHook(hook: ErrorHook | undefined, ctx: T, error: Error): Promise; /** * Create a new hook context with start time */ export declare function createHookContext(data: Omit): T; /** * Calculate duration from context start time */ export declare function getDuration(ctx: HookContext): number; //# sourceMappingURL=hooks.d.ts.map