@@ -28,7 +28,7 @@ Create a job by extending the `Job` class:
2828
2929``` typescript
3030import { Job } from ' @boringnode/queue'
31- import type { JobOptions } from ' @boringnode/queue/types/main '
31+ import type { JobContext , JobOptions } from ' @boringnode/queue/types'
3232
3333interface SendEmailPayload {
3434 to: string
@@ -41,8 +41,12 @@ export default class SendEmailJob extends Job<SendEmailPayload> {
4141 queue: ' email' ,
4242 }
4343
44+ constructor (payload : SendEmailPayload , context : JobContext ) {
45+ super (payload , context )
46+ }
47+
4448 async execute(): Promise <void > {
45- console .log (` Sending email to: ${this .payload .to } ` )
49+ console .log (` [Attempt ${ this . context . attempt }] Sending email to: ${this .payload .to }` )
4650 }
4751}
4852```
@@ -227,10 +231,10 @@ Schedule jobs to run in the future:
227231
228232``` typescript
229233// Various time formats
230- await SendEmailJob .dispatch (payload ).in (' 30s' ) // 30 seconds
231- await SendEmailJob .dispatch (payload ).in (' 5m' ) // 5 minutes
232- await SendEmailJob .dispatch (payload ).in (' 2h' ) // 2 hours
233- await SendEmailJob .dispatch (payload ).in (' 1d' ) // 1 day
234+ await SendEmailJob .dispatch (payload ).in (' 30s' ) // 30 seconds
235+ await SendEmailJob .dispatch (payload ).in (' 5m' ) // 5 minutes
236+ await SendEmailJob .dispatch (payload ).in (' 2h' ) // 2 hours
237+ await SendEmailJob .dispatch (payload ).in (' 1d' ) // 1 day
234238```
235239
236240## Priority
@@ -295,7 +299,7 @@ export default class LimitedJob extends Job<Payload> {
295299 static readonly jobName = ' LimitedJob'
296300
297301 static options: JobOptions = {
298- timeout: ' 30s' , // Maximum execution time
302+ timeout: ' 30s' , // Maximum execution time
299303 failOnTimeout: false , // Retry on timeout (default)
300304 }
301305
@@ -315,22 +319,58 @@ const config = {
315319}
316320```
317321
322+ ## Job Context
323+
324+ Every job has access to execution context via ` this.context ` . This provides metadata about the current job execution:
325+
326+ ``` typescript
327+ import { Job } from ' @boringnode/queue'
328+ import type { JobContext } from ' @boringnode/queue'
329+
330+ export default class MyJob extends Job <Payload > {
331+ constructor (payload : Payload , context : JobContext ) {
332+ super (payload , context )
333+ }
334+
335+ async execute(): Promise <void > {
336+ console .log (` Job ID: ${this .context .jobId } ` )
337+ console .log (` Attempt: ${this .context .attempt } ` ) // 1, 2, 3...
338+ console .log (` Queue: ${this .context .queue } ` )
339+ console .log (` Priority: ${this .context .priority } ` )
340+ console .log (` Acquired at: ${this .context .acquiredAt } ` )
341+
342+ if (this .context .attempt > 1 ) {
343+ console .log (' This is a retry!' )
344+ }
345+ }
346+ }
347+ ```
348+
349+ ### Context Properties
350+
351+ | Property | Type | Description |
352+ | -------------- | ------ | ----------------------------------------------- |
353+ | ` jobId ` | string | Unique identifier for this job |
354+ | ` name ` | string | Job class name |
355+ | ` attempt ` | number | Current attempt number (1-based) |
356+ | ` queue ` | string | Queue name this job is being processed from |
357+ | ` priority ` | number | Job priority (lower = higher priority) |
358+ | ` acquiredAt ` | Date | When this job was acquired by the worker |
359+ | ` stalledCount ` | number | Times this job was recovered from stalled state |
360+
318361## Dependency Injection
319362
320363Use the ` jobFactory ` option to integrate with IoC containers for dependency injection. This allows your jobs to receive injected services in their constructor.
321364
322365``` typescript
323- import { Worker } from ' @boringnode/queue'
324- import type { JobFactory } from ' @boringnode/queue'
366+ import { QueueManager } from ' @boringnode/queue'
325367
326- const worker = new Worker ({
368+ await QueueManager . init ({
327369 default: ' redis' ,
328370 adapters: { redis: redis (connection ) },
329- worker: {
330- jobFactory : async (JobClass , payload ) => {
331- // Use your IoC container to instantiate jobs
332- return app .container .make (JobClass , [payload ])
333- },
371+ jobFactory : async (JobClass , payload , context ) => {
372+ // Use your IoC container to instantiate jobs
373+ return app .container .make (JobClass , [payload , context ])
334374 },
335375})
336376```
@@ -339,6 +379,7 @@ Example with injected dependencies:
339379
340380``` typescript
341381import { Job } from ' @boringnode/queue'
382+ import type { JobContext } from ' @boringnode/queue'
342383
343384interface SendEmailPayload {
344385 to: string
@@ -350,20 +391,21 @@ export default class SendEmailJob extends Job<SendEmailPayload> {
350391
351392 constructor (
352393 payload : SendEmailPayload ,
353- private mailer : MailerService , // Injected by IoC container
354- private logger : Logger // Injected by IoC container
394+ context : JobContext ,
395+ private mailer : MailerService , // Injected by IoC container
396+ private logger : Logger // Injected by IoC container
355397 ) {
356- super (payload )
398+ super (payload , context )
357399 }
358400
359401 async execute(): Promise <void > {
360- this .logger .info (` Sending email to ${this .payload .to } ` )
402+ this .logger .info (` [Attempt ${ this . context . attempt }] Sending email to ${this .payload .to }` )
361403 await this .mailer .send (this .payload )
362404 }
363405}
364406```
365407
366- Without a ` jobFactory ` , jobs are instantiated with ` new JobClass(payload) ` .
408+ Without a ` jobFactory ` , jobs are instantiated with ` new JobClass(payload, context ) ` .
367409
368410## Job Discovery
369411
@@ -407,7 +449,7 @@ By default, a simple console logger is used that only outputs warnings and error
407449Performance comparison with BullMQ using realistic jobs (5ms simulated work per job):
408450
409451| Jobs | Concurrency | @boringnode/queue | BullMQ | Diff |
410- | ------ | ------------- | ------------------- | -------- | ------------- |
452+ | ---- | ----------- | ----------------- | ------ | ----------- |
411453| 100 | 1 | 562ms | 596ms | 5.7% faster |
412454| 100 | 5 | 116ms | 117ms | ~ same |
413455| 100 | 10 | 62ms | 62ms | ~ same |
0 commit comments