answer-runs-workflow.ts
This file
Path
apps/platform/src/adapters/answer-runs-workflow.ts
63 lines · 1.9 KiB · TypeScript
Last changed by
Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8
History of this fileTake it
The walk stopped after 200 commits: lines older than 678c8b8 point at it.
Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 1–22why, one step back1import { AnswerRuns } from "@repo/project/answer-runs";2import type { ProjectName } from "@repo/protocol/ids";3import { Effect, Layer, Schema } from "effect";45import { AnswerParams } from "../answer-runs.ts";67export interface AnswerInstance {8 readonly id: string;9 readonly restart: () => Promise<void>;10 readonly status: () => Promise<{ readonly status: InstanceStatus["status"] }>;11}1213export interface AnswerWorkflow {14 readonly create: (options: {15 readonly id: string;16 readonly params: typeof AnswerParams.Encoded;17 }) => Promise<{ readonly id: string }>;18 readonly get: (id: string) => Promise<AnswerInstance>;19}2021const encodeParams = Schema.encodeEffect(AnswerParams);22Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8
line 23why, one step back23// A run ends whenever the answer waits; the project starts the same request again once it can go on.Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 24–63why, one step back24const ended: ReadonlySet<InstanceStatus["status"]> = new Set([25 "complete",26 "errored",27 "terminated",28]);2930const runAgain = (instance: AnswerInstance) =>31 Effect.gen(function* () {32 const { status } = yield* Effect.tryPromise(() => instance.status());3334 if (ended.has(status)) {35 yield* Effect.tryPromise(() => instance.restart());36 }37 });3839export const AnswerRunsWorkflow = (40 workflow: AnswerWorkflow,41 project: ProjectName42): Layer.Layer<AnswerRuns> =>43 Layer.succeed(44 AnswerRuns,45 AnswerRuns.of({46 start: (requestId) =>47 Effect.gen(function* () {48 const params = yield* encodeParams({ project, requestId });4950 yield* Effect.tryPromise(() =>51 workflow.create({ id: requestId, params })52 ).pipe(53 // The id is taken: the run was started before.54 Effect.catch((error) =>55 Effect.tryPromise(() => workflow.get(requestId)).pipe(56 Effect.mapError(() => error),57 Effect.flatMap(runAgain)58 )59 )60 );61 }).pipe(Effect.orDie),62 })63 );