Browse Source

fix: avoid tmux pane skips on slow server startup. Ignore local .ignore artifacts to keep the tree clean. (#43)

Ivan Marshall Widjaja 2 months ago
parent
commit
4144443434
2 changed files with 30 additions and 20 deletions
  1. 3 1
      .gitignore
  2. 27 19
      src/utils/tmux.ts

+ 3 - 1
.gitignore

@@ -39,4 +39,6 @@ temp/
 local
 
 .sisyphus/
-.hive/
+.hive/
+
+.ignore

+ 27 - 19
src/utils/tmux.ts

@@ -18,32 +18,40 @@ let serverCheckUrl: string | null = null;
  */
 async function isServerRunning(serverUrl: string): Promise<boolean> {
   // Use cached result if checking the same URL
-  if (serverCheckUrl === serverUrl && serverAvailable !== null) {
-    return serverAvailable;
+  if (serverCheckUrl === serverUrl && serverAvailable === true) {
+    return true;
   }
 
-  try {
-    const controller = new AbortController();
-    const timeout = setTimeout(() => controller.abort(), 1000);
+  const healthUrl = new URL("/health", serverUrl).toString();
+  const timeoutMs = 3000;
+  const maxAttempts = 2;
 
-    // Try to hit the health endpoint or just the root
-    const response = await fetch(`${serverUrl}/health`, {
-      signal: controller.signal,
-    }).catch(() => null);
+  for (let attempt = 1; attempt <= maxAttempts; attempt++) {
+    const controller = new AbortController();
+    const timeout = setTimeout(() => controller.abort(), timeoutMs);
 
-    clearTimeout(timeout);
+    let response: Response | null = null;
+    try {
+      response = await fetch(healthUrl, { signal: controller.signal }).catch(() => null);
+    } finally {
+      clearTimeout(timeout);
+    }
 
-    serverCheckUrl = serverUrl;
-    serverAvailable = response?.ok ?? false;
+    const available = response?.ok ?? false;
+    if (available) {
+      serverCheckUrl = serverUrl;
+      serverAvailable = true;
+      log("[tmux] isServerRunning: checked", { serverUrl, available, attempt });
+      return true;
+    }
 
-    log("[tmux] isServerRunning: checked", { serverUrl, available: serverAvailable });
-    return serverAvailable;
-  } catch {
-    serverCheckUrl = serverUrl;
-    serverAvailable = false;
-    log("[tmux] isServerRunning: server not reachable", { serverUrl });
-    return false;
+    if (attempt < maxAttempts) {
+      await new Promise((r) => setTimeout(r, 250));
+    }
   }
+
+  log("[tmux] isServerRunning: checked", { serverUrl, available: false });
+  return false;
 }
 
 /**