Skip to content

Commit

Permalink
Get ready to remove duplicate loading of env vars
Browse files Browse the repository at this point in the history
Refs: #1834
  • Loading branch information
erkannt committed Sep 4, 2024
1 parent aa8bebf commit 004557e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
42 changes: 27 additions & 15 deletions src/Redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)())
Expand All @@ -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)(),
),
)
}),
)
3 changes: 3 additions & 0 deletions src/env.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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')<DeprecatedEnvVars, D.TypeOf<typeof EnvD>>() {}

export function decodeEnv(process: NodeJS.Process) {
return pipe(
process.env,
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -54,6 +54,7 @@ pipe(
Layer.launch,
Effect.provideServiceEffect(Express, expressServer),
Effect.provideServiceEffect(Redis, redisLifecycle),
Effect.provideService(DeprecatedEnvVars, env),
Effect.scoped,
NodeRuntime.runMain,
)

0 comments on commit 004557e

Please sign in to comment.