Merge pull request #19988 from backstage/rugvip/retry

backend-common: added DB creation retries and lowered pool limits
This commit is contained in:
Patrik Oldsberg
2023-09-19 12:46:30 +02:00
committed by GitHub
3 changed files with 47 additions and 2 deletions
@@ -151,13 +151,33 @@ export async function ensureMysqlDatabaseExists(
connection: {
database: null as unknown as string,
},
pool: {
min: 0,
acquireTimeoutMillis: 10000,
},
});
try {
const ensureDatabase = async (database: string) => {
await admin.raw(`CREATE DATABASE IF NOT EXISTS ??`, [database]);
};
await Promise.all(databases.map(ensureDatabase));
await Promise.all(
databases.map(async database => {
// For initial setup we use a smaller timeout but several retries. Given that this
// is a separate connection pool we should never really run into issues with connection
// acquisition timeouts, but we do anyway. This might be a bug in knex or some other dependency.
let lastErr: Error | undefined = undefined;
for (let i = 0; i < 3; i++) {
try {
return await ensureDatabase(database);
} catch (err) {
lastErr = err;
}
await new Promise(resolve => setTimeout(resolve, 100));
}
throw lastErr;
}),
);
} finally {
await admin.destroy();
}
@@ -126,6 +126,10 @@ export async function ensurePgDatabaseExists(
connection: {
database: 'postgres',
},
pool: {
min: 0,
acquireTimeoutMillis: 10000,
},
});
try {
@@ -142,7 +146,23 @@ export async function ensurePgDatabaseExists(
await admin.raw(`CREATE DATABASE ??`, [database]);
};
await Promise.all(databases.map(ensureDatabase));
await Promise.all(
databases.map(async database => {
// For initial setup we use a smaller timeout but several retries. Given that this
// is a separate connection pool we should never really run into issues with connection
// acquisition timeouts, but we do anyway. This might be a bug in knex or some other dependency.
let lastErr: Error | undefined = undefined;
for (let i = 0; i < 3; i++) {
try {
return await ensureDatabase(database);
} catch (err) {
lastErr = err;
}
await new Promise(resolve => setTimeout(resolve, 100));
}
throw lastErr;
}),
);
} finally {
await admin.destroy();
}