@boringnode/queue 0.0.1-alpha.3 → 0.0.1-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/build/index.d.ts CHANGED
@@ -1,27 +1,375 @@
1
- import { Q as QueueManagerConfig, W as WorkerCycle, A as Adapter, R as RetryConfig } from './job-Bd_c2lFK.js';
2
- export { J as Job, c as customBackoff, e as exponentialBackoff, f as fixedBackoff, l as linearBackoff } from './job-Bd_c2lFK.js';
1
+ import { Q as QueueManagerConfig, W as WorkerCycle, A as Adapter, R as RetryConfig, b as JobFactory, c as Job, d as JobClass } from './index-C3_tlebh.js';
2
+ export { e as customBackoff, f as exponentialBackoff, g as fixedBackoff, l as linearBackoff } from './index-C3_tlebh.js';
3
+ import * as _poppinss_utils from '@poppinss/utils';
3
4
 
5
+ /**
6
+ * Job processing worker.
7
+ *
8
+ * The Worker continuously polls queues for jobs and executes them
9
+ * with configurable concurrency. It handles:
10
+ * - Concurrent job execution via JobPool
11
+ * - Automatic retries with backoff strategies
12
+ * - Stalled job detection and recovery
13
+ * - Graceful shutdown on SIGINT/SIGTERM
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * import { Worker, redis } from '@boringnode/queue'
18
+ *
19
+ * const worker = new Worker({
20
+ * default: 'redis',
21
+ * adapters: { redis: redis() },
22
+ * locations: ['./jobs/**\/*.js'],
23
+ * worker: {
24
+ * concurrency: 5,
25
+ * idleDelay: '1s',
26
+ * },
27
+ * })
28
+ *
29
+ * // Start processing jobs
30
+ * await worker.start(['default', 'emails'])
31
+ *
32
+ * // Or for testing, process one cycle at a time
33
+ * const cycle = await worker.processCycle(['default'])
34
+ * ```
35
+ */
4
36
  declare class Worker {
5
37
  #private;
38
+ /** Unique identifier for this worker instance */
6
39
  get id(): string;
40
+ /**
41
+ * Create a new worker instance.
42
+ *
43
+ * @param config - Queue configuration including adapter and worker settings
44
+ */
7
45
  constructor(config: QueueManagerConfig);
46
+ /**
47
+ * Initialize the worker (called automatically by `start()`).
48
+ *
49
+ * Sets up the QueueManager and adapter connection.
50
+ */
8
51
  init(): Promise<void>;
52
+ /**
53
+ * Start processing jobs from the specified queues.
54
+ *
55
+ * This method blocks until the worker is stopped (via `stop()` or signal).
56
+ * Jobs are processed concurrently up to the configured concurrency limit.
57
+ *
58
+ * @param queues - Queue names to process (default: ['default'])
59
+ *
60
+ * @example
61
+ * ```typescript
62
+ * // Process single queue
63
+ * await worker.start()
64
+ *
65
+ * // Process multiple queues (priority order)
66
+ * await worker.start(['high-priority', 'default', 'low-priority'])
67
+ * ```
68
+ */
9
69
  start(queues?: string[]): Promise<void>;
70
+ /**
71
+ * Stop the worker gracefully.
72
+ *
73
+ * Waits for all running jobs to complete before shutting down.
74
+ * Called automatically on SIGINT/SIGTERM if gracefulShutdown is enabled.
75
+ */
10
76
  stop(): Promise<void>;
77
+ /**
78
+ * Process a single cycle and return the result.
79
+ *
80
+ * Useful for testing or when you need fine-grained control.
81
+ * Each cycle may start new jobs, complete a job, or return idle.
82
+ *
83
+ * @param queues - Queue names to process
84
+ * @returns The cycle result, or null if the worker was stopped
85
+ *
86
+ * @example
87
+ * ```typescript
88
+ * const worker = new Worker(config)
89
+ *
90
+ * // Process cycles manually
91
+ * let cycle = await worker.processCycle(['default'])
92
+ * while (cycle) {
93
+ * console.log('Cycle:', cycle.type)
94
+ * cycle = await worker.processCycle(['default'])
95
+ * }
96
+ * ```
97
+ */
11
98
  processCycle(queues: string[]): Promise<WorkerCycle | null>;
99
+ /**
100
+ * Generator that yields worker cycle events.
101
+ *
102
+ * Low-level API for processing jobs. Yields events for:
103
+ * - `started`: A new job began execution
104
+ * - `completed`: A job finished (success or failure)
105
+ * - `idle`: No jobs available, suggest waiting
106
+ * - `error`: An error occurred during processing
107
+ *
108
+ * @param queues - Queue names to process
109
+ * @yields WorkerCycle events
110
+ *
111
+ * @example
112
+ * ```typescript
113
+ * for await (const cycle of worker.process(['default'])) {
114
+ * switch (cycle.type) {
115
+ * case 'started':
116
+ * console.log(`Started job ${cycle.job.id}`)
117
+ * break
118
+ * case 'completed':
119
+ * console.log(`Completed job ${cycle.job.id}`)
120
+ * break
121
+ * case 'idle':
122
+ * await sleep(cycle.suggestedDelay)
123
+ * break
124
+ * }
125
+ * }
126
+ * ```
127
+ */
12
128
  process(queues: string[]): AsyncGenerator<WorkerCycle, void, unknown>;
13
129
  }
