Skip to content

Commit

Permalink
fix(): register job ref before instatiating a provider
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Nov 16, 2020
1 parent 246939d commit 9f1c730
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
14 changes: 8 additions & 6 deletions lib/bull.explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,20 @@ export class BullExplorer implements OnModuleInit {
...args: unknown[]
) => {
const contextId = createContextId();
const contextInstance = await this.injector.loadPerContext(
instance,
moduleRef,
moduleRef.providers,
contextId,
);

if (this.moduleRef.registerRequestByContextId) {
// Additional condition to prevent breaking changes in
// applications that use @nestjs/bull older than v7.4.0.
const jobRef = args[0];
this.moduleRef.registerRequestByContextId(jobRef, contextId);
}

const contextInstance = await this.injector.loadPerContext(
instance,
moduleRef,
moduleRef.providers,
contextId,
);
return contextInstance[key].call(contextInstance, ...args);
};
args.push(callback);
Expand Down
2 changes: 1 addition & 1 deletion lib/bull.tokens.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { REQUEST } from '@nestjs/core';

export const JOB_DATA = REQUEST;
export const JOB_REF = REQUEST;
29 changes: 26 additions & 3 deletions lib/decorators/processor.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
import { SetMetadata } from '@nestjs/common';
import { Scope, SetMetadata } from '@nestjs/common';
import { SCOPE_OPTIONS_METADATA } from '@nestjs/common/constants';
import { BULL_MODULE_QUEUE } from '../bull.constants';

export interface ProcessorOptions {
/**
* Specifies the name of the queue to subscribe to.
*/
name?: string;
/**
* Specifies the lifetime of an injected Processor.
*/
scope?: Scope;
}

export const Processor = (queueName?: string): ClassDecorator =>
SetMetadata(BULL_MODULE_QUEUE, { name: queueName });
export function Processor(): ClassDecorator;
export function Processor(queueName: string): ClassDecorator;
export function Processor(processorOptions: ProcessorOptions): ClassDecorator;
export function Processor(
queueNameOrOptions?: string | ProcessorOptions,
): ClassDecorator {
const options =
queueNameOrOptions && typeof queueNameOrOptions === 'object'
? queueNameOrOptions
: { name: queueNameOrOptions };

// eslint-disable-next-line @typescript-eslint/ban-types
return (target: Function) => {
SetMetadata(SCOPE_OPTIONS_METADATA, options)(target);
SetMetadata(BULL_MODULE_QUEUE, options)(target);
};
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nestjs/bull",
"version": "0.2.3",
"version": "0.3.0-next.2",
"description": "Nest - modern, fast, powerful node.js web framework (@bull)",
"homepage": "https://github.com/nestjs/bull",
"bugs": {
Expand Down

0 comments on commit 9f1c730

Please sign in to comment.