NestJs:如何使TypeORMModule永远重试数据库连接



我有一个nestJS API。我正在使用TypeORM连接到数据库。我的API是一个docker容器。当我运行容器时,如果它不能连接到DB,它会不断重试大约10次,然后退出。以下显示了部分日志:

> 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

我希望TypeORMModule永远保持重试。我想知道我该怎么做?

我的主要任务如下:

import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
}
bootstrap();

和如下的app.module.ts:

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 {}

您可以传递一个大数字作为retryAttempts,默认情况下为10

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,
retryAttempts: 9999,
};
return options;
},
}),
],
controllers: [AppControlle],
providers: [AppService],
})
export class AppModule {}

还有默认为3000ms/3sretryDelay,您可以使用该选项来控制重试时间。

最新更新