14
130
 
131
+ /**
132
+ * Central configuration and adapter management for the queue system.
133
+ *
134
+ * The QueueManager is responsible for:
135
+ * - Initializing adapters and job registration
136
+ * - Providing adapter instances to workers and dispatchers
137
+ * - Managing retry configuration across global, queue, and job levels
138
+ *
139
+ * @example
140
+ * ```typescript
141
+ * import { QueueManager, redis } from '@boringnode/queue'
142
+ *
143
+ * await QueueManager.init({
144
+ * default: 'redis',
145
+ * adapters: {
146
+ * redis: redis({ host: 'localhost' }),
147
+ * },
148
+ * locations: ['./jobs/**\/*.js'],
149
+ * retry: {
150
+ * maxRetries: 3,
151
+ * backoff: exponentialBackoff(),
152
+ * },
153
+ * })
154
+ *
155
+ * // Get the default adapter
156
+ * const adapter = QueueManager.use()
157
+ *
158
+ * // Clean up when done
159
+ * await QueueManager.destroy()
160
+ * ```
161
+ */
15
162
  declare class QueueManagerSingleton {
16
163
  #private;
164
+ /**
165
+ * Initialize the queue system with the given configuration.
166
+ *
167
+ * This must be called before using the queue system. It:
168
+ * - Validates the configuration
169
+ * - Registers adapters
170
+ * - Auto-discovers and registers job classes from `locations`
171
+ *
172
+ * @param config - The queue configuration
173
+ * @returns This instance for chaining
174
+ * @throws {E_CONFIGURATION_ERROR} If the configuration is invalid
175
+ *
176
+ * @example
177
+ * ```typescript
178
+ * await QueueManager.init({
179
+ * default: 'redis',
180
+ * adapters: {
181
+ * redis: redis(),
182
+ * postgres: knex(pgConfig),
183
+ * },
184
+ * locations: ['./jobs/**\/*.js'],
185
+ * })
186
+ * ```
187
+ */
17
188
  init(config: QueueManagerConfig): Promise<this>;
189
+ /**
190
+ * Get an adapter instance by name.
191
+ *
192
+ * Adapter instances are cached and reused. If no name is provided,
193
+ * the default adapter is returned.
194
+ *
195
+ * @param adapter - Adapter name (optional, defaults to the default adapter)
196
+ * @returns The adapter instance
197
+ * @throws {E_QUEUE_NOT_INITIALIZED} If `init()` hasn't been called
198
+ * @throws {E_CONFIGURATION_ERROR} If the adapter is not registered
199
+ * @throws {E_ADAPTER_INIT_ERROR} If the adapter factory throws
200
+ *
201
+ * @example
202
+ * ```typescript
203
+ * // Get default adapter
204
+ * const adapter = QueueManager.use()
205
+ *
206
+ * // Get specific adapter
207
+ * const redisAdapter = QueueManager.use('redis')
208
+ * ```
209
+ */
18
210
  use(adapter?: string): Adapter;
19
211
  /**
20
- * Priority: job > queue > global
212
+ * Get the merged retry configuration for a job.
213
+ *
214
+ * Configuration is merged with priority: job > queue > global.
215
+ * This allows specific jobs or queues to override global defaults.
216
+ *
217
+ * @param queue - The queue name
218
+ * @param jobRetryConfig - Optional job-level retry config
219
+ * @returns The merged retry configuration
220
+ *
221
+ * @example
222
+ * ```typescript
223
+ * // Global: maxRetries=3, Queue: maxRetries=5, Job: maxRetries=1
224
+ * // Result: maxRetries=1 (job wins)
225
+ * const config = QueueManager.getMergedRetryConfig('emails', { maxRetries: 1 })
226
+ * ```
21
227
  */
22
228
  getMergedRetryConfig(queue: string, jobRetryConfig?: RetryConfig): RetryConfig;
229
+ /**
230
+ * Get the configured job factory for custom instantiation.
231
+ *
232
+ * @returns The job factory function, or undefined if not configured
233
+ */
234
+ getJobFactory(): JobFactory | undefined;
235
+ /**
236
+ * Clean up all adapter instances and reset state.
237
+ *
238
+ * Call this when shutting down the application or when
239
+ * you need to reinitialize with a new configuration.
240
+ *
241
+ * @example
242
+ * ```typescript
243
+ * // On application shutdown
244
+ * await QueueManager.destroy()
245
+ * ```
246
+ */
23
247
  destroy(): Promise<void>;
24
248
  }
