Skip to content

Commit 80402fe

Browse files
Postgres connection timeout
1 parent 509cd1c commit 80402fe

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

src/database/Database.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import fs from "node:fs";
2121
import { green, red, yellow } from "picocolors";
2222
import { DataSource } from "typeorm";
2323
import { ProcessLifecycle } from "../util/util/ProcessLifecycle";
24+
import { PostgresDataSourceOptions } from "typeorm/driver/postgres/PostgresDataSourceOptions";
2425

2526
// UUID extension option is only supported with postgres
2627
// We want to generate all id's with Snowflakes that's why we have our own BaseEntity class
@@ -67,7 +68,8 @@ export const DataSourceOptions = isHeadlessProcess
6768
null: "sql-null",
6869
undefined: "ignore",
6970
},
70-
});
71+
connectTimeoutMS: 10000,
72+
} satisfies PostgresDataSourceOptions);
7173

7274
// Gets the existing database connection
7375
export function getDatabase(): DataSource | null {
@@ -96,7 +98,16 @@ export async function initDatabase(): Promise<DataSource> {
9698

9799
console.log(`[Database] ${yellow(`Connecting to ${DatabaseType} db`)}`);
98100

99-
dbConnection = await DataSourceOptions.initialize();
101+
let retries = 0;
102+
do {
103+
try {
104+
dbConnection = await DataSourceOptions.initialize();
105+
} catch (e) {
106+
console.error("[Database] Could not connect to database after", retries, "retries:", e);
107+
}
108+
} while (!dbConnection && retries++ < 10);
109+
110+
if (!dbConnection) throw new Error("[Database] FATAL: Could not connect to database!");
100111

101112
// Crude way of detecting if the migrations table exists.
102113
const dbExists = async () => {

0 commit comments

Comments
 (0)