| ... | @@ -9,7 +9,6 @@ import * as Layer from "effect/Layer"; | ... | @@ -9,7 +9,6 @@ import * as Layer from "effect/Layer"; |
| 9 | import * as Option from "effect/Option"; | 9 | import * as Option from "effect/Option"; |
| 10 | import * as PlatformError from "effect/PlatformError"; | 10 | import * as PlatformError from "effect/PlatformError"; |
| 11 | import * as Ref from "effect/Ref"; | 11 | import * as Ref from "effect/Ref"; |
| 12 | import * as Result from "effect/Result"; | | |
| 13 | import * as Schedule from "effect/Schedule"; | 12 | import * as Schedule from "effect/Schedule"; |
| 14 | import * as Schema from "effect/Schema"; | 13 | import * as Schema from "effect/Schema"; |
| 15 | import * as Semaphore from "effect/Semaphore"; | 14 | import * as Semaphore from "effect/Semaphore"; |
| ... | @@ -43,64 +42,107 @@ type BackendProcessRunRequirements = BackendProcessLayerServices | Scope.Scope; | ... | @@ -43,64 +42,107 @@ type BackendProcessRunRequirements = BackendProcessLayerServices | Scope.Scope; |
| 43 | | 42 | |
| 44 | export type BackendProcessOutputStream = "stdout" | "stderr"; | 43 | export type BackendProcessOutputStream = "stdout" | "stderr"; |
| 45 | | 44 | |
| 46 | export interface DesktopBackendStartConfig { | 45 | export interface BackendProcessContext { |
| 47 | readonly executablePath: string; | 46 | readonly executablePath: string; |
| 48 | readonly entryPath: string; | 47 | readonly entryPath: string; |
| 49 | readonly cwd: string; | 48 | readonly cwd: string; |
| | 49 | readonly httpBaseUrl: URL; |
| | 50 | } |
| | 51 | |
| | 52 | export interface DesktopBackendStartConfig extends BackendProcessContext { |
| 50 | readonly env: Record<string, string | undefined>; | 53 | readonly env: Record<string, string | undefined>; |
| 51 | readonly bootstrap: DesktopBackendBootstrapValue; | 54 | readonly bootstrap: DesktopBackendBootstrapValue; |
| 52 | readonly httpBaseUrl: URL; | | |
| 53 | readonly captureOutput: boolean; | 55 | readonly captureOutput: boolean; |
| 54 | } | 56 | } |
| 55 | | 57 | |
| 56 | interface BackendProcessExit { | 58 | interface BackendProcessExit { |
| 57 | readonly code: Option.Option<number>; | 59 | readonly code: Option.Option<number>; |
| 58 | readonly reason: string; | 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 | const backendProcessContextSchema = { |
| 63 | "BackendTimeoutError", | 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 | override get message(): string { | 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 | "BackendProcessBootstrapEncodeError", | 85 | "BackendProcessBootstrapEncodeError", |
| 75 | { | 86 | { |
| 76 | detail: Schema.String, | 87 | ...backendProcessContextSchema, |
| 77 | cause: Schema.Defect(), | 88 | cause: Schema.Defect(), |
| 78 | }, | 89 | }, |
| 79 | ) { | 90 | ) { |
| 80 | override get message(): string { | 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 | "BackendProcessSpawnError", | 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 | cause: Schema.Defect(), | 126 | cause: Schema.Defect(), |
| 90 | }, | 127 | }, |
| 91 | ) { | 128 | ) { |
| 92 | override get message(): string { | 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 | interface RunBackendProcessOptions extends DesktopBackendStartConfig { | 141 | interface RunBackendProcessOptions extends DesktopBackendStartConfig { |
| 100 | readonly readinessTimeout?: Duration.Duration; | 142 | readonly readinessTimeout?: Duration.Duration; |
| 101 | readonly onStarted?: (pid: number) => Effect.Effect<void>; | 143 | readonly onStarted?: (pid: number) => Effect.Effect<void>; |
| 102 | readonly onReady?: () => Effect.Effect<void>; | 144 | readonly onReady?: () => Effect.Effect<void>; |
| 103 | readonly onReadinessFailure?: (error: BackendTimeoutError) => Effect.Effect<void>; | 145 | readonly onReadinessFailure?: (error: BackendReadinessTimeoutError) => Effect.Effect<void>; |
| 104 | readonly onOutput?: ( | 146 | readonly onOutput?: ( |
| 105 | streamName: BackendProcessOutputStream, | 147 | streamName: BackendProcessOutputStream, |
| 106 | chunk: Uint8Array, | 148 | chunk: Uint8Array, |
| ... | @@ -183,11 +225,10 @@ const closeRun = ( | ... | @@ -183,11 +225,10 @@ const closeRun = ( |
| 183 | ).pipe(Effect.ignore); | 225 | ).pipe(Effect.ignore); |
| 184 | }; | 226 | }; |
| 185 | | 227 | |
| 186 | const waitForHttpReady = Effect.fn("desktop.backendManager.waitForHttpReady")(function* ( | 228 | export const waitForHttpReady = Effect.fn("desktop.backendManager.waitForHttpReady")(function* ( |
| 187 | baseUrl: URL, | 229 | options: BackendProcessContext & { readonly timeout: Duration.Duration }, |
| 188 | timeout: Duration.Duration, | 230 | ): Effect.fn.Return<void, BackendReadinessTimeoutError, HttpClient.HttpClient> { |
| 189 | ): Effect.fn.Return<void, BackendTimeoutError, HttpClient.HttpClient> { | 231 | const readinessUrl = new URL(BACKEND_READINESS_PATH, options.httpBaseUrl); |
| 190 | const readinessUrl = new URL(BACKEND_READINESS_PATH, baseUrl); | | |
| 191 | const client = (yield* HttpClient.HttpClient).pipe( | 232 | const client = (yield* HttpClient.HttpClient).pipe( |
| 192 | HttpClient.filterStatusOk, | 233 | HttpClient.filterStatusOk, |
| 193 | HttpClient.transformResponse(Effect.timeout(DEFAULT_BACKEND_READINESS_REQUEST_TIMEOUT)), | 234 | HttpClient.transformResponse(Effect.timeout(DEFAULT_BACKEND_READINESS_REQUEST_TIMEOUT)), |
| ... | @@ -196,29 +237,22 @@ const waitForHttpReady = Effect.fn("desktop.backendManager.waitForHttpReady")(fu | ... | @@ -196,29 +237,22 @@ const waitForHttpReady = Effect.fn("desktop.backendManager.waitForHttpReady")(fu |
| 196 | | 237 | |
| 197 | yield* client.get(readinessUrl).pipe( | 238 | yield* client.get(readinessUrl).pipe( |
| 198 | Effect.asVoid, | 239 | Effect.asVoid, |
| 199 | Effect.timeout(timeout), | 240 | Effect.timeout(options.timeout), |
| 200 | Effect.mapError(() => new BackendTimeoutError({ url: readinessUrl })), | 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 | function drainBackendOutput( | 256 | function drainBackendOutput( |
| 223 | streamName: BackendProcessOutputStream, | 257 | streamName: BackendProcessOutputStream, |
| 224 | stream: Stream.Stream<Uint8Array, PlatformError.PlatformError>, | 258 | stream: Stream.Stream<Uint8Array, PlatformError.PlatformError>, |
| ... | @@ -232,7 +266,7 @@ function drainBackendOutput( | ... | @@ -232,7 +266,7 @@ function drainBackendOutput( |
| 232 | | 266 | |
| 233 | const encodeBootstrapJson = Schema.encodeEffect(Schema.fromJsonString(DesktopBackendBootstrap)); | 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 | options: RunBackendProcessOptions, | 270 | options: RunBackendProcessOptions, |
| 237 | ): Effect.fn.Return<BackendProcessExit, BackendProcessError, BackendProcessRunRequirements> { | 271 | ): Effect.fn.Return<BackendProcessExit, BackendProcessError, BackendProcessRunRequirements> { |
| 238 | const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; | 272 | const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; |
| ... | @@ -240,7 +274,10 @@ const runBackendProcess = Effect.fn("runBackendProcess")(function* ( | ... | @@ -240,7 +274,10 @@ const runBackendProcess = Effect.fn("runBackendProcess")(function* ( |
| 240 | Effect.mapError( | 274 | Effect.mapError( |
| 241 | (cause) => | 275 | (cause) => |
| 242 | new BackendProcessBootstrapEncodeError({ | 276 | new BackendProcessBootstrapEncodeError({ |
| 243 | detail: cause.message, | 277 | executablePath: options.executablePath, |
| | 278 | entryPath: options.entryPath, |
| | 279 | cwd: options.cwd, |
| | 280 | httpBaseUrl: options.httpBaseUrl, |
| 244 | cause, | 281 | cause, |
| 245 | }), | 282 | }), |
| 246 | ), | 283 | ), |
| ... | @@ -273,7 +310,10 @@ const runBackendProcess = Effect.fn("runBackendProcess")(function* ( | ... | @@ -273,7 +310,10 @@ const runBackendProcess = Effect.fn("runBackendProcess")(function* ( |
| 273 | Effect.mapError( | 310 | Effect.mapError( |
| 274 | (cause) => | 311 | (cause) => |
| 275 | new BackendProcessSpawnError({ | 312 | new BackendProcessSpawnError({ |
| 276 | detail: cause.message, | 313 | executablePath: options.executablePath, |
| | 314 | entryPath: options.entryPath, |
| | 315 | cwd: options.cwd, |
| | 316 | httpBaseUrl: options.httpBaseUrl, |
| 277 | cause, | 317 | cause, |
| 278 | }), | 318 | }), |
| 279 | ), | 319 | ), |
| ... | @@ -284,16 +324,37 @@ const runBackendProcess = Effect.fn("runBackendProcess")(function* ( | ... | @@ -284,16 +324,37 @@ const runBackendProcess = Effect.fn("runBackendProcess")(function* ( |
| 284 | yield* drainBackendOutput("stdout", handle.stdout, onOutput).pipe(Effect.forkScoped); | 324 | yield* drainBackendOutput("stdout", handle.stdout, onOutput).pipe(Effect.forkScoped); |
| 285 | yield* drainBackendOutput("stderr", handle.stderr, onOutput).pipe(Effect.forkScoped); | 325 | yield* drainBackendOutput("stderr", handle.stderr, onOutput).pipe(Effect.forkScoped); |
| 286 | } | 326 | } |
| 287 | yield* waitForHttpReady( | 327 | yield* waitForHttpReady({ |
| 288 | options.httpBaseUrl, | 328 | executablePath: options.executablePath, |
| 289 | options.readinessTimeout ?? DEFAULT_BACKEND_READINESS_TIMEOUT, | 329 | entryPath: options.entryPath, |
| 290 | ).pipe( | 330 | cwd: options.cwd, |
| | 331 | httpBaseUrl: options.httpBaseUrl, |
| | 332 | timeout: options.readinessTimeout ?? DEFAULT_BACKEND_READINESS_TIMEOUT, |
| | 333 | }).pipe( |
| 291 | Effect.tap(() => options.onReady?.() ?? Effect.void), | 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 | Effect.forkScoped, | 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 | export const make = Effect.gen(function* () { | 360 | export const make = Effect.gen(function* () { |
| ... | @@ -351,7 +412,7 @@ export const make = Effect.gen(function* () { | ... | @@ -351,7 +412,7 @@ export const make = Effect.gen(function* () { |
| 351 | const config = yield* configuration.resolve.pipe( | 412 | const config = yield* configuration.resolve.pipe( |
| 352 | Effect.tapError((error) => | 413 | Effect.tapError((error) => |
| 353 | logBackendManagerError("failed to generate desktop backend configuration", { | 414 | logBackendManagerError("failed to generate desktop backend configuration", { |
| 354 | cause: error.message, | 415 | cause: error, |
| 355 | }), | 416 | }), |
| 356 | ), | 417 | ), |
| 357 | Effect.option, | 418 | Effect.option, |
| ... | @@ -489,14 +550,14 @@ export const make = Effect.gen(function* () { | ... | @@ -489,14 +550,14 @@ export const make = Effect.gen(function* () { |
| 489 | yield* desktopWindow.handleBackendReady.pipe( | 550 | yield* desktopWindow.handleBackendReady.pipe( |
| 490 | Effect.catch((error) => | 551 | Effect.catch((error) => |
| 491 | logBackendManagerError("failed to open main window after backend readiness", { | 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 | onReadinessFailure: (error) => | 558 | onReadinessFailure: (error) => |
| 498 | logBackendManagerWarning("backend readiness check failed during bootstrap", { | 559 | logBackendManagerWarning("backend readiness check failed during bootstrap", { |
| 499 | error: error.message, | 560 | error, |
| 500 | }), | 561 | }), |
| 501 | onOutput: (streamName, chunk) => backendOutputLog.writeOutputChunk(streamName, chunk), | 562 | onOutput: (streamName, chunk) => backendOutputLog.writeOutputChunk(streamName, chunk), |
| 502 | }).pipe( | 563 | }).pipe( |
| ... | @@ -504,7 +565,10 @@ export const make = Effect.gen(function* () { | ... | @@ -504,7 +565,10 @@ export const make = Effect.gen(function* () { |
| 504 | Effect.provideService(HttpClient.HttpClient, httpClient), | 565 | Effect.provideService(HttpClient.HttpClient, httpClient), |
| 505 | Scope.provide(runScope), | 566 | Scope.provide(runScope), |
| 506 | Effect.matchEffect({ | 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 | onSuccess: (exit) => finalizeRun(exit.reason), | 572 | onSuccess: (exit) => finalizeRun(exit.reason), |
| 509 | }), | 573 | }), |
| 510 | Effect.ensuring(Scope.close(runScope, Exit.void).pipe(Effect.ignore)), | 574 | Effect.ensuring(Scope.close(runScope, Exit.void).pipe(Effect.ignore)), |
| ... | @@ -559,11 +623,17 @@ export const make = Effect.gen(function* () { | ... | @@ -559,11 +623,17 @@ export const make = Effect.gen(function* () { |
| 559 | }), | 623 | }), |
| 560 | ), | 624 | ), |
| 561 | Effect.flatMap((shouldRestart) => (shouldRestart ? start : Effect.void)), | 625 | Effect.flatMap((shouldRestart) => (shouldRestart ? start : Effect.void)), |
| 562 | Effect.catchCause((cause) => | 626 | Effect.catchCause((cause) => { |
| 563 | logBackendManagerError("desktop backend restart fiber failed", { | 627 | if (Cause.hasInterruptsOnly(cause)) { |
| 564 | cause: Cause.pretty(cause), | 628 | return Effect.void; |
| 565 | }), | 629 | } |
| 566 | ), | 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 | parentScope, | 638 | parentScope, |
| 569 | ); | 639 | ); |