@@ -72,7 +72,7 @@ const config = {
7272
7373 worker: {
7474 concurrency: 5 ,
75- pollingInterval : ' 10ms ' ,
75+ idleDelay : ' 2s ' ,
7676 },
7777
7878 locations: [' ./app/jobs/**/*.ts' ],
@@ -242,7 +242,7 @@ export default class UrgentJob extends Job<Payload> {
242242 static readonly jobName = ' UrgentJob'
243243
244244 static options: JobOptions = {
245- priority: 1 , // Processed before default priority (5)
245+ priority: 1 , // Processed before default priority (5)
246246 }
247247
248248 async execute(): Promise <void > {
@@ -264,12 +264,13 @@ export default class ReliableJob extends Job<Payload> {
264264 static options: JobOptions = {
265265 maxRetries: 5 ,
266266 retry: {
267- backoff : () => exponentialBackoff ({
268- baseDelay: ' 1s' ,
269- maxDelay: ' 1m' ,
270- multiplier: 2 ,
271- jitter: true ,
272- }),
267+ backoff : () =>
268+ exponentialBackoff ({
269+ baseDelay: ' 1s' ,
270+ maxDelay: ' 1m' ,
271+ multiplier: 2 ,
272+ jitter: true ,
273+ }),
273274 },
274275 }
275276
@@ -309,25 +310,73 @@ You can also set a global timeout in the worker configuration:
309310``` typescript
310311const config = {
311312 worker: {
312- timeout: ' 1m' , // Default timeout for all jobs
313+ timeout: ' 1m' , // Default timeout for all jobs
313314 },
314315}
315316```
316317
318+ ## Dependency Injection
319+
320+ Use the ` jobFactory ` option to integrate with IoC containers for dependency injection. This allows your jobs to receive injected services in their constructor.
321+
322+ ``` typescript
323+ import { Worker } from ' @boringnode/queue'
324+ import type { JobFactory } from ' @boringnode/queue'
325+
326+ const worker = new Worker ({
327+ default: ' redis' ,
328+ 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+ },
334+ },
335+ })
336+ ```
337+
338+ Example with injected dependencies:
339+
340+ ``` typescript
341+ import { Job } from ' @boringnode/queue'
342+
343+ interface SendEmailPayload {
344+ to: string
345+ subject: string
346+ }
347+
348+ export default class SendEmailJob extends Job <SendEmailPayload > {
349+ static readonly jobName = ' SendEmailJob'
350+
351+ constructor (
352+ payload : SendEmailPayload ,
353+ private mailer : MailerService , // Injected by IoC container
354+ private logger : Logger // Injected by IoC container
355+ ) {
356+ super (payload )
357+ }
358+
359+ async execute(): Promise <void > {
360+ this .logger .info (` Sending email to ${this .payload .to } ` )
361+ await this .mailer .send (this .payload )
362+ }
363+ }
364+ ```
365+
366+ Without a ` jobFactory ` , jobs are instantiated with ` new JobClass(payload) ` .
367+
317368## Job Discovery
318369
319370The queue manager automatically discovers and registers jobs from the specified locations:
320371
321372``` typescript
322373const config = {
323- locations: [
324- ' ./app/jobs/**/*.ts' ,
325- ' ./modules/**/jobs/**/*.ts' ,
326- ],
374+ locations: [' ./app/jobs/**/*.ts' , ' ./modules/**/jobs/**/*.ts' ],
327375}
328376```
329377
330378Jobs must:
379+
331380- Extend the ` Job ` class
332381- Have a static ` jobName ` property
333382- Implement the ` execute ` method
@@ -357,20 +406,20 @@ By default, a simple console logger is used that only outputs warnings and error
357406
358407Performance comparison with BullMQ using realistic jobs (5ms simulated work per job):
359408
360- | Jobs | Concurrency | @boringnode/queue | BullMQ | Diff |
361- | ------| -------------| -------------------| --------| -------------- |
362- | 100 | 1 | 562ms | 596ms | 5.7% faster |
363- | 100 | 5 | 116ms | 117ms | ~ same |
364- | 100 | 10 | 62ms | 62ms | ~ same |
365- | 500 | 1 | 2728ms | 2798ms | 2.5% faster |
366- | 500 | 5 | 565ms | 565ms | ~ same |
367- | 500 | 10 | 287ms | 288ms | ~ same |
368- | 1000 | 1 | 5450ms | 5547ms | 1.7% faster |
369- | 1000 | 5 | 1096ms | 1116ms | 1.8% faster |
370- | 1000 | 10 | 565ms | 579ms | 2.4% faster |
371- | 100K | 5 | 110.5s | 112.3s | 1.5% faster |
372- | 100K | 10 | 56.2s | 57.5s | 2.1% faster |
373- | 100K | 20 | 29.1s | 29.6s | 1.7% faster |
409+ | Jobs | Concurrency | @boringnode/queue | BullMQ | Diff |
410+ | ------| -------------| -------------------| --------| -------------|
411+ | 100 | 1 | 562ms | 596ms | 5.7% faster |
412+ | 100 | 5 | 116ms | 117ms | ~ same |
413+ | 100 | 10 | 62ms | 62ms | ~ same |
414+ | 500 | 1 | 2728ms | 2798ms | 2.5% faster |
415+ | 500 | 5 | 565ms | 565ms | ~ same |
416+ | 500 | 10 | 287ms | 288ms | ~ same |
417+ | 1000 | 1 | 5450ms | 5547ms | 1.7% faster |
418+ | 1000 | 5 | 1096ms | 1116ms | 1.8% faster |
419+ | 1000 | 10 | 565ms | 579ms | 2.4% faster |
420+ | 100K | 5 | 110.5s | 112.3s | 1.5% faster |
421+ | 100K | 10 | 56.2s | 57.5s | 2.1% faster |
422+ | 100K | 20 | 29.1s | 29.6s | 1.7% faster |
374423
375424Run benchmarks yourself:
376425
0 commit comments