MCPcopy Create free account
hub / github.com/Gan-Xing/CodexBridge / runDaemonSupervisor

Function runDaemonSupervisor

packages/codex-native-api/src/daemon_manager.ts:138–193  ·  view source on GitHub ↗
(argv: string[])

Source from the content-addressed store, hash-verified

136}
137
138export async function runDaemonSupervisor(argv: string[]): Promise<void> {
139 const options = parseSupervisorArgs(argv);
140 const layout = resolveDaemonLayout(process.env, {
141 homeDir: options.homeDir ? path.resolve(options.homeDir) : null,
142 });
143 const envFile = path.resolve(options.envFile ?? layout.envFile);
144 const stdoutLog = path.resolve(options.stdoutLog ?? layout.stdoutLog);
145 const stderrLog = path.resolve(options.stderrLog ?? layout.stderrLog);
146
147 await loadEnvFileIntoProcessEnv(envFile);
148 process.env.HOME ||= layout.homeDir;
149 process.env.USERPROFILE ||= layout.homeDir;
150 if (process.platform === 'win32') {
151 process.env.APPDATA ||= path.join(layout.homeDir, 'AppData', 'Roaming');
152 process.env.LOCALAPPDATA ||= path.join(layout.homeDir, 'AppData', 'Local');
153 }
154
155 await fsp.mkdir(path.dirname(stdoutLog), { recursive: true });
156 await fsp.mkdir(path.dirname(stderrLog), { recursive: true });
157
158 let child: ReturnType<typeof spawn> | null = null;
159 let stopping = false;
160 process.on('SIGINT', () => stop('SIGINT'));
161 process.on('SIGTERM', () => stop('SIGTERM'));
162
163 do {
164 const code = await runSupervisorChild({
165 stdoutLog,
166 stderrLog,
167 onChild: (nextChild) => {
168 child = nextChild;
169 },
170 });
171 child = null;
172 if (stopping || options.once) {
173 process.exitCode = typeof code === 'number' ? code : 0;
174 break;
175 }
176 const restartMs = Math.max(
177 0,
178 Number.parseFloat(process.env.CODEX_NATIVE_API_RESTART_SEC ?? '2') * 1000,
179 );
180 await sleep(restartMs);
181 } while (!stopping);
182
183 function stop(signal: string) {
184 if (stopping) {
185 return;
186 }
187 stopping = true;
188 writeLogLine(stderrLog, 'stderr', `[codex-native-api-daemon] stopping on ${signal}`);
189 if (child && !child.killed) {
190 child.kill(signal as NodeJS.Signals);
191 }
192 }
193}
194
195export async function buildDaemonInstallPlan(

Callers 1

mainFunction · 0.85

Calls 7

parseSupervisorArgsFunction · 0.85
resolveDaemonLayoutFunction · 0.85
runSupervisorChildFunction · 0.85
resolveMethod · 0.80
stopFunction · 0.70
sleepFunction · 0.70

Tested by

no test coverage detected