MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / runDaemonSession

Function runDaemonSession

apps/cli/src/main.ts:1187–1316  ·  view source on GitHub ↗
(input: {
  port: number;
  hostname: string;
  allowedHosts: ReadonlyArray<string>;
  authToken: string | undefined;
})

Source from the content-addressed store, hash-verified

1185 });
1186
1187const runDaemonSession = (input: {
1188 port: number;
1189 hostname: string;
1190 allowedHosts: ReadonlyArray<string>;
1191 authToken: string | undefined;
1192}) =>
1193 Effect.gen(function* () {
1194 const daemonHost = canonicalDaemonHost(input.hostname);
1195 const restoreWebBaseUrl = installDefaultExecutorWebBaseUrl(
1196 daemonBaseUrl(daemonHost, input.port),
1197 );
1198 const scopeId = currentDaemonScopeId();
1199
1200 try {
1201 // No process-level startup lock: the DB ownership lock acquired inside
1202 // startServer (openOwnedLocalDatabase) is the real gate. server.json and
1203 // the daemon pointer are attach/dedup hints, and the checks below are
1204 // friendly fast-paths that may race without being unsafe.
1205
1206 // A supervised daemon (launchd/systemd) is the OS-guaranteed singleton
1207 // — kickstart -k kills the old instance before starting the new — so any
1208 // server.json from a previous boot is stale. Reclaim it rather than
1209 // refusing: across a reboot the recorded pid may have been recycled by
1210 // an unrelated process, which would otherwise make the "is one already
1211 // running?" check treat it as alive-but-unreachable, refuse to start,
1212 // and crash-loop under KeepAlive. (Found by a real reboot test with
1213 // integration data in the DB.)
1214 if (process.env.EXECUTOR_SUPERVISED) {
1215 yield* takeOverActiveLocalServer().pipe(Effect.ignore);
1216 } else {
1217 yield* assertNoOtherActiveLocalServer();
1218 }
1219
1220 const existing = yield* readDaemonPointer({ hostname: daemonHost, scopeId });
1221
1222 if (existing) {
1223 const existingUrl = daemonBaseUrl(existing.hostname, existing.port);
1224 if (isPidAlive(existing.pid) && (yield* isServerReachable(existingUrl))) {
1225 if (process.env.EXECUTOR_SUPERVISED) {
1226 yield* terminatePid(existing.pid).pipe(Effect.ignore);
1227 const stopped = yield* waitForUnreachable({
1228 check: isServerReachable(existingUrl),
1229 timeoutMs: DAEMON_STOP_TIMEOUT_MS,
1230 intervalMs: DAEMON_BOOT_POLL_MS,
1231 });
1232 if (!stopped) {
1233 return yield* Effect.fail(
1234 new Error(
1235 [
1236 `The existing daemon for scope ${scopeId} at ${existingUrl} (pid ${existing.pid}) did not stop within ${DAEMON_STOP_TIMEOUT_MS / 1000}s.`,
1237 "Stop it manually and re-run.",
1238 ].join("\n"),
1239 ),
1240 );
1241 }
1242 } else {
1243 return yield* Effect.fail(
1244 new Error(

Callers 1

main.tsFile · 0.85

Calls 15

canonicalDaemonHostFunction · 0.90
currentDaemonScopeIdFunction · 0.90
readDaemonPointerFunction · 0.90
isPidAliveFunction · 0.90
terminatePidFunction · 0.90
waitForUnreachableFunction · 0.90
writeDaemonRecordFunction · 0.90
writeDaemonPointerFunction · 0.90
removeDaemonRecordFunction · 0.90
removeDaemonPointerFunction · 0.90

Tested by

no test coverage detected