/** Daemon heartbeat and routine-process reconciliation under supervision. */
import { reapExitedRunningJobs } from './runner.js';
import { reapTerminalRoutineProcesses } from '../routine-process-cleanup.js';
import { BasePeriodicService } from './service.js';
const HEARTBEAT_TICK_MS = 60_000;
const HEARTBEAT_DEADLINE_MS = 30_000;
export class HeartbeatService extends BasePeriodicService {
    publishHeartbeat;
    id = 'daemon-heartbeat';
    intervalMs = HEARTBEAT_TICK_MS;
    deadlineMs = HEARTBEAT_DEADLINE_MS;
    constructor(publishHeartbeat) {
        super();
        this.publishHeartbeat = publishHeartbeat;
    }
    async onStart(_ctx) { }
    async onStop() { }
    async onTick(ctx) {
        this.publishHeartbeat();
        await reapExitedRunningJobs();
        const reaped = await reapTerminalRoutineProcesses();
        if (reaped.length > 0) {
            ctx.log('WARN', `Reaped ${reaped.length} terminal routine process group(s): ${reaped.join(', ')}`);
        }
    }
}
