feat: add stack restart orchestration

This commit is contained in:
Eyejoker
2026-03-29 07:08:01 +09:00
parent 0e12a560a4
commit ca578d1627
10 changed files with 281 additions and 88 deletions

View File

@@ -0,0 +1,69 @@
import fs from 'fs';
import os from 'os';
import path from 'path';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { restartStackServices } from './restart-stack.js';
describe('restartStackServices', () => {
const tempRoots: string[] = [];
afterEach(() => {
for (const root of tempRoots.splice(0)) {
fs.rmSync(root, { recursive: true, force: true });
}
});
it('restarts and verifies the configured three-service stack', () => {
const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'ejclaw-restart-'));
tempRoots.push(tempRoot);
fs.writeFileSync(path.join(tempRoot, '.env.codex'), 'A=1\n');
fs.writeFileSync(path.join(tempRoot, '.env.codex-review'), 'B=1\n');
const execFileSyncImpl = vi.fn();
const services = restartStackServices(tempRoot, {
execFileSyncImpl,
runningAsRoot: false,
serviceManager: 'systemd',
});
expect(services).toEqual(['ejclaw', 'ejclaw-codex', 'ejclaw-review']);
expect(execFileSyncImpl).toHaveBeenNthCalledWith(
1,
'systemctl',
['--user', 'restart', 'ejclaw', 'ejclaw-codex', 'ejclaw-review'],
{ stdio: 'ignore' },
);
expect(execFileSyncImpl).toHaveBeenNthCalledWith(
2,
'systemctl',
['--user', 'is-active', '--quiet', 'ejclaw'],
{ stdio: 'ignore' },
);
expect(execFileSyncImpl).toHaveBeenNthCalledWith(
3,
'systemctl',
['--user', 'is-active', '--quiet', 'ejclaw-codex'],
{ stdio: 'ignore' },
);
expect(execFileSyncImpl).toHaveBeenNthCalledWith(
4,
'systemctl',
['--user', 'is-active', '--quiet', 'ejclaw-review'],
{ stdio: 'ignore' },
);
});
it('rejects non-systemd environments', () => {
const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'ejclaw-restart-'));
tempRoots.push(tempRoot);
expect(() =>
restartStackServices(tempRoot, {
serviceManager: 'nohup',
}),
).toThrow('restart:stack only supports Linux systemd services in this repo');
});
});

63
setup/restart-stack.ts Normal file
View File

@@ -0,0 +1,63 @@
import { execFileSync } from 'child_process';
import { pathToFileURL } from 'url';
import { getServiceManager, isRoot } from './platform.js';
import { getConfiguredServiceNames } from './service-defs.js';
type ServiceManager = ReturnType<typeof getServiceManager>;
interface RestartStackDeps {
execFileSyncImpl?: typeof execFileSync;
runningAsRoot?: boolean;
serviceManager?: ServiceManager;
}
export function restartStackServices(
projectRoot: string,
deps: RestartStackDeps = {},
): string[] {
const serviceManager = deps.serviceManager ?? getServiceManager();
if (serviceManager !== 'systemd') {
throw new Error(
'restart:stack only supports Linux systemd services in this repo',
);
}
const services = getConfiguredServiceNames(projectRoot);
if (services.length === 0) {
throw new Error('No EJClaw services are configured in this project');
}
const execImpl = deps.execFileSyncImpl ?? execFileSync;
const systemctlArgs = deps.runningAsRoot ?? isRoot() ? [] : ['--user'];
execImpl('systemctl', [...systemctlArgs, 'restart', ...services], {
stdio: 'ignore',
});
for (const service of services) {
execImpl('systemctl', [...systemctlArgs, 'is-active', '--quiet', service], {
stdio: 'ignore',
});
}
return services;
}
export async function run(_args: string[]): Promise<void> {
const services = restartStackServices(process.cwd());
console.log(`Restarted and verified: ${services.join(', ')}`);
}
if (process.argv[1]) {
const isMain =
import.meta.url === pathToFileURL(process.argv[1]).href ||
import.meta.url === pathToFileURL(process.argv[1]).toString();
if (isMain) {
run([]).catch((error) => {
const message = error instanceof Error ? error.message : String(error);
console.error(message);
process.exit(1);
});
}
}

62
setup/service-defs.ts Normal file
View File

