-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmigrate-db-retry.mjs
More file actions
50 lines (41 loc) · 1.2 KB
/
migrate-db-retry.mjs
File metadata and controls
50 lines (41 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { spawn } from "node:child_process";
const MAX_ATTEMPTS = 3;
const RETRY_DELAY_MS = 3000;
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
function runMigrate() {
return new Promise((resolve) => {
const child = spawn("npm", ["run", "db:migrate"], {
stdio: "inherit",
shell: true,
env: process.env,
});
child.on("close", (code) => {
resolve(code ?? 1);
});
});
}
async function main() {
for (let attempt = 1; attempt <= MAX_ATTEMPTS; attempt += 1) {
// Keep logs explicit so startup failures are easy to debug in terminals/CI.
console.log(`[db:migrate:retry] attempt ${attempt}/${MAX_ATTEMPTS}`);
const code = await runMigrate();
if (code === 0) {
console.log("[db:migrate:retry] migrations succeeded");
process.exit(0);
}
if (attempt < MAX_ATTEMPTS) {
console.warn(
`[db:migrate:retry] attempt ${attempt} failed (exit ${code}), retrying in ${RETRY_DELAY_MS}ms...`,
);
await sleep(RETRY_DELAY_MS);
continue;
}
console.error(
`[db:migrate:retry] failed after ${MAX_ATTEMPTS} attempts, aborting startup`,
);
process.exit(code);
}
}
void main();