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.89k stars 7.56k forks source link

Client connection refused after rabbitmq server restart #10687

Closed corentingosselin closed 1 year ago

corentingosselin commented 1 year ago

Is there an existing issue for this?

Current behavior

I have a docker container which is running rabbitmq service, I have an api-gateway with clientproxy of rabbitmq And an authentification-service with message pattern to intercept the message.

Everything works fine until I stop the rabbitmq service and restart it.

I am using a dynamic module to register the client

import { Module, DynamicModule} from '@nestjs/common';
import { ClientsModule, Transport } from '@nestjs/microservices';
import { ConfigService } from '@nestjs/config';
import { RmqService } from './rmq.service';

interface RmqModuleOptions {
  name: string;
}
@Module({
  controllers: [],
  providers: [RmqService],
  exports: [RmqService],
})
export class SharedRabbitmqModule {

  static registerClient({ name }: RmqModuleOptions): DynamicModule {
    return {
      module: SharedRabbitmqModule,
      imports: [
        ClientsModule.registerAsync([
          {
            name,
            useFactory: (configService: ConfigService) => ({
              transport: Transport.RMQ,
              options: {
                urls: [configService.get<string>('RABBIT_MQ_URI')],
                queue: configService.get<string>(`RABBIT_MQ_${name}_QUEUE`),
                durable: true,
                reconnect: true,
                reconnectDelay: 5000,
              },
            }),
            inject: [ConfigService],
          },
        ]),
      ],
      exports: [ClientsModule],
    };
  }

}

Minimum reproduction code

https://complex-issue-not-reproductible-in-sandbox

Steps to reproduce

Steps to reproduce:

I need to restart the api-gateway (producer) to be able to send message again. Important note, if you don't send a message while rabbitmq is stopped and then start it, you don't get any error and it works

Expected behavior

After the rabbitmq server is restarted we should be able to send message without connection refused error. I also tried with rabbitmq installed locally

Package

Other package

No response

NestJS version

9.2.0

Packages versions

{
  "name": "lofi",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "nx",
    "postinstall": "node ./decorate-angular-cli.js && ngcc --properties es2020 browser module main",
    "start": "nx serve",
    "build": "nx build",
    "test": "nx test",
    "affected:build": "nx affected:build --configuration=production",
    "affected:ci": "nx affected:build && nx affected:lint && nx affected:test && nx affected:e2e --configuration=ci-production",
    "affected:e2e": "nx affected:e2e",
    "affected:lint": "nx affected:lint --max-warnings=0",
    "affected:test": "nx affected:test --parallel=2 -- --runInBand "
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~15.0.1",
    "@angular/cdk": "15.0.0",
    "@angular/common": "~15.0.1",
    "@angular/compiler": "~15.0.1",
    "@angular/core": "15.0.1",
    "@angular/forms": "~15.0.1",
    "@angular/platform-browser": "~15.0.1",
    "@angular/platform-browser-dynamic": "~15.0.1",
    "@angular/router": "~15.0.1",
    "@nestjs/axios": "^1.0.0",
    "@nestjs/common": "9.2.0",
    "@nestjs/config": "^2.2.0",
    "@nestjs/core": "9.2.0",
    "@nestjs/microservices": "^9.2.0",
    "@nestjs/passport": "^9.0.0",
    "@nestjs/platform-express": "9.2.0",
    "@nestjs/typeorm": "^9.0.1",
    "@ngneat/transloco": "^4.1.1",
    "@nrwl/angular": "15.2.1",
    "@taiga-ui/addon-charts": "^3.10.0",
    "@taiga-ui/addon-commerce": "^3.10.0",
    "@taiga-ui/addon-doc": "^3.10.0",
    "@taiga-ui/addon-editor": "^3.10.0",
    "@taiga-ui/addon-mobile": "^3.10.0",
    "@taiga-ui/addon-preview": "^3.10.0",
    "@taiga-ui/addon-table": "^3.10.0",
    "@taiga-ui/addon-tablebars": "^3.10.0",
    "@taiga-ui/cdk": "3.10.0",
    "@taiga-ui/core": "^3.10.0",
    "@taiga-ui/icons": "^3.10.0",
    "@taiga-ui/kit": "^3.10.0",
    "@taiga-ui/styles": "^3.10.0",
    "@tinkoff/ng-dompurify": "3.0.0",
    "amqp-connection-manager": "^4.1.9",
    "amqplib": "^0.10.3",
    "argon2": "^0.30.2",
    "axios": "^1.1.3",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.13.2",
    "connect-redis": "^6.1.3",
    "dompurify": "2.2.9",
    "express-session": "^1.17.3",
    "mysql2": "^2.3.3",
    "reflect-metadata": "^0.1.13",
    "rxjs": "~7.5.0",
    "tslib": "^2.3.0",
    "typeorm": "^0.3.10",
    "uuid": "^9.0.0",
    "zone.js": "~0.11.4"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "15.0.1",
    "@angular-devkit/core": "^15.0.1",
    "@angular-devkit/schematics": "^15.0.1",
    "@angular-eslint/eslint-plugin": "15.1.0",
    "@angular-eslint/eslint-plugin-template": "15.1.0",
    "@angular-eslint/template-parser": "15.1.0",
    "@angular/cli": "~15.0.1",
    "@angular/compiler-cli": "~15.0.1",
    "@angular/language-service": "~15.0.1",
    "@nestjs/schematics": "9.0.3",
    "@nestjs/testing": "9.2.0",
    "@nrwl/cli": "15.0.11",
    "@nrwl/cypress": "15.2.1",
    "@nrwl/eslint-plugin-nx": "15.2.1",
    "@nrwl/jest": "15.2.1",
    "@nrwl/linter": "15.2.1",
    "@nrwl/nest": "15.2.1",
    "@nrwl/node": "15.2.1",
    "@nrwl/workspace": "15.2.1",
    "@types/dompurify": "2.2.3",
    "@types/express-session": "^1.17.5",
    "@types/jest": "29.2.3",
    "@types/node": "18.7.1",
    "@types/passport-local": "^1.0.34",
    "@typescript-eslint/eslint-plugin": "5.42.1",
    "@typescript-eslint/parser": "5.42.1",
    "autoprefixer": "^10.4.0",
    "cypress": "^11.0.0",
    "eslint": "8.15.0",
    "eslint-config-prettier": "8.1.0",
    "eslint-plugin-cypress": "^2.10.3",
    "jest": "29.3.1",
    "jest-environment-jsdom": "29.3.1",
    "jest-preset-angular": "12.2.2",
    "nx": "15.2.1",
    "postcss": "^8.4.5",
    "prettier": "2.7.1",
    "ts-jest": "29.0.3",
    "ts-node": "10.9.1",
    "typescript": "4.8.4"
  }
}

Node.js version

No response

In which operating systems have you tested?

Other

No response

corentingosselin commented 1 year ago

This issue is the same related here: https://github.com/nestjs/nest/issues/10562

micalevisk commented 1 year ago

duplicate.

MikeKoval commented 1 year ago

@micalevisk pls share link to duplicate

corentingosselin commented 1 year ago

@micalevisk pls share link to duplicate

here https://github.com/nestjs/nest/issues/10687#issuecomment-1346421869