UNPKG

@penkov/tasks_queue

Version:

A lightweight PostgreSQL-backed task queue system with scheduling, retries, backoff strategies, and priority handling. Designed for efficiency and observability in modern Node.js applications.

79 lines (77 loc) 3.15 kB
import { TaskContext, TaskStatus } from "./tasks-model.js"; /** * Base class for a worker that processes tasks from a queue. * * Extend this class to define how a specific task should be handled. * You must implement the `process()` method. Other lifecycle methods * are optional and can be overridden to add custom behavior. * * See also {@link TaskContext}, {@link TaskFailed}, {@link MultiStepTask}, and * {@link SequentialTask}. */ export declare abstract class TasksWorker { /** * Process the payload of a task. * * This method **must** be implemented by subclasses. It contains * the main logic that should be executed when the task is picked up. * * If task is failed and an implementation requires to pass extra params to next attemts, * then it should throw TaskFailed exception. The payload of the exception will replace * the current task payload. * * To persist final task output independently from runtime state, use * `context.submitResult(...)`. * * @param payload - The data provided to the task. * @param context - the execution details * @returns resolved promise when the attempt work is complete * @see TaskFailed */ abstract process(payload: any, context: TaskContext): Promise<void>; /** * Called right before the task is processed. * * Override this to implement custom behavior that should happen * before processing starts (e.g., logging, tracing, state updates). * * Default implementation does nothing. * * @param taskId - The unique identifier of the task. * @param payload - The data provided to the task. * @returns nothing */ starting(taskId: number, payload: any): void; /** * Called after the task has been successfully completed. * * Override this to implement actions that should happen after * successful execution (e.g., scheduling next task, notifications). * * Default implementation does nothing. * * @param taskId - The unique identifier of the task. * @param payload - The data provided to the task. * @returns resolved promise when post-completion side effects are done */ completed(taskId: number, payload: any): Promise<void>; /** * Called if the task has failed. * * Override this to handle task failure. * * You can use the `finalStatus` parameter to determine whether the * task will be retried again (`'pending'`) or if it's terminally * failed (`'error'`). * * Default implementation does nothing. * * @param taskId - The unique identifier of the task. * @param payload - The data provided to the task. * @param finalStatus - Final status of the task: `'pending'` if it will be retried, * or `'error'` if it won't be retried anymore. * @param error - the error, raised during the task execution. * @returns resolved promise when post-failure side effects are done */ failed(taskId: number, payload: any, finalStatus: TaskStatus, error: any): Promise<void>; }