@@ -0,0 +1,62 @@
import fs from 'fs';
import path from 'path';
export interface ServiceDef {
/** systemd unit name / nohup script name */
name: string;
/** launchd label */
launchdLabel: string;
/** Human-readable description for systemd/launchd */
description: string;
/** Log file prefix (e.g. "ejclaw" → logs/ejclaw.log) */
logName: string;
/** Absolute path to EnvironmentFile (systemd) — loaded before Environment= */
environmentFile?: string;
/** Extra Environment= lines for systemd / env dict entries for launchd */
extraEnv?: Record<string, string>;
}
export function getServiceDefs(projectRoot: string): ServiceDef[] {
const defs: ServiceDef[] = [
{
name: 'ejclaw',
launchdLabel: 'com.ejclaw',
description: 'EJClaw Personal Assistant (Claude Code)',
logName: 'ejclaw',
},
];
const codexEnvPath = path.join(projectRoot, '.env.codex');
if (fs.existsSync(codexEnvPath)) {
defs.push({
name: 'ejclaw-codex',
launchdLabel: 'com.ejclaw-codex',
description: 'EJClaw Codex Assistant',
logName: 'ejclaw-codex',
environmentFile: codexEnvPath,
extraEnv: {
ASSISTANT_NAME: 'codex',
},
});
}
const reviewEnvPath = path.join(projectRoot, '.env.codex-review');
if (fs.existsSync(reviewEnvPath)) {
defs.push({
name: 'ejclaw-review',
launchdLabel: 'com.ejclaw-review',
description: 'EJClaw Codex Review Assistant',
logName: 'ejclaw-review',
environmentFile: reviewEnvPath,
extraEnv: {
ASSISTANT_NAME: 'codex',
},
});
}
return defs;
}
export function getConfiguredServiceNames(projectRoot: string): string[] {
return getServiceDefs(projectRoot).map((def) => def.name);
}

View File

