The service only runs under Linux systemd. Remove setupLaunchd, setupNohupFallback, buildLaunchdPlist, the launchd/nohup service checks in setup/verify-services.ts, ServiceDef.launchdLabel, launchd/ and the unused repo-tokens/ GitHub action. Without systemd, `setup --step service` now fails with systemd_unavailable instead of writing a nohup wrapper. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
90 lines
2.5 KiB
TypeScript
90 lines
2.5 KiB
TypeScript
import path from 'path';
|
|
|
|
import { STARTUP_PRECONDITION_EXIT_CODE } from '../src/startup-preconditions.js';
|
|
import type { ServiceDef } from './service-defs.js';
|
|
|
|
export function buildRuntimePathEnv(nodePath: string, homeDir: string): string {
|
|
return `${path.dirname(nodePath)}:/usr/local/bin:/usr/bin:/bin:${homeDir}/.local/bin:${homeDir}/.npm-global/bin`;
|
|
}
|
|
|
|
function buildSystemdEnvironmentLines(
|
|
nodePath: string,
|
|
homeDir: string,
|
|
extraEnv?: Record<string, string>,
|
|
): string[] {
|
|
const envLines = [
|
|
`Environment=HOME=${homeDir}`,
|
|
`Environment=PATH=${buildRuntimePathEnv(nodePath, homeDir)}`,
|
|
];
|
|
if (extraEnv) {
|
|
for (const [k, v] of Object.entries(extraEnv)) {
|
|
envLines.push(`Environment=${k}=${v}`);
|
|
}
|
|
}
|
|
return envLines;
|
|
}
|
|
|
|
export function buildSystemdUnit(
|
|
def: ServiceDef,
|
|
projectRoot: string,
|
|
nodePath: string,
|
|
homeDir: string,
|
|
runningAsRoot: boolean,
|
|
): string {
|
|
const envLines = buildSystemdEnvironmentLines(
|
|
nodePath,
|
|
homeDir,
|
|
def.extraEnv,
|
|
);
|
|
|
|
const envFileLine = def.environmentFile
|
|
? `EnvironmentFile=${def.environmentFile}\n`
|
|
: '';
|
|
|
|
return `[Unit]
|
|
Description=${def.description}
|
|
After=network.target
|
|
|
|
[Service]
|
|
${envFileLine}Type=simple
|
|
ExecStart=${nodePath} ${projectRoot}/dist/index.js
|
|
WorkingDirectory=${projectRoot}
|
|
Restart=always
|
|
RestartSec=5
|
|
RestartPreventExitStatus=${STARTUP_PRECONDITION_EXIT_CODE}
|
|
# Per-turn agent runners do heavy file I/O (git clone, builds, video/ffmpeg
|
|
# scratch) inside this service cgroup. The kernel keeps those reads as
|
|
# reclaimable page cache, which makes systemd's MemoryCurrent climb to several
|
|
# GB even though real (anon) usage stays a few hundred MB. MemoryHigh applies
|
|
# gentle reclaim pressure so that cache is dropped before the number balloons,
|
|
# while MemoryMax stays unset (infinity) so a legitimate burst is never
|
|
# OOM-killed — it is throttled at worst.
|
|
MemoryAccounting=yes
|
|
MemoryHigh=3G
|
|
${envLines.join('\n')}
|
|
StandardOutput=append:${projectRoot}/logs/${def.logName}.log
|
|
StandardError=append:${projectRoot}/logs/${def.logName}.error.log
|
|
|
|
[Install]
|
|
WantedBy=${runningAsRoot ? 'multi-user.target' : 'default.target'}`;
|
|
}
|
|
|
|
export function buildStackRestartSystemdUnit(
|
|
projectRoot: string,
|
|
nodePath: string,
|
|
homeDir: string,
|
|
): string {
|
|
const envLines = buildSystemdEnvironmentLines(nodePath, homeDir);
|
|
|
|
return `[Unit]
|
|
Description=EJClaw Stack Restart Orchestrator
|
|
After=network.target
|
|
|
|
[Service]
|
|
Type=oneshot
|
|
WorkingDirectory=${projectRoot}
|
|
${envLines.join('\n')}
|
|
ExecStart=${nodePath} ${projectRoot}/setup/restart-stack.ts --direct
|
|
`;
|
|
}
|