nestjs / terminus

Terminus module for Nest framework (node.js) :robot:

Home Page:https://nestjs.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Mikro ORM check returns healthy although not connected

noe-charmet opened this issue · comments

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

When Mikro ORM's connection object return false on the isConnected method of its [Connection](https://mikro-orm.io/api/core/class/Connection) object, the health check is still considered valid.

Minimum reproduction code

// app.module.ts
import {Modulle} from '@nestjs/common';
import {ConfigModule, ConfigService} from '@nestjs/config';
import {ConfigSchema} from './config';
import {MikroOrmModule} from '@mikro-orm/nestjs';
import {defineConfig} from '@mikro-orm/postgresql';
import {TerminusModule} from '@nestjs/terminus';
import {AppController} from './app.controller';

@Module({
  imports: [
    ConfigModule.forRoot({
      validationSchema: ConfigSchema,
      isGlobal: true,
    }),
    MikroOrmModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: (config: ConfigService) => ({
        ...defineConfig({
          user: config.getOrThrow('POSTGRES_USER'),
          password: config.getOrThrow('POSTGRES_PASSWORD'),
          dbName: config.getOrThrow('POSTGRES_DATABASE'),
          host: config.getOrThrow('POSTGRES_HOST'),
          port: config.getOrThrow('POSTGRES_PORT'),
        }),
        autoLoadEntities: true,
        connect: true,
        migrations: {
          path: './data/migrations',
          pattern: /^[\w-]+\d+\.[tj]s$/,
        },
      }),
      inject: [ConfigService],
    }),
    TerminusModule
  ],
  controllers: [AppController],
})
export class AppModule {}
// app.controller.ts
import {Controller, Get} from '@nestjs/common';
import {
  HealthCheckService,
  MikroOrmHealthIndicator,
  HealthCheck,
  type HealthCheckResult,
} from '@nestjs/terminus';

@Controller('/')
export class AppController {
  constructor(
    private readonly HealthCheckService: HealthCheckService,
    private readonly MikroOrmHealthIndicator: MikroOrmHealthIndicator,
  ) {}

  @Get(['live', 'livez'])
  @HealthCheck()
  live(): Promise<HealthCheckResult> {
    return this.HealthCheckService.check([]);
  }

  @Get(['ready', 'readyz'])
  @HealthCheck()
  ready(): Promise<HealthCheckResult> {
    return this.HealthCheckService.check([
      () => this.MikroOrmHealthIndicator.pingCheck('database'),
    ]);
  }
}

Steps to reproduce

  1. run nest app
  2. Do not have any DB running
  3. Call GET /live endpoint

Expected behavior

I would expect the health check to be failing if there is actually no DB connection established

Package version

10.2.0

NestJS version

10.2.10

Node.js version

18.12.1

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

Can you create minimal reproduction in a cloneable git repository?

I can confirm - we discovered the same issue today.
I kill DB container, but health checks returns "up" forever despite of that.

Hi, we face the same bug in our services. What is interesting is that the first pingCheck after the server starts will return an error (which is right), but all ping checks after it will return up

We face the same problem too.

I created a new beta release, would you mind checking? NPMJS link, install via:

npm i @nestjs/terminus@10.2.2-beta.0

Hi @BrunnerLivio , I tested the @nestjs/terminus@10.2.2-beta.0 in one of our services, and it works as expected.
I tested these cases:

  1. The database connection cannot be established. (I intentionally set a wrong port for DB to test this.)
  2. Time out error ( I set timeout to 1ms to test this)

This has now been officially released with 10.2.2 🎉