NestJs: How to make TypeORMModule to retry DB connection for ever?

Viewed 1015

I have a nestJS API. I am using TypeORM to connect to database. My API is a docker container. When I run the container if it cannot connect to DB it keeps retrying for about 10 times and then exits. The following shows a part of logs:

> nest start

[Nest] 31   - 09/23/2020, 8:39:14 PM   [NestFactory] Starting Nest application...
[Nest] 31   - 09/23/2020, 8:39:14 PM   [InstanceLoader] TypeOrmModule dependencies initialized +39ms
[Nest] 31   - 09/23/2020, 8:39:14 PM   [InstanceLoader] AppModule dependencies initialized +1ms
[Nest] 31   - 09/23/2020, 8:39:14 PM   [TypeOrmModule] Unable to connect to the database. Retrying (1)... +3ms
Error: getaddrinfo ENOTFOUND database database:5432
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:56:26)
[Nest] 31   - 09/23/2020, 8:39:14 PM   [TypeOrmModule] Unable to connect to the database. Retrying (1)... +3ms
Error: getaddrinfo ENOTFOUND database database:5432
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:56:26)
[Nest] 31   - 09/23/2020, 8:39:17 PM   [TypeOrmModule] Unable to connect to the database. Retrying (2)... +3013ms
Error: getaddrinfo ENOTFOUND database database:5432
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:56:26)
[Nest] 31   - 09/23/2020, 8:39:17 PM   [TypeOrmModule] Unable to connect to the database. Retrying (2)... +3ms
Error: getaddrinfo ENOTFOUND database database:5432
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:56:26)
[Nest] 31   - 09/23/2020, 8:39:20 PM   [TypeOrmModule] Unable to connect to the database. Retrying (3)... +3010ms
Error: getaddrinfo ENOTFOUND database database:5432
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:56:26)
[Nest] 31   - 09/23/2020, 8:39:20 PM   [TypeOrmModule] Unable to connect to the database. Retrying (3)... +2ms
Error: getaddrinfo ENOTFOUND database database:5432

I would like the TypeORMModule to keep retrying for ever. I was wondering how I can do so?

I have a main.ts as follows:

import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  await app.listen(3000);
}

bootstrap();

and a app.module.ts as below:

import { Module } from "@nestjs/common";
import { TypeOrmModule } from "@nestjs/typeorm";
import { AppController } from "./app.controller";
import { AppService } from "./app.service";
import { PostgresConnectionOptions } from "typeorm/driver/postgres/PostgresConnectionOptions";



@Module({
  imports: [
    TypeOrmModule.forRootAsync({
      useFactory: async () => {
        let options: PostgresConnectionOptions = {
          type: "postgres",
          host: "database",
          port: 5432,
          username: "test",
          password: "postgres",
          database: "test",
          migrationsRun: true,
        };
        return options;
      },
    }),
  ],
  controllers: [AppControlle],
  providers: [AppService],
})

export class AppModule {}

0 Answers
Related