44 lines
1.1 KiB
JavaScript
44 lines
1.1 KiB
JavaScript
#!/usr/bin/env node
|
|||
|
|
/**
|
||
|
|
* Wait until docker-compose Postgres is accepting connections.
|
||
|
|
* Usage: node scripts/wait-postgres.mjs
|
||
|
|
*/
|
||
|
|
import { spawnSync } from "node:child_process";
|
||
|
|
import { setTimeout as delay } from "node:timers/promises";
|
||
|
|
import { repoRoot } from "./root-env.mjs";
|
||
|
|
|
||
|
|
const maxAttempts = Number(process.env.DESCRYBE_DB_WAIT_ATTEMPTS || 60);
|
||
|
|
const sleepMs = Number(process.env.DESCRYBE_DB_WAIT_MS || 1000);
|
||
|
|
|
||
|
|
for (let i = 1; i <= maxAttempts; i++) {
|
||
|
|
const r = spawnSync(
|
||
|
|
"docker",
|
||
|
|
[
|
||
|
|
"compose",
|
||
|
|
"exec",
|
||
|
|
"-T",
|
||
|
|
"postgres",
|
||
|
|
"pg_isready",
|
||
|
|
"-U",
|
||
|
|
"descrybe",
|
||
|
|
"-d",
|
||
|
|
"descrybe",
|
||
|
|
],
|
||
|
|
{
|
||
|
|
cwd: repoRoot,
|
||
|
|
encoding: "utf8",
|
||
|
|
shell: process.platform === "win32",
|
||
|
|
},
|
||
|
|
);
|
||
|
|
if (r.status === 0) {
|
||
|
|
console.log(`[db] postgres ready (attempt ${i})`);
|
||
|
|
process.exit(0);
|
||
|
|
}
|
||
|
|
process.stdout.write(`[db] waiting for postgres… (${i}/${maxAttempts})\r`);
|
||
|
|
await delay(sleepMs);
|
||
|
|
}
|
||
|
|
|
||
|
|
console.error("\n[db] postgres did not become ready in time");
|
||
|
|
console.error("Check: docker compose ps && docker compose logs postgres");
|
||
|
|
process.exit(1);
|