From a16fbd3eaa165b3226a3b0ed9848b51718aaeafa Mon Sep 17 00:00:00 2001 From: Elizabeth Date: Mon, 2 Jun 2025 18:55:32 -0700 Subject: Fixes when the map of an either is expected to be possible undefined --- mod.ts | 2 +- server/health.ts | 13 +++++-------- u/fn/either.ts | 14 +++++++------- u/process/env.ts | 7 +++---- u/server/activity/health.ts | 12 +++++++----- u/trace/itrace.ts | 4 ++-- worker/Dockerfile | 8 ++++---- 7 files changed, 29 insertions(+), 31 deletions(-) diff --git a/mod.ts b/mod.ts index 0af8943..98470fc 100755 --- a/mod.ts +++ b/mod.ts @@ -29,7 +29,7 @@ if (import.meta.main) { eitherDone.fold(({ isLeft, value }) => { if (!isLeft) return; - console.error(value); + console.error(`Failed to start`, value); Deno.exit(1); }), ); diff --git a/server/health.ts b/server/health.ts index 1acc074..8fbc69d 100644 --- a/server/health.ts +++ b/server/health.ts @@ -17,12 +17,9 @@ export const healthCheck: HealthChecker = ( .bimap(TraceUtil.withFunctionTrace(healthCheck)) .move(getRequiredEnv("LAMINAR_HOST")) // ensure LAMINAR_HOST is propagated to getStdout for other procedures - .map((e) => e.get().moveRight(["laminarc", "show-jobs"])) - .map((i) => - i - .get() - .mapRight(i.move.apply) - .flatMapAsync(getStdout.apply) - .then((gotJobs) => gotJobs.moveRight(HealthCheckOutput.YAASSSLAYQUEEN)), - ) + .map((tEitherEnv) => tEitherEnv.get() + .flatMapAsync((_hasEnv) => + getStdout(tEitherEnv.move(["laminarc", "show-jobs"])) + )) + .map(TraceUtil.promiseify((stdout) => stdout.get().moveRight(HealthCheckOutput.YAASSSLAYQUEEN))) .get(); diff --git a/u/fn/either.ts b/u/fn/either.ts index bf90f16..54f9fc2 100644 --- a/u/fn/either.ts +++ b/u/fn/either.ts @@ -32,14 +32,13 @@ export class Either implements IEither { private readonly self: Left | Right; private constructor( - err?: E, - ok?: T, + init: { err?: E, ok?: T }, public readonly _tag: IEitherTag = iEitherTag, ) { this.self = | Right>{ - isLeft: typeof err !== "undefined", - isRight: typeof ok !== "undefined", - value: typeof err !== "undefined" ? err : ok!, + isLeft: "err" in init, + isRight: "ok" in init, + value: init.err ?? init.ok!, }; } @@ -86,10 +85,11 @@ export class Either implements IEither { } static left(e: E): IEither { - return new Either(e, undefined); + return new Either({ err: e}); } + static right(t: T): IEither { - return new Either(undefined, t); + return new Either({ok: t}); } static fromFailable(s: Supplier): IEither { diff --git a/u/process/env.ts b/u/process/env.ts index c0d893c..470cb39 100644 --- a/u/process/env.ts +++ b/u/process/env.ts @@ -1,11 +1,10 @@ import { Either, type IEither } from "@emprespresso/pengueno"; export const getRequiredEnv = (name: V): IEither => - Either.fromFailable(() => Deno.env.get(name) as V) // could throw when no permission. + Either.fromFailable(() => Deno.env.get(name) as V | undefined) // could throw when no permission. .flatMap( - (v) => - (v && Either.right(v)) || - Either.left(new Error(`environment variable "${name}" is required D:`)), + (v) => (v && Either.right(v)) || + Either.left(new Error(`environment variable "${name}" is required D:`)) ); type ObjectFromList, V = string> = { diff --git a/u/server/activity/health.ts b/u/server/activity/health.ts index b9dedf9..3b4a23a 100644 --- a/u/server/activity/health.ts +++ b/u/server/activity/health.ts @@ -35,16 +35,18 @@ export class HealthCheckActivityImpl implements IHealthCheckActivity { return req .bimap(TraceUtil.withFunctionTrace(this.checkHealth)) .bimap(TraceUtil.withMetricTrace(healthCheckMetric)) - .flatMap((r) => r.move(HealthCheckInput.CHECK).map(this.check)) + .flatMap((r) => + r.move(HealthCheckInput.CHECK).map((input) => this.check(input)), + ) .peek( TraceUtil.promiseify((h) => h.get().fold(({ isLeft, value }) => { - if (isLeft) { - h.trace.trace(healthCheckMetric.failure); - h.trace.addTrace(LogLevel.ERROR).trace(`${value}`); + if (!isLeft) { + h.trace.trace(healthCheckMetric.success); return; } - h.trace.trace(healthCheckMetric.success); + h.trace.trace(healthCheckMetric.failure); + h.trace.addTrace(LogLevel.ERROR).trace(`${value}`); }), ), ) diff --git a/u/trace/itrace.ts b/u/trace/itrace.ts index 35164b5..c3cb8ad 100644 --- a/u/trace/itrace.ts +++ b/u/trace/itrace.ts @@ -16,7 +16,7 @@ export type ITraceableMapper> = ( export interface ITraceable { readonly trace: ITrace; get: Supplier; - move: <_T>(u: _T) => ITraceable<_T, Trace>; + move: <_T>(t: _T) => ITraceable<_T, Trace>; map: <_T>(mapper: ITraceableMapper) => ITraceable<_T, Trace>; bimap: <_T>( mapper: ITraceableMapper< @@ -48,7 +48,7 @@ export class TraceableImpl implements ITraceable { public flatMap<_T>( mapper: ITraceableMapper, TraceWith>, ): ITraceable<_T, TraceWith> { - return mapper(this); + return mapper(this); } public flatMapAsync<_T>( diff --git a/worker/Dockerfile b/worker/Dockerfile index 2319a41..8742b44 100644 --- a/worker/Dockerfile +++ b/worker/Dockerfile @@ -1,4 +1,4 @@ -# -- -- +# -- -- FROM debian:stable-slim AS worker_dependencies # Define versions as build arguments to improve caching @@ -11,9 +11,9 @@ RUN unzip /bw-linux.zip -d / \ RUN curl -L "https://get.docker.com/builds/$(uname -s)/$(uname -m)/docker-latest.tgz" > /docker.tgz RUN tar -xvzf /docker.tgz -# -- -- +# -- -- -# -- -- +# -- -- FROM oci.liz.coffee/emprespresso/ci_base:release AS worker RUN apt-get update && apt-get install -yqq git jq @@ -36,4 +36,4 @@ HEALTHCHECK --interval=10s --retries=3 --start-period=3s \ CMD [ "/usr/bin/laminarc show-jobs" ] CMD [ "/usr/sbin/laminard" ] -# -- -- +# -- -- -- cgit v1.2.3-70-g09d2