All files / kernel-cli/src/commands daemon-spawn.ts

0% Statements 0/27
0% Branches 0/14
0% Functions 0/3
0% Lines 0/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86                                                                                                                                                                           
import { spawn } from 'node:child_process';
import { readFile } from 'node:fs/promises';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
 
import { pingDaemon } from './daemon-client.ts';
import { getOcapHome } from '../ocap-home.ts';
import { isProcessAlive } from '../utils.ts';
 
const POLL_INTERVAL_MS = 100;
const MAX_POLLS = 300; // 30 seconds
 
/**
 * Read the PID from `<OCAP_HOME>/daemon.pid`. Returns `undefined` if the
 * file is missing or unparseable.
 *
 * @returns The parsed pid, or `undefined`.
 */
async function readPidFile(): Promise<number | undefined> {
  let raw: string;
  try {
    raw = await readFile(join(getOcapHome(), 'daemon.pid'), 'utf-8');
  } catch (error) {
    if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
      return undefined;
    }
    throw error;
  }
  const pid = Number(raw.trim());
  return Number.isFinite(pid) && pid > 0 ? pid : undefined;
}
 
/**
 * Ensure the daemon is running. If it is not, spawn it as a detached process
 * and wait until the socket becomes responsive.
 *
 * Refuses to spawn if a daemon process is already alive under this
 * OCAP_HOME — orphaning the existing process would leave it holding
 * `kernel.sqlite` locks with no easy way to find it again.
 *
 * @param socketPath - The UNIX socket path.
 */
export async function ensureDaemon(socketPath: string): Promise<void> {
  if (await pingDaemon(socketPath)) {
    return;
  }
 
  const orphanPid = await readPidFile();
  if (orphanPid !== undefined && isProcessAlive(orphanPid)) {
    throw new Error(
      `A daemon process (pid ${orphanPid}) is alive under ` +
        `${getOcapHome()} but its socket is unresponsive. ` +
        `Kill it (\`kill ${orphanPid}\`) and remove ` +
        `${getOcapHome()}/daemon.{sock,pid} before retrying.`,
    );
  }
 
  process.stderr.write('Starting daemon...\n');
 
  const currentDir = dirname(fileURLToPath(import.meta.url));
  const entryPath = join(currentDir, 'daemon-entry.mjs');
 
  const child = spawn(process.execPath, [entryPath], {
    detached: true,
    stdio: 'ignore',
    env: {
      ...process.env,
      OCAP_SOCKET_PATH: socketPath,
    },
  });
  child.unref();
 
  // Poll until daemon responds
  for (let i = 0; i < MAX_POLLS; i++) {
    await new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL_MS));
    if (await pingDaemon(socketPath)) {
      process.stderr.write('Daemon ready.\n');
      return;
    }
  }
 
  throw new Error(
    `Daemon did not start within ${(MAX_POLLS * POLL_INTERVAL_MS) / 1000}s`,
  );
}