(
options: DaemonRuntimeOptions = {},
)
| 65 | }; |
| 66 | |
| 67 | export async function startDaemonRuntime( |
| 68 | options: DaemonRuntimeOptions = {}, |
| 69 | ): Promise<DaemonRuntimeController | null> { |
| 70 | const env = options.env ?? process.env; |
| 71 | const stdout = options.stdout ?? process.stdout; |
| 72 | const stderr = options.stderr ?? process.stderr; |
| 73 | const exit = options.exit ?? ((code: number) => process.exit(code)); |
| 74 | const daemonPaths = resolveDaemonPaths(env.AGENT_DEVICE_STATE_DIR); |
| 75 | const { baseDir, infoPath, lockPath, logPath, sessionsDir } = daemonPaths; |
| 76 | const daemonServerMode = resolveDaemonServerMode(env.AGENT_DEVICE_DAEMON_SERVER_MODE); |
| 77 | const retainArtifacts = isEnvTruthy(env.AGENT_DEVICE_RETAIN_ARTIFACTS); |
| 78 | setRunnerLeaseOwnerStateDir(baseDir); |
| 79 | |
| 80 | cleanupStaleAppLogProcesses(sessionsDir); |
| 81 | |
| 82 | const sessionStore = new SessionStore(sessionsDir); |
| 83 | const leaseRegistry = new LeaseRegistry({ |
| 84 | maxActiveSimulatorLeases: parseIntegerEnv(env.AGENT_DEVICE_MAX_SIMULATOR_LEASES), |
| 85 | defaultLeaseTtlMs: parseIntegerEnv(env.AGENT_DEVICE_LEASE_TTL_MS), |
| 86 | minLeaseTtlMs: parseIntegerEnv(env.AGENT_DEVICE_LEASE_MIN_TTL_MS), |
| 87 | maxLeaseTtlMs: parseIntegerEnv(env.AGENT_DEVICE_LEASE_MAX_TTL_MS), |
| 88 | }); |
| 89 | const version = readVersion(); |
| 90 | const token = crypto.randomBytes(24).toString('hex'); |
| 91 | const daemonProcessStartTime = readProcessStartTime(process.pid) ?? undefined; |
| 92 | const daemonCodeSignature = resolveDaemonCodeSignature(); |
| 93 | const providerDeviceRuntimes = createDefaultCloudWebDriverProviderRuntimes(env); |
| 94 | const providerRuntimeProviders = |
| 95 | createProviderDeviceRuntimeRequestProviders(providerDeviceRuntimes); |
| 96 | const cloudArtifactProvider = composeCloudArtifactProviders( |
| 97 | providerRuntimeProviders.cloudArtifactProvider, |
| 98 | createDefaultCloudArtifactProvider(env), |
| 99 | ); |
| 100 | |
| 101 | const handleRequest = createRequestHandler({ |
| 102 | logPath, |
| 103 | stateDir: baseDir, |
| 104 | token, |
| 105 | sessionStore, |
| 106 | leaseRegistry, |
| 107 | leaseLifecycleProvider: providerRuntimeProviders.leaseLifecycleProvider, |
| 108 | cloudArtifactProvider, |
| 109 | deviceInventoryProvider: providerRuntimeProviders.deviceInventoryProvider, |
| 110 | providerDeviceRuntimeScope: providerRuntimeProviders.providerDeviceRuntimeScope, |
| 111 | trackDownloadableArtifact, |
| 112 | }); |
| 113 | |
| 114 | const emitFatalDiagnostic = async (error: unknown): Promise<void> => { |
| 115 | await withDiagnosticsScope( |
| 116 | { command: 'daemon', session: 'daemon', logPath, debug: true }, |
| 117 | async () => { |
| 118 | emitDiagnostic({ |
| 119 | level: 'error', |
| 120 | phase: 'daemon_fatal', |
| 121 | data: { |
| 122 | error: error instanceof Error ? error.message : String(error), |
| 123 | }, |
| 124 | }); |
no test coverage detected