-
Notifications
You must be signed in to change notification settings - Fork 91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't define a concurrency with a 0 value using the processors
field of BullModuleOptions
#590
Comments
Temporary workaround for anyone who might be stuck with this as well as me: import { OnApplicationShutdown, Provider, DynamicModule } from '@nestjs/common';
import * as Bull from 'bull';
import { Queue } from 'bull';
import {
BullModule,
BullQueueProcessor,
BullModuleOptions,
BullModuleAsyncOptions,
getQueueOptionsToken,
getQueueToken,
} from '@nestjs/bull';
import {
isAdvancedProcessor,
isAdvancedSeparateProcessor,
isProcessorCallback,
isSeparateProcessor,
} from '@nestjs/bull/dist/utils/helpers';
import { createQueueOptionProviders } from '@nestjs/bull/dist/bull.providers';
function buildQueue(option: BullModuleOptions): Queue {
const queue: Queue = new Bull(option.name ? option.name : 'default', option);
if (option.processors) {
option.processors.forEach((processor: BullQueueProcessor) => {
let args = [];
if (isAdvancedProcessor(processor)) {
args.push(processor.name, processor.concurrency, processor.callback);
} else if (isAdvancedSeparateProcessor(processor)) {
args.push(processor.name, processor.concurrency, processor.path);
} else if (isSeparateProcessor(processor)) {
args.push(processor);
} else if (isProcessorCallback(processor)) {
args.push(processor);
}
// args = args.filter(arg => !!arg);
args = args.filter((arg) => arg !== undefined); // FIX for concurrency == 0
queue.process.call(queue, ...args);
});
}
((queue as unknown) as OnApplicationShutdown).onApplicationShutdown = function (
this: Queue,
) {
return this.close();
};
return queue;
}
function createQueueProviders(options: BullModuleOptions[]): Provider[] {
return options.map((option) => ({
provide: getQueueToken(option.name),
useFactory: (o: BullModuleOptions) => {
const queueName = o.name || option.name;
return buildQueue({ ...o, name: queueName });
},
inject: [getQueueOptionsToken(option.name)],
}));
}
export class AppBullModule extends BullModule {
static registerQueue(...options: BullModuleOptions[]): DynamicModule {
const queueProviders = createQueueProviders([].concat(options));
const queueOptionProviders = createQueueOptionProviders([].concat(options));
return {
...super.registerQueue(...options),
providers: [...queueOptionProviders, ...queueProviders],
};
}
static registerQueueAsync(
...options: BullModuleAsyncOptions[]
): DynamicModule {
const queueProviders = createQueueProviders([].concat(options));
// const asyncQueueOptionsProviders = options
// .map(queueOptions => this.createAsyncProviders(queueOptions))
// .reduce((a, b) => a.concat(b), []);
const old = super.registerQueueAsync(...options);
// TODO: check and avoid doubling providers if needed
return {
...old,
providers: [...old.providers, ...queueProviders],
};
}
} Another solution is to create Queue directly by using Bull package, not NestJS wrapper. |
Would you like to create a PR for this issue? |
kamilmysliwiec
added a commit
that referenced
this issue
Nov 4, 2020
Fixed in 0.2.3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm submitting a...
Current behavior
This issue almost completely similar to issue #258 .
The only difference that I needed to set up sandboxed processors, so I ended up using
BullModuleOptions.processors
field, but setting zero-concurrency did not get any effect. After some testing I found out that this is because0
is falsy value and skipped by the filter:Expected behavior
Simple workaround:
bull/lib/bull.providers.ts
Line 33 in d13ce90
Just one string to be changed to
Very similar change was introduced in the #259
(0f891c3#diff-5088cd359ffe5490625cf08afdeaa0d6bf6b0f967735abd547fc8abd3a3c4119R82)
To be honest, I have no experience in submitting proper PRs on GitHub yet, so cannot do this myself right now, but I hope that I've given enough info/examples here :)
Environment
The text was updated successfully, but these errors were encountered: