#!/usr/bin/env node /** * Backup for verae-keep. If keep's /health fails, spawn keep again. */ import { spawn } from 'node:child_process'; import fs from 'node:fs'; import http from 'node:http'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { httpProbe } from './probe.js'; import { killPid, pidAlive } from './spawn.js'; import { expandHome } from './expand.js'; const HERE = path.dirname(fileURLToPath(import.meta.url)); const ROOT = path.join(HERE, '..'); const KEEP_URL = (process.env.KEEP_URL || 'http://127.0.0.1:3860').replace(/\/$/, ''); const STATE_DIR = expandHome(process.env.KEEP_STATE || path.join(ROOT, 'data')); const PID_FILE = path.join(STATE_DIR, 'keep.pid'); const INTERVAL = Number(process.env.WATCH_INTERVAL_MS || 2000); const BIND = process.env.WATCH_BIND || '127.0.0.1'; const PORT = Number(process.env.WATCH_PORT || 3861); let child = null; let restarts = 0; function log(event, extra = {}) { const line = JSON.stringify({ t: new Date().toISOString(), event, ...extra }); fs.mkdirSync(STATE_DIR, { recursive: true }); fs.appendFileSync(path.join(STATE_DIR, 'watch.log'), `${line}\n`); process.stdout.write(`${line}\n`); } function readPid() { try { return Number(fs.readFileSync(PID_FILE, 'utf8').trim()); } catch { return null; } } function writePid(pid) { fs.mkdirSync(STATE_DIR, { recursive: true }); fs.writeFileSync(PID_FILE, String(pid)); } function startKeep() { const existing = readPid(); if (existing && pidAlive(existing)) { log('adopt', { pid: existing }); return existing; } const env = { ...process.env }; const proc = spawn(process.execPath, [path.join(HERE, 'keep.js')], { cwd: ROOT, env, detached: true, stdio: ['ignore', 'pipe', 'pipe'], }); proc.unref(); child = proc; writePid(proc.pid); restarts += 1; log('start-keep', { pid: proc.pid, restarts }); proc.stdout?.on('data', (c) => process.stdout.write(c)); proc.stderr?.on('data', (c) => process.stderr.write(c)); proc.on('exit', (code, signal) => { log('keep-exit', { code, signal, pid: proc.pid }); if (readPid() === proc.pid) { try { fs.unlinkSync(PID_FILE); } catch { /* ignore */ } } }); return proc.pid; } async function tick() { const h = await httpProbe(`${KEEP_URL}/health`, { timeoutMs: 1500 }); if (h.ok) return { ok: true, keep: h.body }; log('keep-down', { error: h.error || h.statusCode }); const old = readPid(); if (old && pidAlive(old)) killPid(old, 'SIGTERM'); startKeep(); return { ok: false, restarted: true }; } const server = http.createServer(async (req, res) => { const url = new URL(req.url || '/', `http://127.0.0.1:${PORT}`); const json = (code, obj) => { res.writeHead(code, { 'content-type': 'application/json' }); res.end(JSON.stringify(obj)); }; if (url.pathname === '/health') { return json(200, { ok: true, role: 'verae-keep-watch', pid: process.pid, keepPid: readPid(), restarts }); } if (url.pathname === '/tick' && req.method === 'POST') { return json(200, await tick()); } json(404, { error: 'not found' }); }); server.listen(PORT, BIND, () => { log('listen', { url: `http://${BIND}:${PORT}/`, keep: KEEP_URL }); startKeep(); setInterval(() => { tick().catch((err) => log('tick-error', { error: err.message })); }, INTERVAL); }); process.on('SIGTERM', () => { if (child?.pid) killPid(child.pid, 'SIGTERM'); process.exit(0); });