Skip to content

Commit a3ba60d

Browse files
committed
fix: wait for 5 consecutive health checks before considering server ready
The server was restarting during restore (because bun --watch detects file changes), causing the premature health check success. Now waitForServer requires 5 consecutive successful health checks before returning. Also wait for server to restart after prep-dev completes.
1 parent 5b6a089 commit a3ba60d

2 files changed

Lines changed: 30 additions & 6 deletions

File tree

scripts/dev.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ function readOptionValue(args: string[], index: number, option: string) {
1717
return value;
1818
}
1919

20+
let fullMode = false;
21+
2022
for (let i = 2; i < process.argv.length; i++) {
2123
const arg = process.argv[i];
2224

@@ -143,7 +145,6 @@ console.log(` ADMIN_KEY: ${process.env.ADMIN_KEY}`);
143145
const WIN = process.platform === 'win32';
144146

145147
const childPgids: number[] = [];
146-
let fullMode = false;
147148
let isShuttingDown = false;
148149

149150
function spawnManaged(args: string[], cwd: string): ChildProcess {
@@ -198,12 +199,19 @@ console.log('Watching for changes...');
198199
async function waitForServer(timeout = 30000): Promise<void> {
199200
const url = `http://localhost:${process.env.PORT}`;
200201
const start = Date.now();
202+
let consecutiveOk = 0;
203+
const requiredOk = 5;
201204
while (Date.now() - start < timeout) {
202205
try {
203-
const res = await fetch(`${url}/v0/health`);
204-
if (res.ok) return;
206+
const res = await fetch(`${url}/health`);
207+
if (res.ok) {
208+
consecutiveOk++;
209+
if (consecutiveOk >= requiredOk) return;
210+
} else {
211+
consecutiveOk = 0;
212+
}
205213
} catch {
206-
// not ready yet
214+
consecutiveOk = 0;
207215
}
208216
await new Promise((r) => setTimeout(r, 500));
209217
}
@@ -231,6 +239,15 @@ if (fullMode) {
231239
);
232240
proc.on('error', reject);
233241
}).catch((err) => console.error(`[full] ${err instanceof Error ? err.message : err}`));
242+
243+
// prep-dev triggers a server restart after restore, so wait for it to come back up
244+
console.log('[full] Waiting for server to restart after restore...');
245+
try {
246+
await waitForServer();
247+
console.log('[full] Server restarted and ready.\n');
248+
} catch (err) {
249+
console.error(`[full] ${err instanceof Error ? err.message : err}.`);
250+
}
234251
})();
235252
}
236253

scripts/prep-dev.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,19 @@ function stripOAuthProviders(config: any): { config: any; removed: string[] } {
237237
async function waitForServer(timeout = 30000): Promise<void> {
238238
const url = LOCAL_URL.replace(/\/$/, '');
239239
const start = Date.now();
240+
let consecutiveOk = 0;
241+
const requiredOk = 5;
240242
while (Date.now() - start < timeout) {
241243
try {
242244
const res = await fetch(`${url}/health`);
243-
if (res.ok) return;
245+
if (res.ok) {
246+
consecutiveOk++;
247+
if (consecutiveOk >= requiredOk) return;
248+
} else {
249+
consecutiveOk = 0;
250+
}
244251
} catch {
245-
// not ready yet
252+
consecutiveOk = 0;
246253
}
247254
await new Promise((r) => setTimeout(r, 500));
248255
}

0 commit comments

Comments
 (0)