diff --git a/src/Redis.ts b/src/Redis.ts index 956d3cf7a..ad6c01750 100644 --- a/src/Redis.ts +++ b/src/Redis.ts @@ -4,16 +4,18 @@ import * as C from 'fp-ts/lib/Console.js' import { toError } from 'fp-ts/lib/Either.js' import { Redis as IoRedis } from 'ioredis' import * as L from 'logger-fp-ts' -import { decodeEnv } from './env.js' - -const env = decodeEnv(process)() -const loggerEnv: L.LoggerEnv = { - clock: SystemClock, - logger: pipe(C.log, L.withShow(env.LOG_FORMAT === 'json' ? L.JsonShowLogEntry : L.getColoredShow(L.ShowLogEntry))), -} +import { DeprecatedEnvVars } from './env.js' export const redisLifecycle = Effect.acquireRelease( - Effect.sync(() => { + Effect.gen(function* () { + const env = yield* DeprecatedEnvVars + const loggerEnv: L.LoggerEnv = { + clock: SystemClock, + logger: pipe( + C.log, + L.withShow(env.LOG_FORMAT === 'json' ? L.JsonShowLogEntry : L.getColoredShow(L.ShowLogEntry)), + ), + } const redis = new IoRedis(env.REDIS_URI.href, { commandTimeout: 2 * 1000, enableAutoPipelining: true }) redis.on('connect', () => L.debug('Redis connected')(loggerEnv)()) @@ -25,12 +27,22 @@ export const redisLifecycle = Effect.acquireRelease( return redis }), redis => - Effect.promise(async () => { - await redis - .quit() - .then(() => L.debug('Redis disconnected')(loggerEnv)()) - .catch((error: unknown) => - L.warnP('Redis unable to disconnect')({ error: toError(error).message })(loggerEnv)(), - ) + Effect.gen(function* () { + const env = yield* DeprecatedEnvVars + const loggerEnv: L.LoggerEnv = { + clock: SystemClock, + logger: pipe( + C.log, + L.withShow(env.LOG_FORMAT === 'json' ? L.JsonShowLogEntry : L.getColoredShow(L.ShowLogEntry)), + ), + } + yield* Effect.promise(() => + redis + .quit() + .then(() => L.debug('Redis disconnected')(loggerEnv)()) + .catch((error: unknown) => + L.warnP('Redis unable to disconnect')({ error: toError(error).message })(loggerEnv)(), + ), + ) }), ) diff --git a/src/env.ts b/src/env.ts index 67a30575a..87e11be9c 100644 --- a/src/env.ts +++ b/src/env.ts @@ -1,3 +1,4 @@ +import { Context } from 'effect' import * as C from 'fp-ts/lib/Console.js' import * as E from 'fp-ts/lib/Either.js' import * as IOE from 'fp-ts/lib/IOEither.js' @@ -8,6 +9,8 @@ import { isOrcid } from 'orcid-id-ts' import { v4 } from 'uuid-ts' import { type NonEmptyString, NonEmptyStringC } from './types/string.js' +export class DeprecatedEnvVars extends Context.Tag('DeprecatedEnvVars')>() {} + export function decodeEnv(process: NodeJS.Process) { return pipe( process.env, diff --git a/src/index.ts b/src/index.ts index d04430672..80454c704 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,7 +9,7 @@ import { pipe } from 'fp-ts/lib/function.js' import type { JsonRecord } from 'fp-ts/lib/Json.js' import * as L from 'logger-fp-ts' import type { app } from './app.js' -import { decodeEnv } from './env.js' +import { decodeEnv, DeprecatedEnvVars } from './env.js' import { expressServer, Redis } from './ExpressServer.js' import { redisLifecycle } from './Redis.js' @@ -54,6 +54,7 @@ pipe( Layer.launch, Effect.provideServiceEffect(Express, expressServer), Effect.provideServiceEffect(Redis, redisLifecycle), + Effect.provideService(DeprecatedEnvVars, env), Effect.scoped, NodeRuntime.runMain, )