11import type { JobData } from '../types/main.js'
22
3+ /**
4+ * A job that has been acquired by a worker for processing.
5+ * Extends JobData with the timestamp when the job was acquired.
6+ */
37export interface AcquiredJob extends JobData {
8+ /** Timestamp (in ms) when the job was acquired by the worker */
49 acquiredAt : number
510}
611
12+ /**
13+ * Adapter interface for queue storage backends.
14+ *
15+ * Implementations handle job persistence, atomic operations, and
16+ * concurrency control. Built-in adapters: Redis, Knex (PostgreSQL/SQLite).
17+ *
18+ * @example
19+ * ```typescript
20+ * import { redis } from '@boringnode/queue'
21+ *
22+ * const config = {
23+ * default: 'redis',
24+ * adapters: {
25+ * redis: redis({ host: 'localhost', port: 6379 })
26+ * }
27+ * }
28+ * ```
29+ */
730export interface Adapter {
831 /**
932 * Set the worker ID for this adapter instance.
1033 * Required before calling pop methods when consuming jobs.
34+ *
35+ * @param workerId - Unique identifier for the worker
1136 */
1237 setWorkerId ( workerId : string ) : void
1338
1439 /**
1540 * Pop the next available job from the default queue.
16- * The driver handles locking internally.
41+ * Atomically moves the job from pending to active state.
42+ *
43+ * @returns The acquired job, or null if queue is empty
1744 */
1845 pop ( ) : Promise < AcquiredJob | null >
1946
2047 /**
2148 * Pop the next available job from a specific queue.
22- * The driver handles locking internally.
49+ * Atomically moves the job from pending to active state.
50+ *
51+ * @param queue - The queue name to pop from
52+ * @returns The acquired job, or null if queue is empty
2353 */
2454 popFrom ( queue : string ) : Promise < AcquiredJob | null >
2555
2656 /**
2757 * Recover stalled jobs that have been active for too long.
28- * Jobs that exceed maxStalledCount will be failed permanently.
58+ * A stalled job is one where the worker stopped responding (e.g., crash).
59+ *
60+ * Jobs within maxStalledCount are moved back to pending.
61+ * Jobs exceeding maxStalledCount are failed permanently.
2962 *
3063 * @param queue - The queue to check for stalled jobs
3164 * @param stalledThreshold - Duration in ms after which a job is considered stalled
@@ -39,26 +72,81 @@ export interface Adapter {
3972 ) : Promise < number >
4073
4174 /**
42- * Mark a job as completed and remove it from active set.
75+ * Mark a job as completed and remove it from the queue.
76+ *
77+ * @param jobId - The job ID to complete
78+ * @param queue - The queue the job belongs to
4379 */
4480 completeJob ( jobId : string , queue : string ) : Promise < void >
4581
4682 /**
47- * Mark a job as failed permanently.
83+ * Mark a job as failed permanently and remove it from the queue.
84+ *
85+ * @param jobId - The job ID to fail
86+ * @param queue - The queue the job belongs to
87+ * @param error - Optional error that caused the failure
4888 */
4989 failJob ( jobId : string , queue : string , error ?: Error ) : Promise < void >
5090
5191 /**
52- * Retry a job - move back to pending queue with incremented attempts.
92+ * Retry a job by moving it back to pending with incremented attempts.
93+ *
94+ * @param jobId - The job ID to retry
95+ * @param queue - The queue the job belongs to
96+ * @param retryAt - Optional future date to delay the retry
5397 */
5498 retryJob ( jobId : string , queue : string , retryAt ?: Date ) : Promise < void >
5599
100+ /**
101+ * Push a job to the default queue for immediate processing.
102+ *
103+ * @param jobData - The job data to push
104+ */
56105 push ( jobData : JobData ) : Promise < void >
106+
107+ /**
108+ * Push a job to a specific queue for immediate processing.
109+ *
110+ * @param queue - The queue name to push to
111+ * @param jobData - The job data to push
112+ */
57113 pushOn ( queue : string , jobData : JobData ) : Promise < void >
114+
115+ /**
116+ * Push a job to the default queue with a delay.
117+ *
118+ * @param jobData - The job data to push
119+ * @param delay - Delay in milliseconds before the job becomes available
120+ */
58121 pushLater ( jobData : JobData , delay : number ) : Promise < void >
122+
123+ /**
124+ * Push a job to a specific queue with a delay.
125+ *
126+ * @param queue - The queue name to push to
127+ * @param jobData - The job data to push
128+ * @param delay - Delay in milliseconds before the job becomes available
129+ */
59130 pushLaterOn ( queue : string , jobData : JobData , delay : number ) : Promise < void >
60131
132+ /**
133+ * Get the number of pending jobs in the default queue.
134+ *
135+ * @returns The number of pending jobs
136+ */
61137 size ( ) : Promise < number >
138+
139+ /**
140+ * Get the number of pending jobs in a specific queue.
141+ *
142+ * @param queue - The queue name to check
143+ * @returns The number of pending jobs
144+ */
62145 sizeOf ( queue : string ) : Promise < number >
146+
147+ /**
148+ * Clean up resources (close connections, etc.).
149+ * Called when the worker stops or the adapter is no longer needed.
150+ */
63151 destroy ( ) : Promise < void >
64152}
0 commit comments