ssut / nestjs-sqs

A project to make SQS easier to use within NestJS, with decorator-based handling and seamless NestJS-way integration.
MIT License
215 stars 53 forks source link

Dependency injection? #2

Closed Ciboulette closed 4 years ago

Ciboulette commented 4 years ago
import { Injectable } from "@nestjs/common";
import { SqsMessageHandler } from "@ssut/nestjs-sqs";
import { SyncsService } from "../syncs.service";

import { Jobtypes } from "./job.types";
export const SYNC_WORKSPACE_QUEUE_NAME = "syncs_workspace";

export const SYNCS_QUEUE_NAME = "syncs";

@Injectable()
export class SyncsConsumer {
  constructor(private readonly syncsService: SyncsService) {
    console.log(syncsService);
  }

  @SqsMessageHandler(SYNCS_QUEUE_NAME)
  public async processJob(message: AWS.SQS.Message) {
    const job = JSON.parse(message.Body);

    try {
      switch (job.type) {
        case Jobtypes.syncOne:
          console.log("received ", job);
          await this.syncsService.execute(job);

          break;

        default:
          throw `No job with type: ${job.type} exists in syncs queue`;
      }
    } catch (error) {
      console.log("error", error);
    }
  }
}

I've created this class as a consumer, and when receiving the message the syncService is not injected, is-it normal?

ssut commented 4 years ago

Probably not normal. Can you let me know syncsService exists in the constructor?

Ciboulette commented 4 years ago

The console.log in the constructor return 'undefined'

ssut commented 4 years ago

So it's not related to this project I think. You will have to import the module in which SyncsService belongs.

Shpadoinkle commented 2 years ago

For others who stumble across this same issue. It is not just related to this package, but almost all using sqs-consumer under the hood.

If it is the same issues we encountered. It was due to converting our originally Request scoped functionality to now use a new queue based worker, yet our classes were still scoped to the Request that now no longer exists.
Removing the scope from Injectable and refactoring solved the issue.

bryanmaraujo544 commented 1 year ago

I am still having this issue, how can I "remove the scope from Injectable"?