From c734567d046ede744154f7eff20077d88ec5d259 Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Wed, 4 Sep 2024 11:14:11 +0100 Subject: [PATCH] Remove duplication Refs #1834 --- src/Redis.ts | 23 +++++------------------ src/index.ts | 3 ++- 2 files changed, 7 insertions(+), 19 deletions(-) diff --git a/src/Redis.ts b/src/Redis.ts index ad6c01750..2482322b7 100644 --- a/src/Redis.ts +++ b/src/Redis.ts @@ -1,21 +1,15 @@ -import { SystemClock } from 'clock-ts' -import { Effect, pipe } from 'effect' -import * as C from 'fp-ts/lib/Console.js' +import { Context, Effect } from 'effect' import { toError } from 'fp-ts/lib/Either.js' import { Redis as IoRedis } from 'ioredis' import * as L from 'logger-fp-ts' import { DeprecatedEnvVars } from './env.js' +export class DeprecatedLoggerEnv extends Context.Tag('DeprecatedLoggerEnv')() {} + export const redisLifecycle = Effect.acquireRelease( 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 loggerEnv = yield* DeprecatedLoggerEnv const redis = new IoRedis(env.REDIS_URI.href, { commandTimeout: 2 * 1000, enableAutoPipelining: true }) redis.on('connect', () => L.debug('Redis connected')(loggerEnv)()) @@ -28,14 +22,7 @@ export const redisLifecycle = Effect.acquireRelease( }), redis => 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 loggerEnv = yield* DeprecatedLoggerEnv yield* Effect.promise(() => redis .quit() diff --git a/src/index.ts b/src/index.ts index 80454c704..425fdd580 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,7 +11,7 @@ import * as L from 'logger-fp-ts' import type { app } from './app.js' import { decodeEnv, DeprecatedEnvVars } from './env.js' import { expressServer, Redis } from './ExpressServer.js' -import { redisLifecycle } from './Redis.js' +import { DeprecatedLoggerEnv, redisLifecycle } from './Redis.js' const env = decodeEnv(process)() @@ -54,6 +54,7 @@ pipe( Layer.launch, Effect.provideServiceEffect(Express, expressServer), Effect.provideServiceEffect(Redis, redisLifecycle), + Effect.provideService(DeprecatedLoggerEnv, loggerEnv), Effect.provideService(DeprecatedEnvVars, env), Effect.scoped, NodeRuntime.runMain,