// dist/src/daemon/restart-helper.js
import { spawn } from "node:child_process";
var port = Number(process.env.HONEYCOMB_RESTART_PORT ?? "3850");
var entry = process.env.HONEYCOMB_RESTART_ENTRY ?? "";
var POLL_INTERVAL_MS = 200;
var MAX_WAIT_MS = 3e4;
var POLL_TIMEOUT_MS = 1e3;
var LOCK_RELEASE_GRACE_MS = 800;
var healthUrl = `http://127.0.0.1:${Number.isFinite(port) && port > 0 ? port : 3850}/health`;
async function stillUp() {
  const ac = new AbortController();
  const timer = setTimeout(() => ac.abort(), POLL_TIMEOUT_MS);
  try {
    await fetch(healthUrl, { method: "GET", signal: ac.signal });
    return true;
  } catch (error) {
    return error instanceof Error && error.name === "AbortError";
  } finally {
    clearTimeout(timer);
  }
}
function sleep(ms) {
  return new Promise((r) => {
    const t = setTimeout(r, ms);
    t.unref?.();
  });
}
async function main() {
  if (entry === "")
    return;
  const deadline = Date.now() + MAX_WAIT_MS;
  while (Date.now() < deadline) {
    if (!await stillUp())
      break;
    await sleep(POLL_INTERVAL_MS);
  }
  await sleep(LOCK_RELEASE_GRACE_MS);
  const child = spawn(process.execPath, [entry], { detached: true, stdio: "ignore" });
  child.unref();
}
void main();
