project-shell.ts
This file
Path
apps/platform/src/project-shell.ts
172 lines · 5.0 KiB · TypeScript
Last changed by
Merge observability into mainoscabrielbefore adoption1985111
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173import { EvidenceStore } from "@repo/checks/evidence-store";import { projectService } from "@repo/project/project";import type { ProjectApi, ProjectPorts } from "@repo/project/project";import { ProjectStore } from "@repo/project/project-store";import { ProjectStoreSqlite } from "@repo/project/project-store/sqlite";import type { ProjectName } from "@repo/protocol/ids";import { UnknownProject } from "@repo/protocol/refusal";import { withInvocation } from "@repo/telemetry/spans";import { Context, Effect, ErrorReporter, Exit, Layer, Option, Ref, Schema, SynchronizedRef,} from "effect";import type { Scope } from "effect";import type { SqlClient } from "effect/sql/SqlClient";
import { CallRequest, ProjectCalls } from "./project-calls.ts";import type { ArgsOf, ExitEncoded, ProjectMethod } from "./project-calls.ts";
export interface ProjectShell { readonly call: ( request: typeof CallRequest.Encoded ) => Effect.Effect<ExitEncoded>; readonly alarm: () => Effect.Effect<void>;}
type Methods = { readonly [M in ProjectMethod]: ( ...args: Readonly<ArgsOf<M>> ) => Effect.Effect< Effect.Success<ReturnType<ProjectApi[M]>>, Effect.Error<ReturnType<ProjectApi[M]>>, EvidenceStore >;};
const nameless = Effect.die( "the Project DO has no name: the Worker reaches it by `idFromName`");
export const projectShell = (input: { readonly name: ProjectName | undefined; readonly sql: Layer.Layer<SqlClient>; readonly ports: Layer.Layer< Exclude<ProjectPorts, ProjectStore> | EvidenceStore >;}): Effect.Effect<ProjectShell, never, Scope.Scope> => Effect.gen(function* () { const context = yield* Layer.build( Layer.merge( // Fresh: one store per shell, never one an enclosing layer made. Layer.fresh(ProjectStoreSqlite.pipe(Layer.provide(input.sql))), input.ports ) );
const store = Context.get(context, ProjectStore);
const withEvidence = Effect.provideService( EvidenceStore, Context.get(context, EvidenceStore) );
const made = yield* Ref.make(false); const built = yield* SynchronizedRef.make(Option.none<Methods>());
const isMade = Effect.gen(function* () { if (yield* Ref.get(made)) { return true; }
const { project } = yield* store.load();
if (Option.isSome(project)) { yield* Ref.set(made, true); }
return Option.isSome(project); });
const nameOf = Effect.gen(function* () { if (input.name !== undefined) { return Option.some(input.name); }
const { project } = yield* store.load();
return Option.map(project, (row) => row.project); });
const service = SynchronizedRef.modifyEffect(built, (current) => Option.match(current, { onNone: () => Effect.gen(function* () { const name = yield* nameOf;
if (Option.isNone(name)) { return yield* nameless; }
const api: Methods = yield* projectService(name.value).pipe( Effect.provideContext(context) );
return [api, Option.some(api)] as const; }), onSome: (api) => Effect.succeed([api, current] as const), }) );
const serve = <M extends ProjectMethod>( method: M, request: typeof CallRequest.Encoded & { readonly method: M } ) => Effect.gen(function* () { const call = ProjectCalls[method];
const args = yield* Schema.decodeUnknownEffect(call.payload)( call.caller ? [request.caller, ...request.payload] : request.payload ).pipe(Effect.orDie);
if (call.refusesUnmade && !(yield* isMade)) { const name = yield* nameOf;
if (Option.isNone(name)) { return yield* nameless; }
return yield* Schema.encodeEffect(call.exit)( Exit.fail(new UnknownProject({ project: name.value })) ).pipe(Effect.orDie); }
const api = yield* service; const outcome = yield* Effect.exit(withEvidence(api[method](...args)));
// The reply carries a defect without its stack, and the caller reports it no // more: it is reported here, where the stack is. if (Exit.isFailure(outcome)) { yield* ErrorReporter.report(outcome.cause); }
return yield* Schema.encodeUnknownEffect(call.exit)(outcome).pipe( Effect.orDie ); }).pipe(Effect.withSpan(`Project.${method}`));
const named = withInvocation( input.name === undefined ? {} : { project: input.name } );
return { alarm: Effect.fn("Project.alarm")(function* () { if (yield* isMade) { yield* Effect.flatMap(service, (api) => withEvidence(api.onAlarm()) ).pipe(named); } }), call: (request) => Effect.flatMap( Schema.decodeEffect(CallRequest)(request).pipe(Effect.orDie), ({ method }) => named(serve(method, request)) ), }; });