fix: initialization retry to avoid DB startup race condition
Build & Deployment / Build & Deploy (push) Failing after 30s

This commit is contained in:
Raul Lugo
2026-08-10 18:08:32 +02:00
parent 52a883ef07
commit e99fc2e388
3 changed files with 23 additions and 10 deletions
+20 -1
View File
@@ -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));
}
}
}