@igniter-js/jobs 0.1.0
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/AGENTS.md +557 -0
- package/README.md +410 -0
- package/dist/adapter-CcQCatSa.d.mts +1411 -0
- package/dist/adapter-CcQCatSa.d.ts +1411 -0
- package/dist/adapters/bullmq.adapter.d.mts +131 -0
- package/dist/adapters/bullmq.adapter.d.ts +131 -0
- package/dist/adapters/bullmq.adapter.js +598 -0
- package/dist/adapters/bullmq.adapter.js.map +1 -0
- package/dist/adapters/bullmq.adapter.mjs +596 -0
- package/dist/adapters/bullmq.adapter.mjs.map +1 -0
- package/dist/adapters/index.d.mts +5 -0
- package/dist/adapters/index.d.ts +5 -0
- package/dist/adapters/index.js +1129 -0
- package/dist/adapters/index.js.map +1 -0
- package/dist/adapters/index.mjs +1126 -0
- package/dist/adapters/index.mjs.map +1 -0
- package/dist/adapters/memory.adapter.d.mts +118 -0
- package/dist/adapters/memory.adapter.d.ts +118 -0
- package/dist/adapters/memory.adapter.js +571 -0
- package/dist/adapters/memory.adapter.js.map +1 -0
- package/dist/adapters/memory.adapter.mjs +569 -0
- package/dist/adapters/memory.adapter.mjs.map +1 -0
- package/dist/index.d.mts +1107 -0
- package/dist/index.d.ts +1107 -0
- package/dist/index.js +1137 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1128 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +93 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,1107 @@
|
|
|
1
|
+
import { IgniterLogger, IgniterError } from '@igniter-js/core';
|
|
2
|
+
import { IgniterTelemetry } from '@igniter-js/telemetry';
|
|
3
|
+
import { n as IgniterJobDefinition, o as IgniterCronDefinition, p as IgniterQueueConfig, I as IgniterJobsAdapter, q as IgniterJobScopeOptions, r as IgniterJobActorOptions, l as AdapterJobHandler } from './adapter-CcQCatSa.js';
|
|
4
|
+
export { A as AdapterDispatchParams, a as AdapterScheduleParams, k as AdapterWorkerConfig, m as AdapterWorkerHandle, y as IgniterCronFailureHook, u as IgniterCronHandler, v as IgniterCronHandlerContext, Q as IgniterJobActorEntry, z as IgniterJobBackoff, x as IgniterJobCompleteHook, M as IgniterJobCompletedPayload, g as IgniterJobCounts, J as IgniterJobEnqueuedPayload, G as IgniterJobEventContext, i as IgniterJobEventHandler, H as IgniterJobEventPayload, F as IgniterJobEventType, N as IgniterJobFailedPayload, w as IgniterJobFailureHook, s as IgniterJobHandler, t as IgniterJobHandlerContext, b as IgniterJobInfo, B as IgniterJobLimiter, d as IgniterJobLog, L as IgniterJobProgressPayload, O as IgniterJobRetryingPayload, P as IgniterJobScopeEntry, h as IgniterJobSearchResult, K as IgniterJobStartedPayload, c as IgniterJobStatus, j as IgniterJobUnsubscribeFn, D as IgniterJobsSearchFilter, f as IgniterQueueCleanOptions, e as IgniterQueueInfo, C as IgniterQueuesSearchFilter, E as IgniterWorkersSearchFilter } from './adapter-CcQCatSa.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @fileoverview Configuration types for @igniter-js/jobs
|
|
8
|
+
* @module @igniter-js/jobs/types/config
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Full configuration for IgniterJobs.
|
|
13
|
+
*
|
|
14
|
+
* @typeParam TContext - Application context type
|
|
15
|
+
* @typeParam TQueues - Map of queue names to queue configs
|
|
16
|
+
* @typeParam TScope - Scope key (only one allowed)
|
|
17
|
+
* @typeParam TActor - Actor key (only one allowed)
|
|
18
|
+
*/
|
|
19
|
+
interface IgniterJobsConfig<TContext = unknown, TQueues extends Record<string, IgniterQueueConfig<TContext, any, any>> = Record<string, never>, TScope extends string = never, TActor extends string = never> {
|
|
20
|
+
/**
|
|
21
|
+
* Queue adapter (required).
|
|
22
|
+
*/
|
|
23
|
+
adapter: IgniterJobsAdapter;
|
|
24
|
+
/**
|
|
25
|
+
* Application context provider (required).
|
|
26
|
+
* Called for each job to get the context.
|
|
27
|
+
*/
|
|
28
|
+
context: () => TContext | Promise<TContext>;
|
|
29
|
+
/**
|
|
30
|
+
* Queue configurations.
|
|
31
|
+
*/
|
|
32
|
+
queues: TQueues;
|
|
33
|
+
/**
|
|
34
|
+
* Scope definition (only one allowed).
|
|
35
|
+
* Used for multi-tenancy.
|
|
36
|
+
*/
|
|
37
|
+
scope?: TScope extends never ? never : {
|
|
38
|
+
key: TScope;
|
|
39
|
+
options?: IgniterJobScopeOptions;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Actor definition (only one allowed).
|
|
43
|
+
* Used for auditing.
|
|
44
|
+
*/
|
|
45
|
+
actor?: TActor extends never ? never : {
|
|
46
|
+
key: TActor;
|
|
47
|
+
options?: IgniterJobActorOptions;
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Telemetry instance for observability.
|
|
51
|
+
*/
|
|
52
|
+
telemetry?: IgniterTelemetry;
|
|
53
|
+
/**
|
|
54
|
+
* Custom logger.
|
|
55
|
+
*/
|
|
56
|
+
logger?: IgniterLogger;
|
|
57
|
+
/**
|
|
58
|
+
* Default job options applied to all jobs.
|
|
59
|
+
*/
|
|
60
|
+
defaults?: {
|
|
61
|
+
/**
|
|
62
|
+
* Default retry configuration.
|
|
63
|
+
*/
|
|
64
|
+
retry?: {
|
|
65
|
+
attempts?: number;
|
|
66
|
+
backoff?: {
|
|
67
|
+
type: 'exponential' | 'fixed' | 'linear';
|
|
68
|
+
delay: number;
|
|
69
|
+
maxDelay?: number;
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Default timeout in milliseconds.
|
|
74
|
+
*/
|
|
75
|
+
timeout?: number;
|
|
76
|
+
/**
|
|
77
|
+
* Default remove on complete config.
|
|
78
|
+
*/
|
|
79
|
+
removeOnComplete?: boolean | {
|
|
80
|
+
count?: number;
|
|
81
|
+
age?: number;
|
|
82
|
+
};
|
|
83
|
+
/**
|
|
84
|
+
* Default remove on fail config.
|
|
85
|
+
*/
|
|
86
|
+
removeOnFail?: boolean | {
|
|
87
|
+
count?: number;
|
|
88
|
+
age?: number;
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Builder state for IgniterJobs configuration.
|
|
94
|
+
*
|
|
95
|
+
* @typeParam TContext - Application context type
|
|
96
|
+
* @typeParam TQueues - Map of queue names to queue configs
|
|
97
|
+
* @typeParam TScope - Scope key
|
|
98
|
+
* @typeParam TActor - Actor key
|
|
99
|
+
*/
|
|
100
|
+
interface IgniterJobsBuilderState<TContext = unknown, TQueues extends Record<string, IgniterQueueConfig<TContext, any, any>> = Record<string, never>, TScope extends string = never, TActor extends string = never> {
|
|
101
|
+
adapter?: IgniterJobsAdapter;
|
|
102
|
+
context?: () => TContext | Promise<TContext>;
|
|
103
|
+
queues: TQueues;
|
|
104
|
+
scope?: {
|
|
105
|
+
key: TScope;
|
|
106
|
+
options?: IgniterJobScopeOptions;
|
|
107
|
+
};
|
|
108
|
+
actor?: {
|
|
109
|
+
key: TActor;
|
|
110
|
+
options?: IgniterJobActorOptions;
|
|
111
|
+
};
|
|
112
|
+
telemetry?: IgniterTelemetry;
|
|
113
|
+
logger?: IgniterLogger;
|
|
114
|
+
defaults?: IgniterJobsConfig<TContext>['defaults'];
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Builder state for IgniterQueue configuration.
|
|
118
|
+
*
|
|
119
|
+
* @typeParam TContext - Application context type
|
|
120
|
+
* @typeParam TJobs - Map of job names to definitions
|
|
121
|
+
* @typeParam TCrons - Map of cron names to definitions
|
|
122
|
+
*/
|
|
123
|
+
interface IgniterQueueBuilderState<TContext = unknown, TJobs extends Record<string, IgniterJobDefinition<TContext, any, any>> = Record<string, never>, TCrons extends Record<string, IgniterCronDefinition<TContext, any>> = Record<string, never>> {
|
|
124
|
+
name: string;
|
|
125
|
+
jobs: TJobs;
|
|
126
|
+
crons: TCrons;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* @fileoverview Worker types for @igniter-js/jobs
|
|
131
|
+
* @module @igniter-js/jobs/types/worker
|
|
132
|
+
*/
|
|
133
|
+
/**
|
|
134
|
+
* Worker handle returned from worker.start().
|
|
135
|
+
*/
|
|
136
|
+
interface IgniterWorkerHandle {
|
|
137
|
+
/**
|
|
138
|
+
* Unique worker ID.
|
|
139
|
+
*/
|
|
140
|
+
readonly id: string;
|
|
141
|
+
/**
|
|
142
|
+
* Pause the worker (stops processing new jobs).
|
|
143
|
+
*/
|
|
144
|
+
pause(): Promise<void>;
|
|
145
|
+
/**
|
|
146
|
+
* Resume the worker.
|
|
147
|
+
*/
|
|
148
|
+
resume(): Promise<void>;
|
|
149
|
+
/**
|
|
150
|
+
* Close the worker gracefully.
|
|
151
|
+
*/
|
|
152
|
+
close(): Promise<void>;
|
|
153
|
+
/**
|
|
154
|
+
* Check if worker is currently running.
|
|
155
|
+
*/
|
|
156
|
+
isRunning(): boolean;
|
|
157
|
+
/**
|
|
158
|
+
* Check if worker is paused.
|
|
159
|
+
*/
|
|
160
|
+
isPaused(): boolean;
|
|
161
|
+
/**
|
|
162
|
+
* Get worker metrics.
|
|
163
|
+
*/
|
|
164
|
+
getMetrics(): Promise<IgniterWorkerMetrics>;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Worker metrics.
|
|
168
|
+
*/
|
|
169
|
+
interface IgniterWorkerMetrics {
|
|
170
|
+
/**
|
|
171
|
+
* Total jobs processed.
|
|
172
|
+
*/
|
|
173
|
+
processed: number;
|
|
174
|
+
/**
|
|
175
|
+
* Total jobs failed.
|
|
176
|
+
*/
|
|
177
|
+
failed: number;
|
|
178
|
+
/**
|
|
179
|
+
* Total jobs completed successfully.
|
|
180
|
+
*/
|
|
181
|
+
completed: number;
|
|
182
|
+
/**
|
|
183
|
+
* Currently active jobs.
|
|
184
|
+
*/
|
|
185
|
+
active: number;
|
|
186
|
+
/**
|
|
187
|
+
* Worker uptime in milliseconds.
|
|
188
|
+
*/
|
|
189
|
+
uptime: number;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Worker builder state.
|
|
193
|
+
*/
|
|
194
|
+
interface IgniterWorkerBuilderState {
|
|
195
|
+
/**
|
|
196
|
+
* Queue names to process.
|
|
197
|
+
*/
|
|
198
|
+
queues: string[];
|
|
199
|
+
/**
|
|
200
|
+
* Concurrency per queue.
|
|
201
|
+
*/
|
|
202
|
+
concurrency: number;
|
|
203
|
+
/**
|
|
204
|
+
* Lock duration in milliseconds.
|
|
205
|
+
*/
|
|
206
|
+
lockDuration?: number;
|
|
207
|
+
/**
|
|
208
|
+
* Rate limiter configuration.
|
|
209
|
+
*/
|
|
210
|
+
limiter?: {
|
|
211
|
+
max: number;
|
|
212
|
+
duration: number;
|
|
213
|
+
};
|
|
214
|
+
/**
|
|
215
|
+
* Callback when worker becomes idle.
|
|
216
|
+
*/
|
|
217
|
+
onIdle?: () => void;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* @fileoverview Builder for creating IgniterJobs instances
|
|
222
|
+
* @module @igniter-js/jobs/builders/igniter-jobs
|
|
223
|
+
*
|
|
224
|
+
* @description
|
|
225
|
+
* Provides a fluent builder API for configuring and creating IgniterJobs instances.
|
|
226
|
+
* Supports adapter configuration, context provider, queues, scopes, actors, and telemetry.
|
|
227
|
+
*
|
|
228
|
+
* @example
|
|
229
|
+
* ```typescript
|
|
230
|
+
* import { IgniterJobs, IgniterQueue } from '@igniter-js/jobs'
|
|
231
|
+
* import { BullMQAdapter } from '@igniter-js/jobs/adapters'
|
|
232
|
+
* import Redis from 'ioredis'
|
|
233
|
+
*
|
|
234
|
+
* const redis = new Redis()
|
|
235
|
+
*
|
|
236
|
+
* const emailQueue = IgniterQueue.create<AppContext>('email')
|
|
237
|
+
* .addJob('sendWelcome', { ... })
|
|
238
|
+
* .build()
|
|
239
|
+
*
|
|
240
|
+
* const jobs = IgniterJobs.create<AppContext>()
|
|
241
|
+
* .withAdapter(BullMQAdapter.create({ redis }))
|
|
242
|
+
* .withContext(() => ({ db, mailer }))
|
|
243
|
+
* .addQueue(emailQueue)
|
|
244
|
+
* .addScope('organization')
|
|
245
|
+
* .addActor('user')
|
|
246
|
+
* .build()
|
|
247
|
+
* ```
|
|
248
|
+
*/
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Builder for creating IgniterJobs instances.
|
|
252
|
+
*
|
|
253
|
+
* @typeParam TContext - Application context type
|
|
254
|
+
* @typeParam TQueues - Map of queue names to queue configs
|
|
255
|
+
* @typeParam TScope - Scope key (only one allowed)
|
|
256
|
+
* @typeParam TActor - Actor key (only one allowed)
|
|
257
|
+
*/
|
|
258
|
+
declare class IgniterJobsBuilder<TContext = unknown, TQueues extends Record<string, IgniterQueueBuilderState<TContext, any, any>> = Record<string, never>, TScope extends string = never, TActor extends string = never> {
|
|
259
|
+
private readonly state;
|
|
260
|
+
private constructor();
|
|
261
|
+
/**
|
|
262
|
+
* Create a new IgniterJobs builder.
|
|
263
|
+
*
|
|
264
|
+
* @returns A new IgniterJobsBuilder instance
|
|
265
|
+
*
|
|
266
|
+
* @example
|
|
267
|
+
* ```typescript
|
|
268
|
+
* const jobs = IgniterJobs.create<AppContext>()
|
|
269
|
+
* .withAdapter(...)
|
|
270
|
+
* .build()
|
|
271
|
+
* ```
|
|
272
|
+
*/
|
|
273
|
+
static create<TContext = unknown>(): IgniterJobsBuilder<TContext>;
|
|
274
|
+
/**
|
|
275
|
+
* Configure the queue adapter (required).
|
|
276
|
+
*
|
|
277
|
+
* @param adapter - The queue adapter (e.g., BullMQAdapter)
|
|
278
|
+
* @returns The builder with adapter configured
|
|
279
|
+
*
|
|
280
|
+
* @example
|
|
281
|
+
* ```typescript
|
|
282
|
+
* .withAdapter(BullMQAdapter.create({ redis }))
|
|
283
|
+
* ```
|
|
284
|
+
*/
|
|
285
|
+
withAdapter(adapter: IgniterJobsAdapter): IgniterJobsBuilder<TContext, TQueues, TScope, TActor>;
|
|
286
|
+
/**
|
|
287
|
+
* Configure the application context provider (required).
|
|
288
|
+
* This function is called for each job to provide the application context.
|
|
289
|
+
*
|
|
290
|
+
* @param contextFn - Function that returns the application context
|
|
291
|
+
* @returns The builder with context configured
|
|
292
|
+
*
|
|
293
|
+
* @example
|
|
294
|
+
* ```typescript
|
|
295
|
+
* .withContext(() => ({
|
|
296
|
+
* db: prisma,
|
|
297
|
+
* mailer: mailerService,
|
|
298
|
+
* cache: redis,
|
|
299
|
+
* }))
|
|
300
|
+
* ```
|
|
301
|
+
*/
|
|
302
|
+
withContext(contextFn: () => TContext | Promise<TContext>): IgniterJobsBuilder<TContext, TQueues, TScope, TActor>;
|
|
303
|
+
/**
|
|
304
|
+
* Add a queue to the jobs instance.
|
|
305
|
+
*
|
|
306
|
+
* @param queue - Queue configuration from IgniterQueue.create().build()
|
|
307
|
+
* @returns The builder with the queue added
|
|
308
|
+
*
|
|
309
|
+
* @example
|
|
310
|
+
* ```typescript
|
|
311
|
+
* const emailQueue = IgniterQueue.create<AppContext>('email')
|
|
312
|
+
* .addJob('sendWelcome', { ... })
|
|
313
|
+
* .build()
|
|
314
|
+
*
|
|
315
|
+
* const jobs = IgniterJobs.create<AppContext>()
|
|
316
|
+
* .addQueue(emailQueue)
|
|
317
|
+
* .build()
|
|
318
|
+
* ```
|
|
319
|
+
*/
|
|
320
|
+
addQueue<TQueueState extends IgniterQueueBuilderState<TContext, any, any>>(queue: TQueueState): IgniterJobsBuilder<TContext, TQueues & {
|
|
321
|
+
[K in TQueueState['name']]: TQueueState;
|
|
322
|
+
}, TScope, TActor>;
|
|
323
|
+
/**
|
|
324
|
+
* Add a scope for multi-tenancy (only one scope allowed).
|
|
325
|
+
* Scopes are used to isolate jobs by organization, tenant, etc.
|
|
326
|
+
*
|
|
327
|
+
* @param key - Scope key (e.g., 'organization', 'tenant')
|
|
328
|
+
* @param options - Optional scope configuration
|
|
329
|
+
* @returns The builder with scope configured
|
|
330
|
+
*
|
|
331
|
+
* @example
|
|
332
|
+
* ```typescript
|
|
333
|
+
* .addScope('organization', { required: true })
|
|
334
|
+
* ```
|
|
335
|
+
*/
|
|
336
|
+
addScope<TScopeKey extends string>(key: TScope extends never ? TScopeKey : never, options?: IgniterJobScopeOptions): IgniterJobsBuilder<TContext, TQueues, TScopeKey, TActor>;
|
|
337
|
+
/**
|
|
338
|
+
* Add an actor for auditing (only one actor type allowed).
|
|
339
|
+
* Actors are used to track who initiated jobs.
|
|
340
|
+
*
|
|
341
|
+
* @param key - Actor key (e.g., 'user', 'system')
|
|
342
|
+
* @param options - Optional actor configuration
|
|
343
|
+
* @returns The builder with actor configured
|
|
344
|
+
*
|
|
345
|
+
* @example
|
|
346
|
+
* ```typescript
|
|
347
|
+
* .addActor('user', { description: 'The user who initiated the job' })
|
|
348
|
+
* ```
|
|
349
|
+
*/
|
|
350
|
+
addActor<TActorKey extends string>(key: TActor extends never ? TActorKey : never, options?: IgniterJobActorOptions): IgniterJobsBuilder<TContext, TQueues, TScope, TActorKey>;
|
|
351
|
+
/**
|
|
352
|
+
* Configure telemetry for observability (optional).
|
|
353
|
+
*
|
|
354
|
+
* @param telemetry - IgniterTelemetry instance
|
|
355
|
+
* @returns The builder with telemetry configured
|
|
356
|
+
*
|
|
357
|
+
* @example
|
|
358
|
+
* ```typescript
|
|
359
|
+
* .withTelemetry(telemetry)
|
|
360
|
+
* ```
|
|
361
|
+
*/
|
|
362
|
+
withTelemetry(telemetry: IgniterTelemetry): IgniterJobsBuilder<TContext, TQueues, TScope, TActor>;
|
|
363
|
+
/**
|
|
364
|
+
* Configure a custom logger (optional).
|
|
365
|
+
*
|
|
366
|
+
* @param logger - Logger instance
|
|
367
|
+
* @returns The builder with logger configured
|
|
368
|
+
*
|
|
369
|
+
* @example
|
|
370
|
+
* ```typescript
|
|
371
|
+
* .withLogger(customLogger)
|
|
372
|
+
* ```
|
|
373
|
+
*/
|
|
374
|
+
withLogger(logger: IgniterLogger): IgniterJobsBuilder<TContext, TQueues, TScope, TActor>;
|
|
375
|
+
/**
|
|
376
|
+
* Configure default job options applied to all jobs.
|
|
377
|
+
*
|
|
378
|
+
* @param defaults - Default job configuration
|
|
379
|
+
* @returns The builder with defaults configured
|
|
380
|
+
*
|
|
381
|
+
* @example
|
|
382
|
+
* ```typescript
|
|
383
|
+
* .withDefaults({
|
|
384
|
+
* retry: { attempts: 3, backoff: { type: 'exponential', delay: 1000 } },
|
|
385
|
+
* timeout: 30000,
|
|
386
|
+
* removeOnComplete: { count: 1000 },
|
|
387
|
+
* })
|
|
388
|
+
* ```
|
|
389
|
+
*/
|
|
390
|
+
withDefaults(defaults: {
|
|
391
|
+
retry?: {
|
|
392
|
+
attempts?: number;
|
|
393
|
+
backoff?: {
|
|
394
|
+
type: 'exponential' | 'fixed' | 'linear';
|
|
395
|
+
delay: number;
|
|
396
|
+
maxDelay?: number;
|
|
397
|
+
};
|
|
398
|
+
};
|
|
399
|
+
timeout?: number;
|
|
400
|
+
removeOnComplete?: boolean | {
|
|
401
|
+
count?: number;
|
|
402
|
+
age?: number;
|
|
403
|
+
};
|
|
404
|
+
removeOnFail?: boolean | {
|
|
405
|
+
count?: number;
|
|
406
|
+
age?: number;
|
|
407
|
+
};
|
|
408
|
+
}): IgniterJobsBuilder<TContext, TQueues, TScope, TActor>;
|
|
409
|
+
/**
|
|
410
|
+
* Build the IgniterJobs instance.
|
|
411
|
+
* Validates configuration and returns a fully typed jobs instance.
|
|
412
|
+
*
|
|
413
|
+
* @returns The IgniterJobs instance with proxy for typed access
|
|
414
|
+
*
|
|
415
|
+
* @throws {IgniterJobsError} If adapter is not configured
|
|
416
|
+
* @throws {IgniterJobsError} If context is not configured
|
|
417
|
+
* @throws {IgniterJobsError} If no queues are defined
|
|
418
|
+
*
|
|
419
|
+
* @example
|
|
420
|
+
* ```typescript
|
|
421
|
+
* const jobs = IgniterJobs.create<AppContext>()
|
|
422
|
+
* .withAdapter(adapter)
|
|
423
|
+
* .withContext(() => context)
|
|
424
|
+
* .addQueue(emailQueue)
|
|
425
|
+
* .build()
|
|
426
|
+
*
|
|
427
|
+
* // Now use with full type safety
|
|
428
|
+
* await jobs.email.sendWelcome.dispatch({ userId: '123' })
|
|
429
|
+
* ```
|
|
430
|
+
*/
|
|
431
|
+
build(): IgniterJobsInstance<TContext, TQueues, TScope, TActor>;
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* Type for the built IgniterJobs instance.
|
|
435
|
+
* Provides typed access to queues, jobs, and management APIs.
|
|
436
|
+
*/
|
|
437
|
+
type IgniterJobsInstance<TContext, TQueues extends Record<string, IgniterQueueBuilderState<TContext, any, any>>, TScope extends string, TActor extends string> = {
|
|
438
|
+
[QueueName in keyof TQueues]: IgniterQueueProxy<TContext, TQueues[QueueName]['jobs'], TQueues[QueueName]['crons'], TScope, TActor>;
|
|
439
|
+
} & {
|
|
440
|
+
subscribe: IgniterJobsGlobalSubscribe;
|
|
441
|
+
search: IgniterJobsSearch;
|
|
442
|
+
worker: IgniterJobsWorkerBuilder<TQueues>;
|
|
443
|
+
shutdown: () => Promise<void>;
|
|
444
|
+
};
|
|
445
|
+
/**
|
|
446
|
+
* Queue proxy type providing access to jobs and queue management.
|
|
447
|
+
*/
|
|
448
|
+
type IgniterQueueProxy<TContext, TJobs extends Record<string, IgniterJobDefinition<TContext, any, any>>, TCrons extends Record<string, IgniterCronDefinition<TContext, any>>, TScope extends string, TActor extends string> = {
|
|
449
|
+
[JobName in keyof TJobs]: IgniterJobProxy<TContext, TJobs[JobName] extends IgniterJobDefinition<TContext, infer TInput, infer TOutput> ? TInput : unknown, TJobs[JobName] extends IgniterJobDefinition<TContext, infer TInput, infer TOutput> ? TOutput : unknown, TScope, TActor>;
|
|
450
|
+
} & {
|
|
451
|
+
subscribe: (handler: (event: any) => void | Promise<void>) => Promise<() => Promise<void>>;
|
|
452
|
+
list: (options?: {
|
|
453
|
+
status?: string[];
|
|
454
|
+
limit?: number;
|
|
455
|
+
}) => Promise<any[]>;
|
|
456
|
+
get: () => IgniterQueueManagement;
|
|
457
|
+
};
|
|
458
|
+
/**
|
|
459
|
+
* Job proxy type providing dispatch, schedule, and job management.
|
|
460
|
+
*/
|
|
461
|
+
interface IgniterJobProxy<TContext, TInput, TOutput, TScope extends string, TActor extends string> {
|
|
462
|
+
dispatch: IgniterJobDispatch<TInput, TScope, TActor>;
|
|
463
|
+
schedule: IgniterJobSchedule<TInput, TScope, TActor>;
|
|
464
|
+
get: (jobId: string) => IgniterJobManagement;
|
|
465
|
+
many: (jobIds: string[]) => IgniterJobBatchManagement;
|
|
466
|
+
pause: () => Promise<void>;
|
|
467
|
+
resume: () => Promise<void>;
|
|
468
|
+
subscribe: (handler: (event: any) => void | Promise<void>) => Promise<() => Promise<void>>;
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
* Job dispatch function type.
|
|
472
|
+
*/
|
|
473
|
+
type IgniterJobDispatch<TInput, TScope extends string, TActor extends string> = (input: TInput, options?: IgniterJobDispatchOptions<TScope, TActor>) => Promise<string>;
|
|
474
|
+
/**
|
|
475
|
+
* Job dispatch options.
|
|
476
|
+
*/
|
|
477
|
+
interface IgniterJobDispatchOptions<TScope extends string, TActor extends string> {
|
|
478
|
+
jobId?: string;
|
|
479
|
+
delay?: number;
|
|
480
|
+
priority?: number;
|
|
481
|
+
scope?: TScope extends never ? never : {
|
|
482
|
+
id: string;
|
|
483
|
+
tags?: Record<string, string | number | boolean>;
|
|
484
|
+
};
|
|
485
|
+
actor?: TActor extends never ? never : {
|
|
486
|
+
id?: string;
|
|
487
|
+
tags?: Record<string, string | number | boolean>;
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
/**
|
|
491
|
+
* Job schedule function type.
|
|
492
|
+
*/
|
|
493
|
+
type IgniterJobSchedule<TInput, TScope extends string, TActor extends string> = (params: {
|
|
494
|
+
input: TInput;
|
|
495
|
+
at?: Date;
|
|
496
|
+
cron?: string;
|
|
497
|
+
every?: number;
|
|
498
|
+
timezone?: string;
|
|
499
|
+
jobId?: string;
|
|
500
|
+
scope?: TScope extends never ? never : {
|
|
501
|
+
id: string;
|
|
502
|
+
tags?: Record<string, string | number | boolean>;
|
|
503
|
+
};
|
|
504
|
+
actor?: TActor extends never ? never : {
|
|
505
|
+
id?: string;
|
|
506
|
+
tags?: Record<string, string | number | boolean>;
|
|
507
|
+
};
|
|
508
|
+
}) => Promise<string>;
|
|
509
|
+
/**
|
|
510
|
+
* Single job management operations.
|
|
511
|
+
*/
|
|
512
|
+
interface IgniterJobManagement {
|
|
513
|
+
retrieve: () => Promise<any>;
|
|
514
|
+
retry: () => Promise<void>;
|
|
515
|
+
remove: () => Promise<void>;
|
|
516
|
+
state: () => Promise<string | null>;
|
|
517
|
+
progress: () => Promise<number>;
|
|
518
|
+
logs: () => Promise<any[]>;
|
|
519
|
+
promote: () => Promise<void>;
|
|
520
|
+
move: (state: 'failed' | 'completed', reason?: string) => Promise<void>;
|
|
521
|
+
}
|
|
522
|
+
/**
|
|
523
|
+
* Batch job management operations.
|
|
524
|
+
*/
|
|
525
|
+
interface IgniterJobBatchManagement {
|
|
526
|
+
retry: () => Promise<void>;
|
|
527
|
+
remove: () => Promise<void>;
|
|
528
|
+
}
|
|
529
|
+
/**
|
|
530
|
+
* Queue management operations.
|
|
531
|
+
*/
|
|
532
|
+
interface IgniterQueueManagement {
|
|
533
|
+
retrieve: () => Promise<any>;
|
|
534
|
+
pause: () => Promise<void>;
|
|
535
|
+
resume: () => Promise<void>;
|
|
536
|
+
drain: () => Promise<number>;
|
|
537
|
+
clean: (options: {
|
|
538
|
+
status: string | string[];
|
|
539
|
+
olderThan?: number;
|
|
540
|
+
limit?: number;
|
|
541
|
+
}) => Promise<number>;
|
|
542
|
+
obliterate: (options?: {
|
|
543
|
+
force?: boolean;
|
|
544
|
+
}) => Promise<void>;
|
|
545
|
+
retryAll: () => Promise<number>;
|
|
546
|
+
}
|
|
547
|
+
/**
|
|
548
|
+
* Global subscribe function.
|
|
549
|
+
*/
|
|
550
|
+
type IgniterJobsGlobalSubscribe = (handler: (event: any) => void | Promise<void>) => Promise<() => Promise<void>>;
|
|
551
|
+
/**
|
|
552
|
+
* Search function.
|
|
553
|
+
*/
|
|
554
|
+
interface IgniterJobsSearch {
|
|
555
|
+
(target: 'queues', filter?: any): Promise<any[]>;
|
|
556
|
+
(target: 'jobs', filter?: any): Promise<any[]>;
|
|
557
|
+
(target: 'workers', filter?: any): Promise<any[]>;
|
|
558
|
+
}
|
|
559
|
+
/**
|
|
560
|
+
* Worker builder interface.
|
|
561
|
+
*/
|
|
562
|
+
interface IgniterJobsWorkerBuilder<TQueues> {
|
|
563
|
+
create: () => IgniterJobsWorkerConfig<TQueues>;
|
|
564
|
+
}
|
|
565
|
+
/**
|
|
566
|
+
* Worker configuration builder.
|
|
567
|
+
*/
|
|
568
|
+
interface IgniterJobsWorkerConfig<TQueues> {
|
|
569
|
+
forQueues: (...queues: (keyof TQueues)[]) => IgniterJobsWorkerConfig<TQueues>;
|
|
570
|
+
withConcurrency: (concurrency: number) => IgniterJobsWorkerConfig<TQueues>;
|
|
571
|
+
withLockDuration: (duration: number) => IgniterJobsWorkerConfig<TQueues>;
|
|
572
|
+
withLimiter: (config: {
|
|
573
|
+
max: number;
|
|
574
|
+
duration: number;
|
|
575
|
+
}) => IgniterJobsWorkerConfig<TQueues>;
|
|
576
|
+
onIdle: (callback: () => void) => IgniterJobsWorkerConfig<TQueues>;
|
|
577
|
+
start: () => Promise<any>;
|
|
578
|
+
}
|
|
579
|
+
/**
|
|
580
|
+
* Convenience alias for IgniterJobsBuilder.create().
|
|
581
|
+
*
|
|
582
|
+
* @example
|
|
583
|
+
* ```typescript
|
|
584
|
+
* import { IgniterJobs } from '@igniter-js/jobs'
|
|
585
|
+
*
|
|
586
|
+
* const jobs = IgniterJobs.create<AppContext>()
|
|
587
|
+
* .withAdapter(adapter)
|
|
588
|
+
* .withContext(() => context)
|
|
589
|
+
* .addQueue(emailQueue)
|
|
590
|
+
* .build()
|
|
591
|
+
* ```
|
|
592
|
+
*/
|
|
593
|
+
declare const IgniterJobs: {
|
|
594
|
+
create: typeof IgniterJobsBuilder.create;
|
|
595
|
+
};
|
|
596
|
+
|
|
597
|
+
/**
|
|
598
|
+
* @fileoverview Builder for creating IgniterQueue instances
|
|
599
|
+
* @module @igniter-js/jobs/builders/igniter-queue
|
|
600
|
+
*
|
|
601
|
+
* @description
|
|
602
|
+
* Provides a fluent builder API for defining queues with jobs and cron jobs.
|
|
603
|
+
* Queues group related jobs together and provide type-safe job definitions.
|
|
604
|
+
*
|
|
605
|
+
* @example
|
|
606
|
+
* ```typescript
|
|
607
|
+
* import { IgniterQueue } from '@igniter-js/jobs'
|
|
608
|
+
* import { z } from 'zod'
|
|
609
|
+
*
|
|
610
|
+
* const emailQueue = IgniterQueue.create<AppContext>('email')
|
|
611
|
+
* .addJob('sendWelcome', {
|
|
612
|
+
* input: z.object({
|
|
613
|
+
* userId: z.string(),
|
|
614
|
+
* email: z.string().email(),
|
|
615
|
+
* }),
|
|
616
|
+
* retry: { attempts: 3 },
|
|
617
|
+
* handler: async (ctx) => {
|
|
618
|
+
* await ctx.context.mailer.send({
|
|
619
|
+
* to: ctx.input.email,
|
|
620
|
+
* template: 'welcome',
|
|
621
|
+
* })
|
|
622
|
+
* },
|
|
623
|
+
* })
|
|
624
|
+
* .addCron('cleanupExpired', {
|
|
625
|
+
* expression: '0 0 * * *',
|
|
626
|
+
* handler: async (ctx) => {
|
|
627
|
+
* await ctx.context.db.cleanup.expiredSessions()
|
|
628
|
+
* },
|
|
629
|
+
* })
|
|
630
|
+
* .build()
|
|
631
|
+
* ```
|
|
632
|
+
*/
|
|
633
|
+
|
|
634
|
+
/**
|
|
635
|
+
* Builder for creating queue configurations.
|
|
636
|
+
*
|
|
637
|
+
* @typeParam TContext - Application context type
|
|
638
|
+
* @typeParam TJobs - Map of job names to definitions
|
|
639
|
+
* @typeParam TCrons - Map of cron names to definitions
|
|
640
|
+
*/
|
|
641
|
+
declare class IgniterQueueBuilder<TContext = unknown, TJobs extends Record<string, IgniterJobDefinition<TContext, any, any>> = Record<string, never>, TCrons extends Record<string, IgniterCronDefinition<TContext, any>> = Record<string, never>> {
|
|
642
|
+
private readonly state;
|
|
643
|
+
private constructor();
|
|
644
|
+
/**
|
|
645
|
+
* Create a new queue builder.
|
|
646
|
+
*
|
|
647
|
+
* @param name - Queue name (kebab-case, e.g., 'email', 'payment-processing')
|
|
648
|
+
* @returns A new IgniterQueueBuilder instance
|
|
649
|
+
*
|
|
650
|
+
* @example
|
|
651
|
+
* ```typescript
|
|
652
|
+
* const emailQueue = IgniterQueue.create<AppContext>('email')
|
|
653
|
+
* ```
|
|
654
|
+
*/
|
|
655
|
+
static create<TContext = unknown>(name: string): IgniterQueueBuilder<TContext>;
|
|
656
|
+
/**
|
|
657
|
+
* Add a job definition to the queue.
|
|
658
|
+
*
|
|
659
|
+
* @param name - Job name (camelCase, e.g., 'sendWelcome', 'processPayment')
|
|
660
|
+
* @param definition - Job definition with input schema, handler, retry config, etc.
|
|
661
|
+
* @returns The builder with the new job added
|
|
662
|
+
*
|
|
663
|
+
* @example
|
|
664
|
+
* ```typescript
|
|
665
|
+
* .addJob('sendWelcome', {
|
|
666
|
+
* input: z.object({
|
|
667
|
+
* userId: z.string(),
|
|
668
|
+
* email: z.string().email(),
|
|
669
|
+
* }),
|
|
670
|
+
* retry: { attempts: 3, backoff: { type: 'exponential', delay: 1000 } },
|
|
671
|
+
* handler: async (ctx) => {
|
|
672
|
+
* await ctx.context.mailer.send({
|
|
673
|
+
* to: ctx.input.email,
|
|
674
|
+
* template: 'welcome',
|
|
675
|
+
* })
|
|
676
|
+
* },
|
|
677
|
+
* })
|
|
678
|
+
* ```
|
|
679
|
+
*/
|
|
680
|
+
addJob<TName extends string, TInput, TOutput = void>(name: TName extends keyof TJobs ? never : TName, definition: IgniterJobDefinition<TContext, TInput, TOutput>): IgniterQueueBuilder<TContext, TJobs & {
|
|
681
|
+
[K in TName]: IgniterJobDefinition<TContext, TInput, TOutput>;
|
|
682
|
+
}, TCrons>;
|
|
683
|
+
/**
|
|
684
|
+
* Add a cron job definition to the queue.
|
|
685
|
+
*
|
|
686
|
+
* @param name - Cron job name (camelCase)
|
|
687
|
+
* @param definition - Cron definition with expression, handler, etc.
|
|
688
|
+
* @returns The builder with the new cron added
|
|
689
|
+
*
|
|
690
|
+
* @example
|
|
691
|
+
* ```typescript
|
|
692
|
+
* .addCron('cleanupExpired', {
|
|
693
|
+
* expression: '0 0 * * *', // Every day at midnight
|
|
694
|
+
* timezone: 'America/New_York',
|
|
695
|
+
* handler: async (ctx) => {
|
|
696
|
+
* await ctx.context.db.cleanup.expiredSessions()
|
|
697
|
+
* },
|
|
698
|
+
* })
|
|
699
|
+
* ```
|
|
700
|
+
*/
|
|
701
|
+
addCron<TName extends string, TOutput = void>(name: TName extends keyof TCrons ? never : TName, definition: IgniterCronDefinition<TContext, TOutput>): IgniterQueueBuilder<TContext, TJobs, TCrons & {
|
|
702
|
+
[K in TName]: IgniterCronDefinition<TContext, TOutput>;
|
|
703
|
+
}>;
|
|
704
|
+
/**
|
|
705
|
+
* Build the queue configuration.
|
|
706
|
+
*
|
|
707
|
+
* @returns The queue configuration ready to be registered with IgniterJobs
|
|
708
|
+
*
|
|
709
|
+
* @example
|
|
710
|
+
* ```typescript
|
|
711
|
+
* const emailQueue = IgniterQueue.create<AppContext>('email')
|
|
712
|
+
* .addJob('sendWelcome', { ... })
|
|
713
|
+
* .build()
|
|
714
|
+
* ```
|
|
715
|
+
*/
|
|
716
|
+
build(): IgniterQueueBuilderState<TContext, TJobs, TCrons>;
|
|
717
|
+
}
|
|
718
|
+
/**
|
|
719
|
+
* Convenience alias for IgniterQueueBuilder.create().
|
|
720
|
+
* Use this to create queue configurations with a fluent API.
|
|
721
|
+
*
|
|
722
|
+
* @example
|
|
723
|
+
* ```typescript
|
|
724
|
+
* import { IgniterQueue } from '@igniter-js/jobs'
|
|
725
|
+
*
|
|
726
|
+
* const emailQueue = IgniterQueue.create<AppContext>('email')
|
|
727
|
+
* .addJob('sendWelcome', { ... })
|
|
728
|
+
* .build()
|
|
729
|
+
* ```
|
|
730
|
+
*/
|
|
731
|
+
declare const IgniterQueue: {
|
|
732
|
+
create: typeof IgniterQueueBuilder.create;
|
|
733
|
+
};
|
|
734
|
+
|
|
735
|
+
/**
|
|
736
|
+
* @fileoverview Builder for creating workers
|
|
737
|
+
* @module @igniter-js/jobs/builders/igniter-worker
|
|
738
|
+
*/
|
|
739
|
+
|
|
740
|
+
/**
|
|
741
|
+
* Builder for configuring and starting workers.
|
|
742
|
+
*
|
|
743
|
+
* @example
|
|
744
|
+
* ```typescript
|
|
745
|
+
* const worker = await jobs.worker
|
|
746
|
+
* .create()
|
|
747
|
+
* .forQueues('email', 'payment')
|
|
748
|
+
* .withConcurrency(10)
|
|
749
|
+
* .withLimiter({ max: 100, duration: 60000 })
|
|
750
|
+
* .start()
|
|
751
|
+
* ```
|
|
752
|
+
*/
|
|
753
|
+
declare class IgniterWorkerBuilder {
|
|
754
|
+
private state;
|
|
755
|
+
private adapter;
|
|
756
|
+
private jobHandler;
|
|
757
|
+
private availableQueues;
|
|
758
|
+
constructor(adapter: IgniterJobsAdapter, jobHandler: AdapterJobHandler, availableQueues: string[]);
|
|
759
|
+
/**
|
|
760
|
+
* Specify which queues this worker should process.
|
|
761
|
+
* If not called, worker processes all queues.
|
|
762
|
+
*
|
|
763
|
+
* @param queues - Queue names to process
|
|
764
|
+
* @returns The builder for chaining
|
|
765
|
+
*
|
|
766
|
+
* @example
|
|
767
|
+
* ```typescript
|
|
768
|
+
* .forQueues('email', 'payment')
|
|
769
|
+
* ```
|
|
770
|
+
*/
|
|
771
|
+
forQueues(...queues: string[]): this;
|
|
772
|
+
/**
|
|
773
|
+
* Set the concurrency level (jobs processed in parallel per queue).
|
|
774
|
+
*
|
|
775
|
+
* @param concurrency - Number of parallel jobs
|
|
776
|
+
* @returns The builder for chaining
|
|
777
|
+
*
|
|
778
|
+
* @example
|
|
779
|
+
* ```typescript
|
|
780
|
+
* .withConcurrency(10)
|
|
781
|
+
* ```
|
|
782
|
+
*/
|
|
783
|
+
withConcurrency(concurrency: number): this;
|
|
784
|
+
/**
|
|
785
|
+
* Set the lock duration in milliseconds.
|
|
786
|
+
* Jobs are locked for this duration while being processed.
|
|
787
|
+
*
|
|
788
|
+
* @param duration - Lock duration in milliseconds
|
|
789
|
+
* @returns The builder for chaining
|
|
790
|
+
*
|
|
791
|
+
* @example
|
|
792
|
+
* ```typescript
|
|
793
|
+
* .withLockDuration(30000) // 30 seconds
|
|
794
|
+
* ```
|
|
795
|
+
*/
|
|
796
|
+
withLockDuration(duration: number): this;
|
|
797
|
+
/**
|
|
798
|
+
* Configure rate limiting for the worker.
|
|
799
|
+
*
|
|
800
|
+
* @param config - Rate limiter configuration
|
|
801
|
+
* @returns The builder for chaining
|
|
802
|
+
*
|
|
803
|
+
* @example
|
|
804
|
+
* ```typescript
|
|
805
|
+
* .withLimiter({ max: 100, duration: 60000 }) // 100 jobs per minute
|
|
806
|
+
* ```
|
|
807
|
+
*/
|
|
808
|
+
withLimiter(config: {
|
|
809
|
+
max: number;
|
|
810
|
+
duration: number;
|
|
811
|
+
}): this;
|
|
812
|
+
/**
|
|
813
|
+
* Set a callback to be called when the worker becomes idle.
|
|
814
|
+
*
|
|
815
|
+
* @param callback - Idle callback
|
|
816
|
+
* @returns The builder for chaining
|
|
817
|
+
*
|
|
818
|
+
* @example
|
|
819
|
+
* ```typescript
|
|
820
|
+
* .onIdle(() => console.log('Worker is idle'))
|
|
821
|
+
* ```
|
|
822
|
+
*/
|
|
823
|
+
onIdle(callback: () => void): this;
|
|
824
|
+
/**
|
|
825
|
+
* Start the worker.
|
|
826
|
+
*
|
|
827
|
+
* @returns Worker handle for management
|
|
828
|
+
*
|
|
829
|
+
* @example
|
|
830
|
+
* ```typescript
|
|
831
|
+
* const worker = await jobs.worker
|
|
832
|
+
* .create()
|
|
833
|
+
* .forQueues('email')
|
|
834
|
+
* .start()
|
|
835
|
+
*
|
|
836
|
+
* // Later
|
|
837
|
+
* await worker.pause()
|
|
838
|
+
* await worker.close()
|
|
839
|
+
* ```
|
|
840
|
+
*/
|
|
841
|
+
start(): Promise<IgniterWorkerHandle>;
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
/**
|
|
845
|
+
* @fileoverview IgniterJobs runtime class with proxy-based API
|
|
846
|
+
* @module @igniter-js/jobs/core/igniter-jobs
|
|
847
|
+
*/
|
|
848
|
+
|
|
849
|
+
/**
|
|
850
|
+
* Configuration passed to IgniterJobsRuntime.
|
|
851
|
+
*/
|
|
852
|
+
interface IgniterJobsRuntimeConfig<TContext, TQueues extends Record<string, IgniterQueueBuilderState<TContext, any, any>>, TScope extends string, TActor extends string> {
|
|
853
|
+
adapter: IgniterJobsAdapter;
|
|
854
|
+
context: () => TContext | Promise<TContext>;
|
|
855
|
+
queues: TQueues;
|
|
856
|
+
scope?: {
|
|
857
|
+
key: TScope;
|
|
858
|
+
options?: IgniterJobScopeOptions;
|
|
859
|
+
};
|
|
860
|
+
actor?: {
|
|
861
|
+
key: TActor;
|
|
862
|
+
options?: IgniterJobActorOptions;
|
|
863
|
+
};
|
|
864
|
+
telemetry?: IgniterTelemetry;
|
|
865
|
+
logger?: IgniterLogger;
|
|
866
|
+
defaults?: {
|
|
867
|
+
retry?: {
|
|
868
|
+
attempts?: number;
|
|
869
|
+
backoff?: any;
|
|
870
|
+
};
|
|
871
|
+
timeout?: number;
|
|
872
|
+
removeOnComplete?: boolean | {
|
|
873
|
+
count?: number;
|
|
874
|
+
age?: number;
|
|
875
|
+
};
|
|
876
|
+
removeOnFail?: boolean | {
|
|
877
|
+
count?: number;
|
|
878
|
+
age?: number;
|
|
879
|
+
};
|
|
880
|
+
};
|
|
881
|
+
}
|
|
882
|
+
/**
|
|
883
|
+
* Main runtime class for IgniterJobs.
|
|
884
|
+
* Creates proxy-based API for type-safe job access.
|
|
885
|
+
*/
|
|
886
|
+
declare class IgniterJobsRuntime<TContext, TQueues extends Record<string, IgniterQueueBuilderState<TContext, any, any>>, TScope extends string, TActor extends string> {
|
|
887
|
+
private readonly config;
|
|
888
|
+
private readonly queueNames;
|
|
889
|
+
constructor(config: IgniterJobsRuntimeConfig<TContext, TQueues, TScope, TActor>);
|
|
890
|
+
/**
|
|
891
|
+
* Create the main proxy that provides queue access and global methods.
|
|
892
|
+
*/
|
|
893
|
+
private createProxy;
|
|
894
|
+
/**
|
|
895
|
+
* Create a proxy for a specific queue.
|
|
896
|
+
*/
|
|
897
|
+
private createQueueProxy;
|
|
898
|
+
/**
|
|
899
|
+
* Create a proxy for a specific job.
|
|
900
|
+
*/
|
|
901
|
+
private createJobProxy;
|
|
902
|
+
/**
|
|
903
|
+
* Dispatch a job.
|
|
904
|
+
*/
|
|
905
|
+
private dispatchJob;
|
|
906
|
+
/**
|
|
907
|
+
* Schedule a job for future execution.
|
|
908
|
+
*/
|
|
909
|
+
private scheduleJob;
|
|
910
|
+
/**
|
|
911
|
+
* Create job management methods.
|
|
912
|
+
*/
|
|
913
|
+
private createJobManagement;
|
|
914
|
+
/**
|
|
915
|
+
* Create batch job management methods.
|
|
916
|
+
*/
|
|
917
|
+
private createJobBatchManagement;
|
|
918
|
+
/**
|
|
919
|
+
* Create queue management methods.
|
|
920
|
+
*/
|
|
921
|
+
private createQueueManagement;
|
|
922
|
+
/**
|
|
923
|
+
* Pause a specific job type.
|
|
924
|
+
*/
|
|
925
|
+
private pauseJobType;
|
|
926
|
+
/**
|
|
927
|
+
* Resume a specific job type.
|
|
928
|
+
*/
|
|
929
|
+
private resumeJobType;
|
|
930
|
+
/**
|
|
931
|
+
* Subscribe to events for a specific job.
|
|
932
|
+
*/
|
|
933
|
+
private subscribeToJob;
|
|
934
|
+
/**
|
|
935
|
+
* Create queue-level subscribe.
|
|
936
|
+
*/
|
|
937
|
+
private createQueueSubscribe;
|
|
938
|
+
/**
|
|
939
|
+
* List jobs in a queue.
|
|
940
|
+
*/
|
|
941
|
+
private listQueueJobs;
|
|
942
|
+
/**
|
|
943
|
+
* Create global subscribe.
|
|
944
|
+
*/
|
|
945
|
+
private createGlobalSubscribe;
|
|
946
|
+
/**
|
|
947
|
+
* Create search function.
|
|
948
|
+
*/
|
|
949
|
+
private createSearch;
|
|
950
|
+
/**
|
|
951
|
+
* Create worker builder.
|
|
952
|
+
*/
|
|
953
|
+
private createWorkerBuilder;
|
|
954
|
+
/**
|
|
955
|
+
* Create the job handler function for workers.
|
|
956
|
+
*/
|
|
957
|
+
private createJobHandler;
|
|
958
|
+
/**
|
|
959
|
+
* Shutdown the jobs instance.
|
|
960
|
+
*/
|
|
961
|
+
private shutdown;
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
/**
|
|
965
|
+
* @fileoverview Error class and error codes for @igniter-js/jobs
|
|
966
|
+
* @module @igniter-js/jobs/errors
|
|
967
|
+
*/
|
|
968
|
+
|
|
969
|
+
/**
|
|
970
|
+
* All possible error codes for IgniterJobs.
|
|
971
|
+
* Use these codes for programmatic error handling.
|
|
972
|
+
*
|
|
973
|
+
* @example
|
|
974
|
+
* ```typescript
|
|
975
|
+
* try {
|
|
976
|
+
* await jobs.email.sendWelcome.dispatch(input)
|
|
977
|
+
* } catch (error) {
|
|
978
|
+
* if (error instanceof IgniterJobsError) {
|
|
979
|
+
* switch (error.code) {
|
|
980
|
+
* case 'JOBS_ADAPTER_REQUIRED':
|
|
981
|
+
* // Handle missing adapter
|
|
982
|
+
* break
|
|
983
|
+
* case 'JOBS_DISPATCH_FAILED':
|
|
984
|
+
* // Handle dispatch failure
|
|
985
|
+
* break
|
|
986
|
+
* }
|
|
987
|
+
* }
|
|
988
|
+
* }
|
|
989
|
+
* ```
|
|
990
|
+
*/
|
|
991
|
+
declare const IGNITER_JOBS_ERROR_CODES: {
|
|
992
|
+
readonly JOBS_ADAPTER_REQUIRED: "JOBS_ADAPTER_REQUIRED";
|
|
993
|
+
readonly JOBS_CONTEXT_REQUIRED: "JOBS_CONTEXT_REQUIRED";
|
|
994
|
+
readonly JOBS_QUEUE_REQUIRED: "JOBS_QUEUE_REQUIRED";
|
|
995
|
+
readonly JOBS_CONFIGURATION_INVALID: "JOBS_CONFIGURATION_INVALID";
|
|
996
|
+
readonly JOBS_QUEUE_NOT_FOUND: "JOBS_QUEUE_NOT_FOUND";
|
|
997
|
+
readonly JOBS_QUEUE_ALREADY_EXISTS: "JOBS_QUEUE_ALREADY_EXISTS";
|
|
998
|
+
readonly JOBS_QUEUE_NAME_INVALID: "JOBS_QUEUE_NAME_INVALID";
|
|
999
|
+
readonly JOBS_QUEUE_PAUSE_FAILED: "JOBS_QUEUE_PAUSE_FAILED";
|
|
1000
|
+
readonly JOBS_QUEUE_RESUME_FAILED: "JOBS_QUEUE_RESUME_FAILED";
|
|
1001
|
+
readonly JOBS_QUEUE_DRAIN_FAILED: "JOBS_QUEUE_DRAIN_FAILED";
|
|
1002
|
+
readonly JOBS_QUEUE_CLEAN_FAILED: "JOBS_QUEUE_CLEAN_FAILED";
|
|
1003
|
+
readonly JOBS_QUEUE_OBLITERATE_FAILED: "JOBS_QUEUE_OBLITERATE_FAILED";
|
|
1004
|
+
readonly JOBS_JOB_NOT_FOUND: "JOBS_JOB_NOT_FOUND";
|
|
1005
|
+
readonly JOBS_JOB_ALREADY_EXISTS: "JOBS_JOB_ALREADY_EXISTS";
|
|
1006
|
+
readonly JOBS_JOB_NAME_INVALID: "JOBS_JOB_NAME_INVALID";
|
|
1007
|
+
readonly JOBS_JOB_HANDLER_REQUIRED: "JOBS_JOB_HANDLER_REQUIRED";
|
|
1008
|
+
readonly JOBS_CRON_NOT_FOUND: "JOBS_CRON_NOT_FOUND";
|
|
1009
|
+
readonly JOBS_CRON_ALREADY_EXISTS: "JOBS_CRON_ALREADY_EXISTS";
|
|
1010
|
+
readonly JOBS_CRON_EXPRESSION_INVALID: "JOBS_CRON_EXPRESSION_INVALID";
|
|
1011
|
+
readonly JOBS_CRON_HANDLER_REQUIRED: "JOBS_CRON_HANDLER_REQUIRED";
|
|
1012
|
+
readonly JOBS_DISPATCH_FAILED: "JOBS_DISPATCH_FAILED";
|
|
1013
|
+
readonly JOBS_SCHEDULE_FAILED: "JOBS_SCHEDULE_FAILED";
|
|
1014
|
+
readonly JOBS_INPUT_REQUIRED: "JOBS_INPUT_REQUIRED";
|
|
1015
|
+
readonly JOBS_INPUT_VALIDATION_FAILED: "JOBS_INPUT_VALIDATION_FAILED";
|
|
1016
|
+
readonly JOBS_SCOPE_REQUIRED: "JOBS_SCOPE_REQUIRED";
|
|
1017
|
+
readonly JOBS_SCOPE_ALREADY_DEFINED: "JOBS_SCOPE_ALREADY_DEFINED";
|
|
1018
|
+
readonly JOBS_SCOPE_INVALID: "JOBS_SCOPE_INVALID";
|
|
1019
|
+
readonly JOBS_ACTOR_ALREADY_DEFINED: "JOBS_ACTOR_ALREADY_DEFINED";
|
|
1020
|
+
readonly JOBS_ACTOR_INVALID: "JOBS_ACTOR_INVALID";
|
|
1021
|
+
readonly JOBS_GET_FAILED: "JOBS_GET_FAILED";
|
|
1022
|
+
readonly JOBS_RETRY_FAILED: "JOBS_RETRY_FAILED";
|
|
1023
|
+
readonly JOBS_REMOVE_FAILED: "JOBS_REMOVE_FAILED";
|
|
1024
|
+
readonly JOBS_PROMOTE_FAILED: "JOBS_PROMOTE_FAILED";
|
|
1025
|
+
readonly JOBS_MOVE_FAILED: "JOBS_MOVE_FAILED";
|
|
1026
|
+
readonly JOBS_STATE_FAILED: "JOBS_STATE_FAILED";
|
|
1027
|
+
readonly JOBS_PROGRESS_FAILED: "JOBS_PROGRESS_FAILED";
|
|
1028
|
+
readonly JOBS_LOGS_FAILED: "JOBS_LOGS_FAILED";
|
|
1029
|
+
readonly JOBS_WORKER_CREATE_FAILED: "JOBS_WORKER_CREATE_FAILED";
|
|
1030
|
+
readonly JOBS_WORKER_START_FAILED: "JOBS_WORKER_START_FAILED";
|
|
1031
|
+
readonly JOBS_WORKER_STOP_FAILED: "JOBS_WORKER_STOP_FAILED";
|
|
1032
|
+
readonly JOBS_WORKER_NOT_FOUND: "JOBS_WORKER_NOT_FOUND";
|
|
1033
|
+
readonly JOBS_WORKER_ALREADY_RUNNING: "JOBS_WORKER_ALREADY_RUNNING";
|
|
1034
|
+
readonly JOBS_SUBSCRIBE_FAILED: "JOBS_SUBSCRIBE_FAILED";
|
|
1035
|
+
readonly JOBS_UNSUBSCRIBE_FAILED: "JOBS_UNSUBSCRIBE_FAILED";
|
|
1036
|
+
readonly JOBS_EVENT_EMIT_FAILED: "JOBS_EVENT_EMIT_FAILED";
|
|
1037
|
+
readonly JOBS_SEARCH_FAILED: "JOBS_SEARCH_FAILED";
|
|
1038
|
+
readonly JOBS_SEARCH_INVALID_TARGET: "JOBS_SEARCH_INVALID_TARGET";
|
|
1039
|
+
readonly JOBS_SHUTDOWN_FAILED: "JOBS_SHUTDOWN_FAILED";
|
|
1040
|
+
readonly JOBS_HANDLER_FAILED: "JOBS_HANDLER_FAILED";
|
|
1041
|
+
readonly JOBS_HANDLER_TIMEOUT: "JOBS_HANDLER_TIMEOUT";
|
|
1042
|
+
};
|
|
1043
|
+
/**
|
|
1044
|
+
* Type for all possible error codes.
|
|
1045
|
+
*/
|
|
1046
|
+
type IgniterJobsErrorCode = keyof typeof IGNITER_JOBS_ERROR_CODES;
|
|
1047
|
+
/**
|
|
1048
|
+
* Options for creating an IgniterJobsError.
|
|
1049
|
+
*/
|
|
1050
|
+
interface IgniterJobsErrorOptions {
|
|
1051
|
+
/**
|
|
1052
|
+
* Error code from IGNITER_JOBS_ERROR_CODES.
|
|
1053
|
+
*/
|
|
1054
|
+
code: IgniterJobsErrorCode;
|
|
1055
|
+
/**
|
|
1056
|
+
* Human-readable error message.
|
|
1057
|
+
*/
|
|
1058
|
+
message: string;
|
|
1059
|
+
/**
|
|
1060
|
+
* HTTP status code (for REST API responses).
|
|
1061
|
+
* @default 500
|
|
1062
|
+
*/
|
|
1063
|
+
statusCode?: number;
|
|
1064
|
+
/**
|
|
1065
|
+
* Original error that caused this error.
|
|
1066
|
+
*/
|
|
1067
|
+
cause?: Error;
|
|
1068
|
+
/**
|
|
1069
|
+
* Additional details for debugging.
|
|
1070
|
+
*/
|
|
1071
|
+
details?: Record<string, unknown>;
|
|
1072
|
+
/**
|
|
1073
|
+
* Logger instance for logging the error.
|
|
1074
|
+
*/
|
|
1075
|
+
logger?: IgniterLogger;
|
|
1076
|
+
}
|
|
1077
|
+
/**
|
|
1078
|
+
* Custom error class for @igniter-js/jobs.
|
|
1079
|
+
* Extends IgniterError for consistent error handling across the Igniter ecosystem.
|
|
1080
|
+
*
|
|
1081
|
+
* @example
|
|
1082
|
+
* ```typescript
|
|
1083
|
+
* throw new IgniterJobsError({
|
|
1084
|
+
* code: 'JOBS_DISPATCH_FAILED',
|
|
1085
|
+
* message: 'Failed to dispatch job to queue',
|
|
1086
|
+
* statusCode: 500,
|
|
1087
|
+
* details: { queue: 'email', job: 'sendWelcome' },
|
|
1088
|
+
* })
|
|
1089
|
+
* ```
|
|
1090
|
+
*/
|
|
1091
|
+
declare class IgniterJobsError extends IgniterError {
|
|
1092
|
+
/**
|
|
1093
|
+
* Error code for programmatic handling.
|
|
1094
|
+
*/
|
|
1095
|
+
readonly code: IgniterJobsErrorCode;
|
|
1096
|
+
/**
|
|
1097
|
+
* Additional error details.
|
|
1098
|
+
*/
|
|
1099
|
+
readonly details?: Record<string, unknown>;
|
|
1100
|
+
constructor(options: IgniterJobsErrorOptions);
|
|
1101
|
+
/**
|
|
1102
|
+
* Convert error to a plain object for serialization.
|
|
1103
|
+
*/
|
|
1104
|
+
toJSON(): Record<string, unknown>;
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
export { AdapterJobHandler, IGNITER_JOBS_ERROR_CODES, IgniterCronDefinition, IgniterJobActorOptions, IgniterJobDefinition, IgniterJobScopeOptions, IgniterJobs, IgniterJobsAdapter, IgniterJobsBuilder, type IgniterJobsBuilderState, type IgniterJobsConfig, IgniterJobsError, type IgniterJobsErrorCode, IgniterJobsRuntime, IgniterQueue, IgniterQueueBuilder, type IgniterQueueBuilderState, IgniterQueueConfig, IgniterWorkerBuilder, type IgniterWorkerBuilderState, type IgniterWorkerHandle, type IgniterWorkerMetrics };
|