nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
66.91k stars 7.55k forks source link

PRECONDITION_FAILED - reply consumer cannot acknowledge RabbitMQ noAck: false #12000

Closed vulongc3 closed 1 year ago

vulongc3 commented 1 year ago

Is there an existing issue for this?

Current behavior

I want to send ack manually on the consumer side to make sure messages have been properly processed. But I receive the error PRECONDITION_FAILED - reply consumer cannot acknowledge when emitting the message.

My code (following this post):

// consumer main.ts
async function bootstrap() {
  const app = await NestFactory.createMicroservice<MicroserviceOptions>(
    LogWorkerModule,
    {
      transport: Transport.RMQ,
      options: {
        urls: [config.rmq.uri],
        queue: 'log_queue',
        noAck: true,
        queueOptions: {
          durable: true,
        },
      },
    },
  );

  app.enableShutdownHooks();
  await app.listen().then(() => console.log(`log_worker: on`));
}
// producer module
@Module({
  imports: [
    ClientsModule.registerAsync([
      {
        name: 'LOG_WORKER',
        imports: [LibConfigModule],
        inject: [LibConfigService],
        useFactory: (config: LibConfigService) => {
          return {
            transport: Transport.RMQ,
            options: {
              urls: [config.rmq.uri],
              queue: 'log_queue',
              noAck: false,
              queueOptions: {
                durable: true,
              },
            },
          };
        },
      },
    ]),
  ],
  controllers: [UserBalanceLogController],
  providers: [UserBalanceLogService],
  exports: [UserBalanceLogService],
})
export class UserBalanceLogModule {}

Minimum reproduction code

Stackblitz

Steps to reproduce

No response

Expected behavior

Can manually ack the message on consumer side after processed with out error

Package

Other package

No response

NestJS version

^9.0.0

Packages versions

{
  "name": "lotopro_be",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  "scripts": {
    "build": "nest build",
    "format": "prettier --write \"apps/**/*.ts\" \"libs/**/*.ts\"",
    "dev": "npx concurrently \"yarn start:dev\" \\ \"yarn start:dev payout_worker\" \"yarn start:dev result_worker\" \"yarn start:dev log_worker\"",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/apps/api/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./apps/api/test/jest-e2e.json"
  },
  "dependencies": {
    "@nestjs/common": "^9.0.0",
    "@nestjs/core": "^9.0.0",
    "@nestjs/microservices": "^10.0.5",
    "@nestjs/mongoose": "^10.0.0",
    "@nestjs/platform-express": "^9.0.0",
    "amqp-connection-manager": "^4.1.13",
    "amqplib": "^0.10.3",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.14.0",
    "dayjs": "^1.11.9",
    "mongoose": "^7.3.2",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^7.2.0"
  },
  "devDependencies": {
    "@nestjs/cli": "^9.0.0",
    "@nestjs/schematics": "^9.0.0",
    "@nestjs/testing": "^9.0.0",
    "@types/express": "^4.17.13",
    "@types/jest": "29.2.4",
    "@types/node": "18.11.18",
    "@types/supertest": "^2.0.11",
    "@typescript-eslint/eslint-plugin": "^5.0.0",
    "@typescript-eslint/parser": "^5.0.0",
    "concurrently": "^8.2.0",
    "eslint": "^8.0.1",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^4.0.0",
    "jest": "29.3.1",
    "prettier": "^2.3.2",
    "source-map-support": "^0.5.20",
    "supertest": "^6.1.3",
    "ts-jest": "29.0.3",
    "ts-loader": "^9.2.3",
    "ts-node": "^10.0.0",
    "tsconfig-paths": "4.1.1",
    "typescript": "^4.7.4"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": ".",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "./coverage",
    "testEnvironment": "node",
    "roots": [
      "<rootDir>/apps/",
      "<rootDir>/libs/"
    ],
    "moduleNameMapper": {
      "^@app/lib_auth(|/.*)$": "<rootDir>/libs/lib_auth/src/$1",
      "^@app/lib_config(|/.*)$": "<rootDir>/libs/lib_config/src/$1"
    }
  }
}

Node.js version

19.6.1

In which operating systems have you tested?

Other

No response

kamilmysliwiec commented 1 year ago

Please provide a minimum reproduction repository (Git repository/StackBlitz/CodeSandbox project).

vulongc3 commented 1 year ago

Please provide a minimum reproduction repository (Git repository/StackBlitz/CodeSandbox project).

https://stackblitz.com/edit/nestjs-typescript-starter-fx6q9u Please check

kamilmysliwiec commented 1 year ago

I just updated my comment here https://github.com/nestjs/nest/issues/11966#issuecomment-1619735777 (true in the client configuration, false in the main.ts)

codingwill commented 1 year ago

I'm having the same exact issue...

Edit: my mistake for not reading the above comment, it's solved