@@ -27,32 +27,78 @@ export interface JobOptions {
2727 failOnTimeout ?: boolean
2828}
2929
30- export type JobClass < T extends Job = Job > = ( new ( payload : any ) => T ) & { options ?: JobOptions }
30+ /**
31+ * Context information available to a job during execution.
32+ *
33+ * Provides metadata about the current job execution, including
34+ * retry information, queue details, and timing.
35+ *
36+ * @example
37+ * ```typescript
38+ * class MyJob extends Job<Payload> {
39+ * async execute() {
40+ * console.log(`Attempt ${this.context.attempt} of job ${this.context.jobId}`)
41+ * console.log(`Running on queue: ${this.context.queue}`)
42+ * }
43+ * }
44+ * ```
45+ */
46+ export interface JobContext {
47+ /** Unique identifier for this job */
48+ jobId : string
49+
50+ /** Job class name */
51+ name : string
52+
53+ /** Current attempt number (1-based: first attempt = 1) */
54+ attempt : number
55+
56+ /** Queue name this job is being processed from */
57+ queue : string
58+
59+ /** Job priority (lower number = higher priority) */
60+ priority : number
61+
62+ /** When this job was acquired by the worker for processing */
63+ acquiredAt : Date
64+
65+ /** Number of times this job has been recovered from stalled state */
66+ stalledCount : number
67+ }
68+
69+ export type JobClass < T extends Job = Job > = ( new ( payload : any , context : JobContext ) => T ) & {
70+ options ?: JobOptions
71+ }
3172
3273/**
3374 * Factory function for custom job instantiation.
3475 *
3576 * Use this to integrate with IoC containers for dependency injection.
36- * The factory receives the job class and payload , and must return
77+ * The factory receives the job class, payload, and context , and must return
3778 * a job instance (or a Promise that resolves to one).
3879 *
3980 * @param JobClass - The job class to instantiate
4081 * @param payload - The payload data for the job
82+ * @param context - The job execution context (jobId, attempt, queue, etc.)
4183 * @returns The job instance, or a Promise resolving to the instance
4284 *
4385 * @example
4486 * ```typescript
4587 * // With AdonisJS IoC container
4688 * const worker = new Worker({
4789 * worker: {
48- * jobFactory: async (JobClass, payload) => {
49- * return app.container.make(JobClass, [payload])
90+ * jobFactory: async (JobClass, payload, context ) => {
91+ * return app.container.make(JobClass, [payload, context ])
5092 * }
5193 * }
5294 * })
5395 * ```
5496 */
55- export type JobFactory = ( JobClass : JobClass , payload : any ) => Job | Promise < Job >
97+ export type JobFactory = (
98+ JobClass : JobClass ,
99+ payload : any ,
100+ context : JobContext
101+ ) => Job | Promise < Job >
56102
57103export interface RetryConfig {
58104 maxRetries ?: number
@@ -128,26 +174,6 @@ export interface WorkerConfig {
128174 * Called before the worker starts stopping.
129175 */
130176 onShutdownSignal ?: ( ) => void | Promise < void >
131-
132- /**
133- * Custom factory function for job instantiation.
134- *
135- * Use this to integrate with IoC containers for dependency injection.
136- * When provided, this factory is called instead of `new JobClass(payload)`.
137- *
138- * @example
139- * ```typescript
140- * const worker = new Worker({
141- * worker: {
142- * jobFactory: async (JobClass, payload) => {
143- * // Inject dependencies via IoC container
144- * return app.container.make(JobClass, [payload])
145- * }
146- * }
147- * })
148- * ```
149- */
150- jobFactory ?: JobFactory
151177}
152178
153179export type WorkerCycle =
@@ -166,4 +192,23 @@ export interface QueueManagerConfig {
166192 worker ?: WorkerConfig
167193 locations ?: string [ ]
168194 logger ?: Logger
195+
196+ /**
197+ * Custom factory function for job instantiation.
198+ *
199+ * Use this to integrate with IoC containers for dependency injection.
200+ * When provided, this factory is called instead of `new JobClass(payload, context)`.
201+ *
202+ * @example
203+ * ```typescript
204+ * await QueueManager.init({
205+ * default: 'redis',
206+ * adapters: { redis: redis() },
207+ * jobFactory: async (JobClass, payload, context) => {
208+ * return app.container.make(JobClass, [payload, context])
209+ * }
210+ * })
211+ * ```
212+ */
213+ jobFactory ?: JobFactory
169214}
0 commit comments