| ... | ... | @@ -9,7 +9,6 @@ import * as Layer from "effect/Layer"; |
| 9 | 9 | import * as Option from "effect/Option"; |
| 10 | 10 | import * as PlatformError from "effect/PlatformError"; |
| 11 | 11 | import * as Ref from "effect/Ref"; |
| 12 | | import * as Result from "effect/Result"; |
| 13 | 12 | import * as Schedule from "effect/Schedule"; |
| 14 | 13 | import * as Schema from "effect/Schema"; |
| 15 | 14 | import * as Semaphore from "effect/Semaphore"; |
| ... | ... | @@ -43,64 +42,107 @@ type BackendProcessRunRequirements = BackendProcessLayerServices | Scope.Scope; |
| 43 | 42 | |
| 44 | 43 | export type BackendProcessOutputStream = "stdout" | "stderr"; |
| 45 | 44 | |
| 46 | | export interface DesktopBackendStartConfig { |
| 45 | export interface BackendProcessContext { |
| 47 | 46 | readonly executablePath: string; |
| 48 | 47 | readonly entryPath: string; |
| 49 | 48 | readonly cwd: string; |
| 49 | readonly httpBaseUrl: URL; |
| 50 | } |
| 51 | |
| 52 | export interface DesktopBackendStartConfig extends BackendProcessContext { |
| 50 | 53 | readonly env: Record<string, string | undefined>; |
| 51 | 54 | readonly bootstrap: DesktopBackendBootstrapValue; |
| 52 | | readonly httpBaseUrl: URL; |
| 53 | 55 | readonly captureOutput: boolean; |
| 54 | 56 | } |
| 55 | 57 | |
| 56 | 58 | interface BackendProcessExit { |
| 57 | 59 | readonly code: Option.Option<number>; |
| 58 | 60 | readonly reason: string; |
| 59 | | readonly result: Result.Result<ChildProcessSpawner.ExitCode, PlatformError.PlatformError>; |
| 60 | 61 | } |
| 61 | 62 | |
| 62 | | export class BackendTimeoutError extends Schema.TaggedErrorClass<BackendTimeoutError>()( |
| 63 | | "BackendTimeoutError", |
| 63 | const backendProcessContextSchema = { |
| 64 | executablePath: Schema.String, |
| 65 | entryPath: Schema.String, |
| 66 | cwd: Schema.String, |
| 67 | httpBaseUrl: Schema.URL, |
| 68 | }; |
| 69 | |
| 70 | export class BackendReadinessTimeoutError extends Schema.TaggedErrorClass<BackendReadinessTimeoutError>()( |
| 71 | "BackendReadinessTimeoutError", |
| 64 | 72 | { |
| 65 | | url: Schema.URL, |
| 73 | ...backendProcessContextSchema, |
| 74 | readinessUrl: Schema.URL, |
| 75 | timeoutMs: Schema.Number, |
| 76 | cause: Schema.Defect(), |
| 66 | 77 | }, |
| 67 | 78 | ) { |
| 68 | 79 | override get message(): string { |
| 69 | | return `Timed out waiting for backend readiness at ${this.url.href}.`; |
| 80 | return `Timed out after ${this.timeoutMs}ms waiting for desktop backend readiness at ${this.readinessUrl.href}.`; |
| 70 | 81 | } |
| 71 | 82 | } |
| 72 | 83 | |
| 73 | | class BackendProcessBootstrapEncodeError extends Schema.TaggedErrorClass<BackendProcessBootstrapEncodeError>()( |
| 84 | export class BackendProcessBootstrapEncodeError extends Schema.TaggedErrorClass<BackendProcessBootstrapEncodeError>()( |
| 74 | 85 | "BackendProcessBootstrapEncodeError", |
| 75 | 86 | { |
| 76 | | detail: Schema.String, |
| 87 | ...backendProcessContextSchema, |
| 77 | 88 | cause: Schema.Defect(), |
| 78 | 89 | }, |
| 79 | 90 | ) { |
| 80 | 91 | override get message(): string { |
| 81 | | return `Failed to encode desktop backend bootstrap payload: ${this.detail}`; |
| 92 | return `Failed to encode the desktop backend bootstrap payload for ${this.entryPath}.`; |
| 82 | 93 | } |
| 83 | 94 | } |
| 84 | 95 | |
| 85 | | class BackendProcessSpawnError extends Schema.TaggedErrorClass<BackendProcessSpawnError>()( |
| 96 | export class BackendProcessSpawnError extends Schema.TaggedErrorClass<BackendProcessSpawnError>()( |
| 86 | 97 | "BackendProcessSpawnError", |
| 87 | 98 | { |
| 88 | | detail: Schema.String, |
| 99 | ...backendProcessContextSchema, |
| 100 | cause: Schema.Defect(), |
| 101 | }, |
| 102 | ) { |
| 103 | override get message(): string { |
| 104 | return `Failed to spawn desktop backend entry ${this.entryPath} with ${this.executablePath}.`; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | export class BackendProcessExitStatusError extends Schema.TaggedErrorClass<BackendProcessExitStatusError>()( |
| 109 | "BackendProcessExitStatusError", |
| 110 | { |
| 111 | ...backendProcessContextSchema, |
| 112 | pid: Schema.Number, |
| 113 | cause: Schema.Defect(), |
| 114 | }, |
| 115 | ) { |
| 116 | override get message(): string { |
| 117 | return `Failed to read the exit status of desktop backend process ${this.pid}.`; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | export class DesktopBackendRestartError extends Schema.TaggedErrorClass<DesktopBackendRestartError>()( |
| 122 | "DesktopBackendRestartError", |
| 123 | { |
| 124 | reason: Schema.String, |
| 125 | delayMs: Schema.Number, |
| 89 | 126 | cause: Schema.Defect(), |
| 90 | 127 | }, |
| 91 | 128 | ) { |
| 92 | 129 | override get message(): string { |
| 93 | | return `Failed to spawn desktop backend process: ${this.detail}`; |
| 130 | return `Desktop backend restart failed after a scheduled ${this.delayMs}ms delay.`; |
| 94 | 131 | } |
| 95 | 132 | } |
| 96 | 133 | |
| 97 | | type BackendProcessError = BackendProcessBootstrapEncodeError | BackendProcessSpawnError; |
| 134 | export const BackendProcessError = Schema.Union([ |
| 135 | BackendProcessBootstrapEncodeError, |
| 136 | BackendProcessSpawnError, |
| 137 | BackendProcessExitStatusError, |
| 138 | ]); |
| 139 | export type BackendProcessError = typeof BackendProcessError.Type; |
| 98 | 140 | |
| 99 | 141 | interface RunBackendProcessOptions extends DesktopBackendStartConfig { |
| 100 | 142 | readonly readinessTimeout?: Duration.Duration; |
| 101 | 143 | readonly onStarted?: (pid: number) => Effect.Effect<void>; |
| 102 | 144 | readonly onReady?: () => Effect.Effect<void>; |
| 103 | | readonly onReadinessFailure?: (error: BackendTimeoutError) => Effect.Effect<void>; |
| 145 | readonly onReadinessFailure?: (error: BackendReadinessTimeoutError) => Effect.Effect<void>; |
| 104 | 146 | readonly onOutput?: ( |
| 105 | 147 | streamName: BackendProcessOutputStream, |
| 106 | 148 | chunk: Uint8Array, |
| ... | ... | @@ -183,11 +225,10 @@ const closeRun = ( |
| 183 | 225 | ).pipe(Effect.ignore); |
| 184 | 226 | }; |
| 185 | 227 | |
| 186 | | const waitForHttpReady = Effect.fn("desktop.backendManager.waitForHttpReady")(function* ( |
| 187 | | baseUrl: URL, |
| 188 | | timeout: Duration.Duration, |
| 189 | | ): Effect.fn.Return<void, BackendTimeoutError, HttpClient.HttpClient> { |
| 190 | | const readinessUrl = new URL(BACKEND_READINESS_PATH, baseUrl); |
| 228 | export const waitForHttpReady = Effect.fn("desktop.backendManager.waitForHttpReady")(function* ( |
| 229 | options: BackendProcessContext & { readonly timeout: Duration.Duration }, |
| 230 | ): Effect.fn.Return<void, BackendReadinessTimeoutError, HttpClient.HttpClient> { |
| 231 | const readinessUrl = new URL(BACKEND_READINESS_PATH, options.httpBaseUrl); |
| 191 | 232 | const client = (yield* HttpClient.HttpClient).pipe( |
| 192 | 233 | HttpClient.filterStatusOk, |
| 193 | 234 | HttpClient.transformResponse(Effect.timeout(DEFAULT_BACKEND_READINESS_REQUEST_TIMEOUT)), |
| ... | ... | @@ -196,29 +237,22 @@ const waitForHttpReady = Effect.fn("desktop.backendManager.waitForHttpReady")(fu |
| 196 | 237 | |
| 197 | 238 | yield* client.get(readinessUrl).pipe( |
| 198 | 239 | Effect.asVoid, |
| 199 | | Effect.timeout(timeout), |
| 200 | | Effect.mapError(() => new BackendTimeoutError({ url: readinessUrl })), |
| 240 | Effect.timeout(options.timeout), |
| 241 | Effect.mapError( |
| 242 | (cause) => |
| 243 | new BackendReadinessTimeoutError({ |
| 244 | executablePath: options.executablePath, |
| 245 | entryPath: options.entryPath, |
| 246 | cwd: options.cwd, |
| 247 | httpBaseUrl: options.httpBaseUrl, |
| 248 | readinessUrl, |
| 249 | timeoutMs: Duration.toMillis(options.timeout), |
| 250 | cause, |
| 251 | }), |
| 252 | ), |
| 201 | 253 | ); |
| 202 | 254 | }); |
| 203 | 255 | |
| 204 | | function describeProcessExit( |
| 205 | | result: Result.Result<ChildProcessSpawner.ExitCode, PlatformError.PlatformError>, |
| 206 | | ): BackendProcessExit { |
| 207 | | if (Result.isSuccess(result)) { |
| 208 | | return { |
| 209 | | code: Option.some(result.success), |
| 210 | | reason: `code=${result.success}`, |
| 211 | | result, |
| 212 | | }; |
| 213 | | } |
| 214 | | |
| 215 | | return { |
| 216 | | code: Option.none(), |
| 217 | | reason: result.failure.message, |
| 218 | | result, |
| 219 | | }; |
| 220 | | } |
| 221 | | |
| 222 | 256 | function drainBackendOutput( |
| 223 | 257 | streamName: BackendProcessOutputStream, |
| 224 | 258 | stream: Stream.Stream<Uint8Array, PlatformError.PlatformError>, |
| ... | ... | @@ -232,7 +266,7 @@ function drainBackendOutput( |
| 232 | 266 | |
| 233 | 267 | const encodeBootstrapJson = Schema.encodeEffect(Schema.fromJsonString(DesktopBackendBootstrap)); |
| 234 | 268 | |
| 235 | | const runBackendProcess = Effect.fn("runBackendProcess")(function* ( |
| 269 | export const runBackendProcess = Effect.fn("runBackendProcess")(function* ( |
| 236 | 270 | options: RunBackendProcessOptions, |
| 237 | 271 | ): Effect.fn.Return<BackendProcessExit, BackendProcessError, BackendProcessRunRequirements> { |
| 238 | 272 | const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; |
| ... | ... | @@ -240,7 +274,10 @@ const runBackendProcess = Effect.fn("runBackendProcess")(function* ( |
| 240 | 274 | Effect.mapError( |
| 241 | 275 | (cause) => |
| 242 | 276 | new BackendProcessBootstrapEncodeError({ |
| 243 | | detail: cause.message, |
| 277 | executablePath: options.executablePath, |
| 278 | entryPath: options.entryPath, |
| 279 | cwd: options.cwd, |
| 280 | httpBaseUrl: options.httpBaseUrl, |
| 244 | 281 | cause, |
| 245 | 282 | }), |
| 246 | 283 | ), |
| ... | ... | @@ -273,7 +310,10 @@ const runBackendProcess = Effect.fn("runBackendProcess")(function* ( |
| 273 | 310 | Effect.mapError( |
| 274 | 311 | (cause) => |
| 275 | 312 | new BackendProcessSpawnError({ |
| 276 | | detail: cause.message, |
| 313 | executablePath: options.executablePath, |
| 314 | entryPath: options.entryPath, |
| 315 | cwd: options.cwd, |
| 316 | httpBaseUrl: options.httpBaseUrl, |
| 277 | 317 | cause, |
| 278 | 318 | }), |
| 279 | 319 | ), |
| ... | ... | @@ -284,16 +324,37 @@ const runBackendProcess = Effect.fn("runBackendProcess")(function* ( |
| 284 | 324 | yield* drainBackendOutput("stdout", handle.stdout, onOutput).pipe(Effect.forkScoped); |
| 285 | 325 | yield* drainBackendOutput("stderr", handle.stderr, onOutput).pipe(Effect.forkScoped); |
| 286 | 326 | } |
| 287 | | yield* waitForHttpReady( |
| 288 | | options.httpBaseUrl, |
| 289 | | options.readinessTimeout ?? DEFAULT_BACKEND_READINESS_TIMEOUT, |
| 290 | | ).pipe( |
| 327 | yield* waitForHttpReady({ |
| 328 | executablePath: options.executablePath, |
| 329 | entryPath: options.entryPath, |
| 330 | cwd: options.cwd, |
| 331 | httpBaseUrl: options.httpBaseUrl, |
| 332 | timeout: options.readinessTimeout ?? DEFAULT_BACKEND_READINESS_TIMEOUT, |
| 333 | }).pipe( |
| 291 | 334 | Effect.tap(() => options.onReady?.() ?? Effect.void), |
| 292 | | Effect.catch((error) => options.onReadinessFailure?.(error) ?? Effect.void), |
| 335 | Effect.catchTags({ |
| 336 | BackendReadinessTimeoutError: (error) => options.onReadinessFailure?.(error) ?? Effect.void, |
| 337 | }), |
| 293 | 338 | Effect.forkScoped, |
| 294 | 339 | ); |
| 295 | 340 | |
| 296 | | return describeProcessExit(yield* Effect.result(handle.exitCode)); |
| 341 | const exitCode = yield* handle.exitCode.pipe( |
| 342 | Effect.mapError( |
| 343 | (cause) => |
| 344 | new BackendProcessExitStatusError({ |
| 345 | executablePath: options.executablePath, |
| 346 | entryPath: options.entryPath, |
| 347 | cwd: options.cwd, |
| 348 | httpBaseUrl: options.httpBaseUrl, |
| 349 | pid: Number(handle.pid), |
| 350 | cause, |
| 351 | }), |
| 352 | ), |
| 353 | ); |
| 354 | return { |
| 355 | code: Option.some(exitCode), |
| 356 | reason: `code=${exitCode}`, |
| 357 | } satisfies BackendProcessExit; |
| 297 | 358 | }); |
| 298 | 359 | |
| 299 | 360 | export const make = Effect.gen(function* () { |
| ... | ... | @@ -351,7 +412,7 @@ export const make = Effect.gen(function* () { |
| 351 | 412 | const config = yield* configuration.resolve.pipe( |
| 352 | 413 | Effect.tapError((error) => |
| 353 | 414 | logBackendManagerError("failed to generate desktop backend configuration", { |
| 354 | | cause: error.message, |
| 415 | cause: error, |
| 355 | 416 | }), |
| 356 | 417 | ), |
| 357 | 418 | Effect.option, |
| ... | ... | @@ -489,14 +550,14 @@ export const make = Effect.gen(function* () { |
| 489 | 550 | yield* desktopWindow.handleBackendReady.pipe( |
| 490 | 551 | Effect.catch((error) => |
| 491 | 552 | logBackendManagerError("failed to open main window after backend readiness", { |
| 492 | | message: error.message, |
| 553 | cause: error, |
| 493 | 554 | }), |
| 494 | 555 | ), |
| 495 | 556 | ); |
| 496 | 557 | }), |
| 497 | 558 | onReadinessFailure: (error) => |
| 498 | 559 | logBackendManagerWarning("backend readiness check failed during bootstrap", { |
| 499 | | error: error.message, |
| 560 | error, |
| 500 | 561 | }), |
| 501 | 562 | onOutput: (streamName, chunk) => backendOutputLog.writeOutputChunk(streamName, chunk), |
| 502 | 563 | }).pipe( |
| ... | ... | @@ -504,7 +565,10 @@ export const make = Effect.gen(function* () { |
| 504 | 565 | Effect.provideService(HttpClient.HttpClient, httpClient), |
| 505 | 566 | Scope.provide(runScope), |
| 506 | 567 | Effect.matchEffect({ |
| 507 | | onFailure: (error) => finalizeRun(error.message), |
| 568 | onFailure: (error) => |
| 569 | logBackendManagerError(error.message, { error }).pipe( |
| 570 | Effect.andThen(finalizeRun(error.message)), |
| 571 | ), |
| 508 | 572 | onSuccess: (exit) => finalizeRun(exit.reason), |
| 509 | 573 | }), |
| 510 | 574 | Effect.ensuring(Scope.close(runScope, Exit.void).pipe(Effect.ignore)), |
| ... | ... | @@ -559,11 +623,17 @@ export const make = Effect.gen(function* () { |
| 559 | 623 | }), |
| 560 | 624 | ), |
| 561 | 625 | Effect.flatMap((shouldRestart) => (shouldRestart ? start : Effect.void)), |
| 562 | | Effect.catchCause((cause) => |
| 563 | | logBackendManagerError("desktop backend restart fiber failed", { |
| 564 | | cause: Cause.pretty(cause), |
| 565 | | }), |
| 566 | | ), |
| 626 | Effect.catchCause((cause) => { |
| 627 | if (Cause.hasInterruptsOnly(cause)) { |
| 628 | return Effect.void; |
| 629 | } |
| 630 | const error = new DesktopBackendRestartError({ |
| 631 | reason, |
| 632 | delayMs: Duration.toMillis(delay), |
| 633 | cause, |
| 634 | }); |
| 635 | return logBackendManagerError(error.message, { error }); |
| 636 | }), |
| 567 | 637 | ), |
| 568 | 638 | parentScope, |
| 569 | 639 | ); |