fix: initialization retry to avoid DB startup race condition
Build & Deployment / Build & Deploy (push) Failing after 30s
Build & Deployment / Build & Deploy (push) Failing after 30s
This commit is contained in:
+20
-1
@@ -47,7 +47,7 @@ async function main() {
|
||||
}
|
||||
|
||||
const client = new Client({ connectionString: url });
|
||||
await client.connect();
|
||||
await connectWithRetry(client);
|
||||
|
||||
try {
|
||||
const files = listSqlFiles(migrationsDir);
|
||||
@@ -68,3 +68,22 @@ main().catch((error) => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
async function connectWithRetry(client) {
|
||||
const maxAttempts = Number(process.env.DB_MIGRATION_CONNECT_ATTEMPTS ?? 30);
|
||||
const delayMs = Number(process.env.DB_MIGRATION_CONNECT_DELAY_MS ?? 2000);
|
||||
|
||||
for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
|
||||
try {
|
||||
await client.connect();
|
||||
return;
|
||||
} catch (error) {
|
||||
if (attempt === maxAttempts) throw error;
|
||||
|
||||
console.log(
|
||||
`Database is not ready yet. Retrying migration connection (${attempt}/${maxAttempts})...`,
|
||||
);
|
||||
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user