import { T } from ".."; import { Worker } from "./worker"; import type { EventJobName, EventName, Handler, JobName, JobStatus, QueueAddOptions, QueueInit, QueueProcessOptions } from "./types"; /** * Represents a job managed by the queue. * * @template T The type of the job payload. */ export type Job = { /** Unique identifier of the job. */ id: number; /** Name of the job. */ name: string; /** Current status of the job. */ status: JobStatus; /** Data associated with the job. */ payload: T; }; /** * Defines the type contract for queue events and jobs. * * This interface is intentionally empty and can be extended * using TypeScript module augmentation to provide type-safe * event and job names. * * @example * ```ts * declare module 'tspace-mysql' { * interface QueueContract { * events: { * 'user.created': ['send-email', 'send-push']; * }; * * jobs: ['send-email', 'send-push']; * } * } * ``` */ export interface QueueContract { } /** * Queue facade class (static API wrapper) * * This class provides a singleton-style interface over the underlying Worker instance. * It must be initialized before use via `Queue.start()`. * * @example * ```ts * const sendEmail = (job) => console.log('send mail :' + job.payload.email) * * await Queue.start({ inspect : true, flush : true // **remove all jobs }); * * // register * Queue.progress("send-email", async (job) => { * return await sendEmail(job); * }, { concurrency : 3 }); * * // add * Queue.add("send-email", { email: "test@gmail.com" }); * * ``` */ declare class Queue { /** * Internal Worker instance used for all queue operations. * @type {Worker | null} */ private static WORKER; private static MESSAGE; /** * The 'start' method is used to initialize the Queue system. * Creates and prepares the underlying Worker instance. * * @param {Object} [opts] - options (inspect, flush) * @property {boolean?} opts.inspect queue work flow * @property {boolean?} opts.flush remove all queue * @property {number?} opts.maxIdleRetries - Maximum idle time () when no jobs are available * @property {boolean} [opts.poll.enabled] - Enable or disable worker job polling. * @property {number} [opts.poll.timeout] - Polling interval in milliseconds. * @returns {Promise} */ static start(opts?: QueueInit): Promise; /** * The 'end' method is used to shutdown the Queue system. * * @returns {Promise} */ static end(): Promise; /** * The 'flush' method is used to flush all jobs in the queue (dangerous operation). * * @throws {Error} If Queue is not initialized. * @returns {Promise} */ static flush(): Promise; /** * The 'getJobOverallStats' method is used to get aggregated queue statistics. * * @param {string} [name] - Optional queue name filter. * @throws {Error} If Queue is not initialized. * @returns {Promise} */ static getJobOverallStats(name?: string): Promise<{ total: number; completed: number; active: number; pending: number; failed: number; }>; /** * The 'getJobStats' method is used to Get jobs statistics grouped by name. * * @param {string} [name] - Optional queue name filter. * @throws {Error} If Queue is not initialized. * @returns {Promise} */ static getJobStats(name?: string): Promise<{ completed: number; active: number; pending: number; failed: number; }[]>; /** * The 'getJobs' method is used to Get jobs. * * @param {string} [name] - Optional queue name filter. * @throws {Error} If Queue is not initialized. * @returns {Promise[]>} */ static getJobs(name?: string): Promise[]>; /** * Get all unique queue names. * * @throws {Error} If Queue is not initialized. * @returns {Promise} */ static getNames(): Promise; /** * Access raw Worker instance safely. * * @param {(worker: Worker) => any} cb - Callback with Worker instance. * @throws {Error} If Queue is not initialized. * @returns {Promise} */ static worker(cb: (worker: Worker) => any): Promise; /** * Start a worker for processing jobs of a specific name. * * @param {string} name - Queue name to process. * @param {Handler} handler - Job handler function. * @param {QueueProcessOptions} [opts] - Job options (interval, concurrency) * @throws {Error} If Queue is not initialized. * @returns {Promise} * * @example * const helloWorld = (job) => console.log('hello world :' + job.id); * * Queue.progress("hello", async (job) => { * return await helloWorld(job) * }, { concurrency : 3 }); */ static process(name: JobName, handler: Handler, opts?: QueueProcessOptions): Promise; /** * Start a worker for processing jobs of a specific name. * * @param {string} name - Queue name to process. * @param {Handler} handler - Job handler function. * @param {QueueProcessOptions} [opts] - Job options (interval, concurrency) * @throws {Error} If Queue is not initialized. * @returns {Promise} * * @example * const helloWorld = (job) => console.log('hello world :' + job.id); * * Queue.on("hello", async (job) => { * return await helloWorld(job) * }, { concurrency : 3 }); */ static on(name: JobName, handler: Handler, opts?: QueueProcessOptions): Promise; /** * Add a new job into the queue. * * @param {string} name - Queue name / job type. * @param {any} payload - Job payload data, send to process * @param {QueueAddOptions} [opts] - Job options (delay, priority, retry, etc.) * @throws {Error} If Queue is not initialized. * @returns {Promise>} * * @example * ```ts * Queue.add("send-email", { email: "test@gmail.com" }); * Queue.add("send-email", { email: "test2@gmail.com" }, { * metadata : 'first priority', * priority : 9999 * delayMs : 100 * maxAttempts : 1 * }); * ``` */ static add(name: JobName, payload: any, opts?: QueueAddOptions): Promise; /** * Publishes an event to all registered subscribers. * * Each subscriber receives the event payload as an individual job. * * @param event - The event name to publish. * @param payload - The data associated with the event. * @param {QueueAddOptions} [opts] - Job options (delay, priority, retry, etc.) * @throws {Error} If the queue worker has not been initialized. * * @example * ```ts * Queue.publish('user.created', { * userId: 123, * email: 'user@example.com', * }); * * Queue.publish('user.created', { * userId: 123, * email: 'user@example.com', * }, { * metadata : 'first priority', * priority : 9999 * delayMs : 100 * maxAttempts : 1 * }); * ``` */ static publish(event: E, payload: any, opts?: QueueAddOptions): Promise; /** * Subscribes a handler to an event. * * The subscriber name is combined with the event name to create * a unique job name in the format `{event}.{name}`. * * @param event - The event name to subscribe to. * @param name - The unique name of the subscriber. * @param handler - The function executed when the event is received. * @param opts - Options controlling job processing behavior. * * @throws {Error} If the queue worker has not been initialized. * * @example * ```ts * await Queue.subscribe( * 'user.created', * 'email', * async (job) => { * await sendEmail(job.payload); * }, * { * concurrency: 5, * } * ); * ``` * * @remarks * A subscription to `user.created` with the name `email` * creates the job queue `user.created.email`. */ static subscribe(event: E, name: EventJobName, handler: Handler, opts?: QueueProcessOptions): Promise; } export { Queue }; export default Queue;