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

Function runDaemonSession

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

Source from the content-addressed store, hash-verified

1141 });
1142
1143const runDaemonSession = (input: {
1144 port: number;
1145 hostname: string;
1146 allowedHosts: ReadonlyArray<string>;
1147 authToken: string | undefined;
1148}) =>
1149 Effect.gen(function* () {
1150 const daemonHost = canonicalDaemonHost(input.hostname);
1151 const restoreWebBaseUrl = installDefaultExecutorWebBaseUrl(
1152 daemonBaseUrl(daemonHost, input.port),
1153 );
1154 const scopeId = currentDaemonScopeId();
1155
1156 try {
1157 // No process-level startup lock: the DB ownership lock acquired inside
1158 // startServer (openOwnedLocalDatabase) is the real gate. server.json and
1159 // the daemon pointer are attach/dedup hints, and the checks below are
1160 // friendly fast-paths that may race without being unsafe.
1161
1162 // A supervised daemon (launchd/systemd) is the OS-guaranteed singleton
1163 // — kickstart -k kills the old instance before starting the new — so any
1164 // server.json from a previous boot is stale. Reclaim it rather than
1165 // refusing: across a reboot the recorded pid may have been recycled by
1166 // an unrelated process, which would otherwise make the "is one already
1167 // running?" check treat it as alive-but-unreachable, refuse to start,
1168 // and crash-loop under KeepAlive. (Found by a real reboot test with
1169 // integration data in the DB.)
1170 if (process.env.EXECUTOR_SUPERVISED) {
1171 yield* takeOverActiveLocalServer().pipe(Effect.ignore);
1172 } else {
1173 yield* assertNoOtherActiveLocalServer();
1174 }
1175
1176 const existing = yield* readDaemonPointer({ hostname: daemonHost, scopeId });
1177
1178 if (existing) {
1179 const existingUrl = daemonBaseUrl(existing.hostname, existing.port);
1180 if (isPidAlive(existing.pid) && (yield* isServerReachable(existingUrl))) {
1181 if (process.env.EXECUTOR_SUPERVISED) {
1182 yield* terminatePid(existing.pid).pipe(Effect.ignore);
1183 const stopped = yield* waitForUnreachable({
1184 check: isServerReachable(existingUrl),
1185 timeoutMs: DAEMON_STOP_TIMEOUT_MS,
1186 intervalMs: DAEMON_BOOT_POLL_MS,
1187 });
1188 if (!stopped) {
1189 return yield* Effect.fail(
1190 new Error(
1191 [
1192 `The existing daemon for scope ${scopeId} at ${existingUrl} (pid ${existing.pid}) did not stop within ${DAEMON_STOP_TIMEOUT_MS / 1000}s.`,
1193 "Stop it manually and re-run.",
1194 ].join("\n"),
1195 ),
1196 );
1197 }
1198 } else {
1199 return yield* Effect.fail(
1200 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