Files
callstack__agent-device/scripts/clean-daemon.ts
Michał Pierzchała c2b29d5600 fix: stabilize Maestro replay on iOS (#713)
* fix: stabilize Maestro replay on iOS

* fix: scope iOS runner cleanup to daemon owner

* fix: lease iOS runner ownership per device

* fix: release prepared iOS runner daemon in CI

* fix: inline runner lease release cleanup
2026-06-09 14:12:57 +02:00

44 lines
1.1 KiB
TypeScript

import fs from 'node:fs';
import { resolveDaemonPaths } from '../src/daemon/config.ts';
import { stopProcessForTakeover } from '../src/utils/process-identity.ts';
const DAEMON_TERM_TIMEOUT_MS = 15_000;
const DAEMON_KILL_TIMEOUT_MS = 2_000;
type DaemonInfo = {
pid?: number;
processStartTime?: string;
};
const paths = resolveDaemonPaths(process.env.AGENT_DEVICE_STATE_DIR);
const info = readDaemonInfo(paths.infoPath);
if (info?.pid && Number.isInteger(info.pid) && info.pid > 0) {
await stopProcessForTakeover(info.pid, {
termTimeoutMs: DAEMON_TERM_TIMEOUT_MS,
killTimeoutMs: DAEMON_KILL_TIMEOUT_MS,
expectedStartTime: info.processStartTime,
});
}
removeIfPresent(paths.infoPath);
removeIfPresent(paths.lockPath);
function readDaemonInfo(infoPath: string): DaemonInfo | null {
try {
return JSON.parse(fs.readFileSync(infoPath, 'utf8')) as DaemonInfo;
} catch {
return null;
}
}
function removeIfPresent(filePath: string): void {
try {
fs.unlinkSync(filePath);
} catch (error) {
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') {
throw error;
}
}
}