From 84f187d8bcef9fa633a3d9605d23802714a2bcd4 Mon Sep 17 00:00:00 2001 From: Daniel Haarhoff Date: Wed, 4 Sep 2024 10:51:19 +0100 Subject: [PATCH] Separate lifecycle of Redis from lifecycle of Express Refs: #1834 --- src/index.ts | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/index.ts b/src/index.ts index f848a9f16..d73c1c4fe 100644 --- a/src/index.ts +++ b/src/index.ts @@ -49,18 +49,22 @@ const expressServerLifecycle = Effect.acquireRelease( L.debug('Server listening')(loggerEnv)() return listeningHttpServer }), - server => - Effect.promise(async () => { - L.debug('Shutting server down')(loggerEnv)() - server.close() + server => { + L.debug('Shutting server down')(loggerEnv)() + server.close() + return Effect.void + }, +) - await redis - .quit() - .then(() => L.debug('Redis disconnected')(loggerEnv)()) - .catch((error: unknown) => - L.warnP('Redis unable to disconnect')({ error: E.toError(error).message })(loggerEnv)(), - ) - }), +const redisLifecycle = Effect.acquireRelease(Effect.succeed(redis), redis => + Effect.promise(async () => { + await redis + .quit() + .then(() => L.debug('Redis disconnected')(loggerEnv)()) + .catch((error: unknown) => + L.warnP('Redis unable to disconnect')({ error: E.toError(error).message })(loggerEnv)(), + ) + }), ) pipe( @@ -68,6 +72,7 @@ pipe( Layer.scopedDiscard, Layer.launch, Effect.provideServiceEffect(Express, expressServer), - Effect.provideServiceEffect(Redis, Effect.succeed(redis)), + Effect.provideServiceEffect(Redis, redisLifecycle), + Effect.scoped, NodeRuntime.runMain, )