Skip to content
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

Dependency injection doesn't work as expected #32

Open
vitalnut opened this issue Sep 2, 2022 · 8 comments
Open

Dependency injection doesn't work as expected #32

vitalnut opened this issue Sep 2, 2022 · 8 comments

Comments

@vitalnut
Copy link

vitalnut commented Sep 2, 2022

It seems that the method decorated with @SqsMessageHandler() gets called just as plain function, ignoring the class it is defined in and its initialisation steps. My setup is following:

scheduled-jobs.handler.ts

@Injectable()
export class ScheduledJobsHandler {
    private readonly logger = new Logger(ScheduledJobsHandler.name);

    constructor(
        private createInvoicesJob: CreateInvoicesJob,
        private chargeInvoicesJob: ChargeInvoicesJob,
        private squareSyncJob: SquareSyncJob,
    ) {}

    @SqsMessageHandler(SCHEDULED_JOBS_QUEUE_NAME, false)
    async handleMessage(message: AWS.SQS.Message) {
        try {
            this.logger.log(
                `SQS event: ${message.Body},
                 QUEUE NAME: ${SCHEDULED_JOBS_QUEUE_NAME}`,
            );
            const body: any = JSON.parse(message.Body);
            switch (body.jobType) {
                case constants.SCHEDULED_JOBS.CREATE_INVOICES:
                    this.logger.log('Running CREATE INVOICE JOB');
                    await this.createInvoicesJob.createInvoicesForDay(body);
                    this.logger.log('Finished CREATE INVOICE JOB');
                    break;
                case ....
            }
        } catch (e) {
            this.logger.error('An error occurred while processing the message.', e);
        }
    }
}

app.module.ts

@Module({
    imports: [
        ConfigModule.forRoot({ isGlobal: true }),
        ...
        SqsModule.registerAsync({
            inject: [ConfigService],
            useFactory: (config: ConfigService) => ({
                consumers: [
                    {
                        name: config.get('SQS_SCHEDULED_JOBS_QUEUE_NAME'),
                        queueUrl: config.get('SQS_SCHEDULED_JOBS_QUEUE_URL'),
                        region: config.get('AWS_REGION'),
                    },
                ],
            }),
        }),
        MailModule,
    ],
    providers: [ScheduledJobsHandler],
})
export class AppModule {}

When the message is received in the debugger I can see that this.logger and this.createInvoicesJob and even constants are all undefined. Seems like the file itself is not executed, only the decorated function. Is this expected behaviour?

@bryanmaraujo544
Copy link

I'm facing exactly the same issue

@RafalKantor
Copy link

I think this not work with scope request
@Injectable({ scope: Scope.REQUEST })
When I did poc and replaced my request scoped providers with singleton it worked for me.

@LaurentRos
Copy link

Found in this example to inject the dependency using @Inject(forwardRef(() => Service)). Seems to do the job, not sure why we need that though...

@LaurentRos
Copy link

Actually forgot the @Injectable() on the Handler in my case...

@nllahat
Copy link

nllahat commented Mar 13, 2023

any solution for this problem ?

@CaoAnhDev
Copy link

I got the same issue. Can anyone help?

@SK-CSE
Copy link

SK-CSE commented Dec 6, 2023

regarding above implementation

@SqsMessageHandler(SCHEDULED_JOBS_QUEUE_NAME, false)

SqsMessageHandler have dificulty in taking dynamic name as above, please refer to my solution below

#8 (comment)

@kamran-codora
Copy link

kamran-codora commented Apr 22, 2024

@SK-CSE I am using this implementation but getting "Object is possibly undefined" error, although I tried giving it some default value as well. Any workaround for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants