Ai *_* Da 5 typescript typeorm nestjs
我有一个 NestJS API。我正在使用 TypeORM 连接到数据库。我的 API 是一个 Docker 容器。当我运行容器时,如果它无法连接到数据库,它会不断重试大约 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
Run Code Online (Sandbox Code Playgroud)
我希望 TypeORMModule 能够永远重试。我想知道我该怎么做?
我有一个 main.ts 如下:
import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
}
bootstrap();
Run Code Online (Sandbox Code Playgroud)
和 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 {}
Run Code Online (Sandbox Code Playgroud)
您可以传递一个很大的数字, 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 {}
Run Code Online (Sandbox Code Playgroud)
还有retryDelay一个默认为3000ms/3s,您可以使用该选项来控制重试时间。
| 归档时间: |
|
| 查看次数: |
2419 次 |
| 最近记录: |