Skip to content

Commit c1b62de

Browse files
Merge branch 'ndeitch-bull-separate-process'
2 parents 068a659 + fcc4b6c commit c1b62de

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

content/techniques/queues.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,44 @@ To resume a paused queue, use the `resume()` method, as follows:
341341
await audioQueue.resume();
342342
```
343343

344+
#### Separate processes
345+
346+
Job handlers can also be run in a separate (forked) process ([source](https://github.com/OptimalBits/bull#separate-processes)). This has several advantages:
347+
348+
- The process is sandboxed so if it crashes it does not affect the worker.
349+
- You can run blocking code without affecting the queue (jobs will not stall).
350+
- Much better utilization of multi-core CPUs.
351+
- Less connections to redis.
352+
353+
```ts
354+
@@filename(app.module)
355+
import { Module } from '@nestjs/common';
356+
import { BullModule } from '@nestjs/bull';
357+
import { join } from 'path';
358+
359+
@Module({
360+
imports: [
361+
BullModule.registerQueue({
362+
name: 'audio',
363+
processors: [join(__dirname, 'processor.js')],
364+
}),
365+
],
366+
})
367+
export class AppModule {}
368+
```
369+
370+
Please note that because your function is being executed in a forked process, Dependency Injection (and IoC container) won't be available. That means that your processor function will need to contain (or create) all instances of external dependencies it needs.
371+
372+
```ts
373+
@@filename(processor)
374+
import { Job, DoneCallback } from 'bull';
375+
376+
export default function (job: Job, cb: DoneCallback) {
377+
console.log(`[${process.pid}] ${JSON.stringify(job.data)}`);
378+
cb(null, 'It works');
379+
}
380+
```
381+
344382
#### Async configuration
345383

346384
You may want to pass `bull` options asynchronously instead of statically. In this case, use the `forRootAsync()` method which provides several ways to deal with async configuration. Likewise, if you want to pass queue options asynchronously, use the `registerQueueAsync()` method.

0 commit comments

Comments
 (0)