Refactor local memory and role-fixed runtime routing

This commit is contained in:
Eyejoker
2026-04-04 03:50:28 +09:00
parent ca58e8c8eb
commit 3dd772c229
47 changed files with 6158 additions and 669 deletions

View File

@@ -16,6 +16,13 @@ export interface ServiceCheck {
status: ServiceStatus;
}
export interface ServiceCheckOptions {
detectArtifacts?: boolean;
homeDir?: string;
}
export type SystemdScope = 'system' | 'user';
export function checkLaunchdService(label: string): ServiceStatus {
try {
const output = execSync('launchctl list', { encoding: 'utf-8' });
@@ -32,8 +39,26 @@ export function checkLaunchdService(label: string): ServiceStatus {
return 'not_found';
}
export function checkSystemdService(name: string): ServiceStatus {
const prefix = isRoot() ? 'systemctl' : 'systemctl --user';
function getLaunchdPlistPath(homeDir: string, launchdLabel: string): string {
return path.join(homeDir, 'Library', 'LaunchAgents', `${launchdLabel}.plist`);
}
export function checkLaunchdServiceArtifact(
label: string,
plistPath: string,
): ServiceStatus {
const status = checkLaunchdService(label);
if (status !== 'not_found') {
return status;
}
return fs.existsSync(plistPath) ? 'stopped' : 'not_found';
}
export function checkSystemdServiceInScope(
name: string,
scope: SystemdScope,
): ServiceStatus {
const prefix = scope === 'system' ? 'systemctl' : 'systemctl --user';
try {
execSync(`${prefix} is-active ${name}`, { stdio: 'ignore' });
return 'running';
@@ -52,6 +77,10 @@ export function checkSystemdService(name: string): ServiceStatus {
return 'not_found';
}
export function checkSystemdService(name: string): ServiceStatus {
return checkSystemdServiceInScope(name, isRoot() ? 'system' : 'user');
}
export function checkNohupService(
projectRoot: string,
serviceName: string,
@@ -72,18 +101,40 @@ export function checkNohupService(
return 'not_found';
}
export function checkNohupServiceArtifact(
projectRoot: string,
serviceName: string,
): ServiceStatus {
const status = checkNohupService(projectRoot, serviceName);
if (status !== 'not_found') {
return status;
}
const wrapperPath = path.join(projectRoot, `start-${serviceName}.sh`);
return fs.existsSync(wrapperPath) ? 'stopped' : 'not_found';
}
export function checkService(
projectRoot: string,
serviceManager: 'launchd' | 'systemd' | 'none',
serviceName: string,
launchdLabel: string,
options: ServiceCheckOptions = {},
): ServiceStatus {
if (serviceManager === 'launchd') {
if (options.detectArtifacts && options.homeDir) {
return checkLaunchdServiceArtifact(
launchdLabel,
getLaunchdPlistPath(options.homeDir, launchdLabel),
);
}
return checkLaunchdService(launchdLabel);
}
if (serviceManager === 'systemd') {
return checkSystemdService(serviceName);
}
if (options.detectArtifacts) {
return checkNohupServiceArtifact(projectRoot, serviceName);
}
return checkNohupService(projectRoot, serviceName);
}
@@ -91,6 +142,7 @@ export function getServiceChecks(
serviceDefs: ServiceDef[],
projectRoot: string,
serviceManager: 'launchd' | 'systemd' | 'none',
options: ServiceCheckOptions = {},
): ServiceCheck[] {
return serviceDefs.map((def) => ({
name: def.name,
@@ -99,6 +151,7 @@ export function getServiceChecks(
serviceManager,
def.name,
def.launchdLabel,
options,
),
}));
}