forked from mtusi2026/mtusi2026lab7
86 lines
2.3 KiB
TypeScript
86 lines
2.3 KiB
TypeScript
import { resolve } from "node:path";
|
|
import { root, local, run, requireThat, safeFailure } from "../scripts/runtime";
|
|
import { containerIP } from "../scripts/runtime-inspect";
|
|
export async function waitReady(
|
|
replicaServices = ["api1", "api2"],
|
|
budget = 90_000,
|
|
) {
|
|
const deadline = Date.now() + budget;
|
|
for (const service of replicaServices) {
|
|
const hostname = service + "-gateway";
|
|
let ready = false;
|
|
while (Date.now() < deadline) {
|
|
const ip = await containerIP(hostname);
|
|
const result = await run(
|
|
[
|
|
"curl",
|
|
"--silent",
|
|
"--show-error",
|
|
"--noproxy",
|
|
"*",
|
|
"--max-time",
|
|
"3",
|
|
"--cacert",
|
|
resolve(local, "pki/lb/ca.crt"),
|
|
"--cert",
|
|
resolve(local, "pki/lb/cert.pem"),
|
|
"--key",
|
|
resolve(local, "pki/lb/key.pem"),
|
|
"--resolve",
|
|
`${hostname}:8443:${ip}`,
|
|
`https://${hostname}:8443/health/ready`,
|
|
"--write-out",
|
|
"\n%{http_code}",
|
|
],
|
|
{ allowFailure: true },
|
|
);
|
|
if (result.code === 0) {
|
|
const split = result.out.lastIndexOf("\n");
|
|
if (result.out.slice(split + 1) === "200") {
|
|
try {
|
|
ready = JSON.parse(result.out.slice(0, split)).status === "ok";
|
|
} catch {}
|
|
if (ready) break;
|
|
}
|
|
}
|
|
await Bun.sleep(500);
|
|
}
|
|
requireThat(ready, "READINESS_TIMEOUT_" + service.toUpperCase());
|
|
}
|
|
let publicReady = false;
|
|
while (Date.now() < deadline) {
|
|
const publicResult = await run(
|
|
[
|
|
"curl",
|
|
"--silent",
|
|
"--show-error",
|
|
"--noproxy",
|
|
"*",
|
|
"--fail",
|
|
"--max-time",
|
|
"3",
|
|
"--cacert",
|
|
resolve(local, "pki/lb/ca.crt"),
|
|
`https://localhost:${Bun.env.HTTPS_PORT ?? 18443}/health/ready`,
|
|
],
|
|
{ allowFailure: true },
|
|
);
|
|
if (publicResult.code === 0) {
|
|
try {
|
|
publicReady = JSON.parse(publicResult.out).status === "ok";
|
|
} catch {}
|
|
if (publicReady) break;
|
|
}
|
|
await Bun.sleep(500);
|
|
}
|
|
requireThat(publicReady, "PUBLIC_READINESS_FAILED");
|
|
console.log("PASS_ALL_REQUIRED_REPLICAS_AND_VERIFIED_HTTPS_READY");
|
|
}
|
|
if (import.meta.main) {
|
|
try {
|
|
await waitReady();
|
|
} catch (e) {
|
|
safeFailure(e);
|
|
}
|
|
}
|