@@ -341,6 +341,44 @@ To resume a paused queue, use the `resume()` method, as follows:
341
341
await audioQueue.resume();
342
342
` ` `
343
343
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
+
344
382
#### Async configuration
345
383
346
384
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 .
@@ -408,40 +446,6 @@ BullModule.forRootAsync({
408
446
409
447
This construction works the same as ` useClass ` with one critical difference - ` BullModule ` will lookup imported modules to reuse an existing ` ConfigService ` instead of instantiating a new one.
410
448
411
- #### Separate processes
412
-
413
- This module allows you to run your job handlers in fork processes.
414
- To do so , add the filesystem path to a file (or more ) exporting your processor function to the ` processors ` property of the BullModule options .
415
- You can read more on this subject in Bull ' s [documentation](https://github.com/OptimalBits/bull#separate-processes).
416
-
417
- Please note that because your function is being executed in a fork , NestJS ' Dependency Injection won' t be available. This means your job function will need to contain , or create all instances of external dependencies it may need.
418
-
419
- ` ` ` ts
420
- @@filename(app.module.ts)
421
- import { Module } from '@nestjs/common';
422
- import { BullModule } from 'nest-bull';
423
- import { join } from 'path';
424
-
425
- @Module({
426
- imports: [
427
- BullModule.forRoot({
428
- processors: [join(__dirname, 'processor.js')],
429
- }),
430
- ],
431
- })
432
- export class AppModule {}
433
- ` ` `
434
-
435
- ` ` ` ts
436
- @@filename(process.ts)
437
- import { Job, DoneCallback } from 'bull';
438
-
439
- export default function (job: Job, cb: DoneCallback) {
440
- console.log( ` [$ {process .pid }] $ {JSON .stringify (job .data )}` );
441
- cb(null, 'It works');
442
- }
443
- ` ` `
444
-
445
449
#### Example
446
450
447
451
A working example is available [here ](https : // github.com/nestjs/nest/tree/master/sample/26-queues).
0 commit comments