@@ -1,5 +1,9 @@
import { describe, it, expect } from 'vitest';
import fs from 'fs';
import os from 'os';
import path from 'path';
import { afterEach, describe, expect, it } from 'vitest';
import { getServiceDefs } from './service-defs.js';
/**
* Tests for service configuration generation.
@@ -174,3 +178,28 @@ echo $! > ${JSON.stringify(pidFile)}`;
expect(wrapper).toContain('ejclaw.pid');
});
});
describe('service definitions', () => {
const tempRoots: string[] = [];
afterEach(() => {
for (const root of tempRoots.splice(0)) {
fs.rmSync(root, { recursive: true, force: true });
}
});
it('includes the review service when .env.codex-review exists', () => {
const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'ejclaw-stack-'));
tempRoots.push(tempRoot);
fs.writeFileSync(path.join(tempRoot, '.env.codex'), 'A=1\n');
fs.writeFileSync(path.join(tempRoot, '.env.codex-review'), 'B=1\n');
const defs = getServiceDefs(tempRoot);
expect(defs.map((def) => def.name)).toEqual([
'ejclaw',
'ejclaw-codex',
'ejclaw-review',
]);
});
});

View File

@@ -2,9 +2,10 @@
* Step: service — Generate and load service manager config.
* Replaces 08-setup-service.sh
*
* Supports dual-service architecture:
* Supports the EJClaw service stack:
* - ejclaw (Claude Code) — always installed
* - ejclaw-codex (Codex) — installed when .env.codex exists
* - ejclaw-review (Codex Review) — installed when .env.codex-review exists
*/
import { execSync } from 'child_process';
import fs from 'fs';
@@ -16,61 +17,11 @@ import {
getPlatform,
getNodePath,
getServiceManager,
hasSystemd,
isRoot,
isWSL,
} from './platform.js';
import { getServiceDefs, type ServiceDef } from './service-defs.js';
import { emitStatus } from './status.js';
/* ------------------------------------------------------------------ */
/* Service definition */
/* ------------------------------------------------------------------ */
interface ServiceDef {
/** systemd unit name / nohup script name */
name: string;
/** launchd label */
launchdLabel: string;
/** Human-readable description for systemd/launchd */
description: string;
/** Log file prefix (e.g. "ejclaw" → logs/ejclaw.log) */
logName: string;
/** Absolute path to EnvironmentFile (systemd) — loaded before Environment= */
environmentFile?: string;
/** Extra Environment= lines for systemd / env dict entries for launchd */
extraEnv?: Record<string, string>;
}
function getServiceDefs(projectRoot: string): ServiceDef[] {
const defs: ServiceDef[] = [
{
name: 'ejclaw',
launchdLabel: 'com.ejclaw',
description: 'EJClaw Personal Assistant (Claude Code)',
logName: 'ejclaw',
},
];
const codexEnvPath = path.join(projectRoot, '.env.codex');
if (fs.existsSync(codexEnvPath)) {
defs.push({
name: 'ejclaw-codex',
launchdLabel: 'com.ejclaw-codex',
description: 'EJClaw Codex Assistant',
logName: 'ejclaw-codex',
environmentFile: codexEnvPath,
extraEnv: {
ASSISTANT_NAME: 'codex',
},
});
logger.info(
'Detected .env.codex — will also install ejclaw-codex service',
);
}
return defs;
}
/* ------------------------------------------------------------------ */
/* Entry point */
/* ------------------------------------------------------------------ */
@@ -107,6 +58,16 @@ export async function run(_args: string[]): Promise<void> {
fs.mkdirSync(path.join(projectRoot, 'logs'), { recursive: true });
const serviceDefs = getServiceDefs(projectRoot);
if (serviceDefs.some((def) => def.name === 'ejclaw-codex')) {
logger.info(
'Detected .env.codex — will also install ejclaw-codex service',
);
}
if (serviceDefs.some((def) => def.name === 'ejclaw-review')) {
logger.info(
'Detected .env.codex-review — will also install ejclaw-review service',
);
}
if (platform === 'macos') {
for (const def of serviceDefs) {

View File

@@ -2,9 +2,10 @@
* Step: verify — End-to-end health check of the full installation.
* Replaces 09-verify.sh
*
* Supports dual-service architecture:
* Supports the EJClaw service stack:
* - ejclaw (Claude Code) — always checked
* - ejclaw-codex (Codex) — checked when .env.codex exists
* - ejclaw-review (Codex Review) — checked when .env.codex-review exists
*
* Uses better-sqlite3 directly (no sqlite3 CLI), platform-aware service checks.
*/
@@ -18,6 +19,7 @@ import { STORE_DIR } from '../src/config.js';
import { readEnvFile } from '../src/env.js';
import { logger } from '../src/logger.js';
import { getPlatform, getServiceManager, isRoot } from './platform.js';
import { getServiceDefs } from './service-defs.js';
import { emitStatus } from './status.js';
/* ------------------------------------------------------------------ */
@@ -113,28 +115,11 @@ export async function run(_args: string[]): Promise<void> {
logger.info('Starting verification');
// 1. Check service statuses
const services: ServiceCheck[] = [];
// Primary service (always checked)
services.push({
name: 'ejclaw',
status: checkService(projectRoot, mgr, 'ejclaw', 'com.ejclaw'),
});
// Codex service (checked when .env.codex exists)
const codexEnvPath = path.join(projectRoot, '.env.codex');
const codexConfigured = fs.existsSync(codexEnvPath);
if (codexConfigured) {
services.push({
name: 'ejclaw-codex',
status: checkService(
projectRoot,
mgr,
'ejclaw-codex',
'com.ejclaw-codex',
),
});
}
const serviceDefs = getServiceDefs(projectRoot);
const services: ServiceCheck[] = serviceDefs.map((def) => ({
name: def.name,
status: checkService(projectRoot, mgr, def.name, def.launchdLabel),
}));
for (const svc of services) {
logger.info({ service: svc.name, status: svc.status }, 'Service status');
@@ -195,12 +180,18 @@ export async function run(_args: string[]): Promise<void> {
}
// Determine overall status
const primaryRunning = services[0].status === 'running';
const codexOk = !codexConfigured || services[1]?.status === 'running';
const allConfiguredServicesRunning = services.every(
(service) => service.status === 'running',
);
const codexConfigured = serviceDefs.some(
(service) => service.name === 'ejclaw-codex',
);
const reviewConfigured = serviceDefs.some(
(service) => service.name === 'ejclaw-review',
);
const status =
primaryRunning &&
codexOk &&
allConfiguredServicesRunning &&
credentials !== 'missing' &&
anyChannelConfigured &&
registeredGroups > 0
@@ -225,6 +216,7 @@ export async function run(_args: string[]): Promise<void> {
REGISTERED_GROUPS: registeredGroups,
GROUPS_BY_AGENT: JSON.stringify(groupsByAgent),
CODEX_CONFIGURED: codexConfigured,
REVIEW_CONFIGURED: reviewConfigured,
STATUS: status,
LOG: 'logs/setup.log',
});