249
+ /** Global queue manager singleton */
25
250
  declare const QueueManager: QueueManagerSingleton;
26
251
 
27
- export { QueueManager, Worker };
252
+ /**
253
+ * Job class registry.
254
+ *
255
+ * The Locator maintains a mapping of job names to their classes,
256
+ * allowing the Worker to instantiate jobs by name when processing.
257
+ *
258
+ * Jobs are typically registered automatically via `QueueManager.init()`
259
+ * using the `locations` config option, but can also be registered manually.
260
+ *
261
+ * @example
262
+ * ```typescript
263
+ * import { Locator } from '@boringnode/queue'
264
+ * import SendEmailJob from './jobs/send_email_job.js'
265
+ *
266
+ * // Manual registration
267
+ * Locator.register('SendEmailJob', SendEmailJob)
268
+ *
269
+ * // Auto-registration via glob (used by QueueManager.init)
270
+ * await Locator.registerFromGlob(['./jobs/**\/*.js'])
271
+ *
272
+ * // Retrieve a job class
273
+ * const JobClass = Locator.getOrThrow('SendEmailJob')
274
+ * ```
275
+ */
276
+ declare class LocatorSingleton {
277
+ #private;
278
+ /**
279
+ * Register a job class with a given name.
280
+ *
281
+ * @param name - The job name (usually the class name)
282
+ * @param JobClass - The job class constructor
283
+ *
284
+ * @example
285
+ * ```typescript
286
+ * Locator.register('SendEmailJob', SendEmailJob)
287
+ * ```
288
+ */
289
+ register<T extends Job>(name: string, JobClass: JobClass<T>): void;
290
+ /**
291
+ * Auto-register job classes from files matching glob patterns.
292
+ *
293
+ * Each file should have a default export that is a Job class.
294
+ * The class name is used as the registration name.
295
+ *
296
+ * @param patterns - Glob patterns to match job files
297
+ * @returns Number of jobs successfully registered
298
+ *
299
+ * @example
300
+ * ```typescript
301
+ * const count = await Locator.registerFromGlob([
302
+ * './jobs/**\/*.js',
303
+ * './app/jobs/**\/*.ts'
304
+ * ])
305
+ * console.log(`Registered ${count} jobs`)
306
+ * ```
307
+ */
308
+ registerFromGlob(patterns: string[]): Promise<number>;
309
+ /**
310
+ * Get a job class by name.
311
+ *
312
+ * @param name - The job name to look up
313
+ * @returns The job class, or undefined if not found
314
+ *
315
+ * @example
316
+ * ```typescript
317
+ * const JobClass = Locator.get('SendEmailJob')
318
+ * if (JobClass) {
319
+ * const instance = new JobClass(payload)
320
+ * }
321
+ * ```
322
+ */
323
+ get<T extends Job = Job>(name: string): JobClass<T> | undefined;
324
+ /**
325
+ * Get a job class by name, throwing if not found.
326
+ *
327
+ * @param name - The job name to look up
328
+ * @returns The job class
329
+ * @throws {E_JOB_NOT_FOUND} If the job is not registered
330
+ *
331
+ * @example
332
+ * ```typescript
333
+ * const JobClass = Locator.getOrThrow('SendEmailJob')
334
+ * const instance = new JobClass(payload)
335
+ * ```
336
+ */
337
+ getOrThrow<T extends Job = Job>(name: string): JobClass<T>;
338
+ /**
339
+ * Remove all registered jobs.
340
+ *
341
+ * Primarily useful for testing.
342
+ */
343
+ clear(): void;
344
+ }
345
+ /** Global job class registry singleton */
346
+ declare const Locator: LocatorSingleton;
347
+
348
+ declare const E_INVALID_DURATION_EXPRESSION: new (args?: any, options?: ErrorOptions) => _poppinss_utils.Exception;
349
+ declare const E_INVALID_BASE_DELAY: new (args: [reason: string], options?: ErrorOptions) => _poppinss_utils.Exception;
350
+ declare const E_INVALID_MAX_DELAY: new (args: [reason: string], options?: ErrorOptions) => _poppinss_utils.Exception;
351
+ declare const E_INVALID_MULTIPLIER: new (args: [reason: string], options?: ErrorOptions) => _poppinss_utils.Exception;
352
+ declare const E_CONFIGURATION_ERROR: new (args: [reason: string], options?: ErrorOptions) => _poppinss_utils.Exception;
353
+ declare const E_JOB_NOT_FOUND: new (args: [jobName: string], options?: ErrorOptions) => _poppinss_utils.Exception;
354
+ declare const E_JOB_MAX_ATTEMPTS_REACHED: new (args: [jobName: string], options?: ErrorOptions) => _poppinss_utils.Exception;
355
+ declare const E_JOB_TIMEOUT: new (args: [jobName: string, timeout: number], options?: ErrorOptions) => _poppinss_utils.Exception;
356
+ declare const E_QUEUE_NOT_INITIALIZED: new (args?: any, options?: ErrorOptions) => _poppinss_utils.Exception;
357
+ declare const E_ADAPTER_INIT_ERROR: new (args: [adapterName: string, originalMessage: string], options?: ErrorOptions) => _poppinss_utils.Exception;
358
+ declare const E_NO_JOBS_FOUND: new (args: [patterns: string], options?: ErrorOptions) => _poppinss_utils.Exception;
359
+
360
+ declare const exceptions_E_ADAPTER_INIT_ERROR: typeof E_ADAPTER_INIT_ERROR;
361
+ declare const exceptions_E_CONFIGURATION_ERROR: typeof E_CONFIGURATION_ERROR;
362
+ declare const exceptions_E_INVALID_BASE_DELAY: typeof E_INVALID_BASE_DELAY;
363
+ declare const exceptions_E_INVALID_DURATION_EXPRESSION: typeof E_INVALID_DURATION_EXPRESSION;
364
+ declare const exceptions_E_INVALID_MAX_DELAY: typeof E_INVALID_MAX_DELAY;
365
+ declare const exceptions_E_INVALID_MULTIPLIER: typeof E_INVALID_MULTIPLIER;
366
+ declare const exceptions_E_JOB_MAX_ATTEMPTS_REACHED: typeof E_JOB_MAX_ATTEMPTS_REACHED;
367
+ declare const exceptions_E_JOB_NOT_FOUND: typeof E_JOB_NOT_FOUND;
368
+ declare const exceptions_E_JOB_TIMEOUT: typeof E_JOB_TIMEOUT;
369
+ declare const exceptions_E_NO_JOBS_FOUND: typeof E_NO_JOBS_FOUND;
370
+ declare const exceptions_E_QUEUE_NOT_INITIALIZED: typeof E_QUEUE_NOT_INITIALIZED;
371
+ declare namespace exceptions {
372
+ export { exceptions_E_ADAPTER_INIT_ERROR as E_ADAPTER_INIT_ERROR, exceptions_E_CONFIGURATION_ERROR as E_CONFIGURATION_ERROR, exceptions_E_INVALID_BASE_DELAY as E_INVALID_BASE_DELAY, exceptions_E_INVALID_DURATION_EXPRESSION as E_INVALID_DURATION_EXPRESSION, exceptions_E_INVALID_MAX_DELAY as E_INVALID_MAX_DELAY, exceptions_E_INVALID_MULTIPLIER as E_INVALID_MULTIPLIER, exceptions_E_JOB_MAX_ATTEMPTS_REACHED as E_JOB_MAX_ATTEMPTS_REACHED, exceptions_E_JOB_NOT_FOUND as E_JOB_NOT_FOUND, exceptions_E_JOB_TIMEOUT as E_JOB_TIMEOUT, exceptions_E_NO_JOBS_FOUND as E_NO_JOBS_FOUND, exceptions_E_QUEUE_NOT_INITIALIZED as E_QUEUE_NOT_INITIALIZED };
373
+ }
374
+
375
+ export { Job, Locator, QueueManager, Worker, exceptions as errors };