Skip to content

Commit

Permalink
Separate lifecycle of Redis from lifecycle of Express
Browse files Browse the repository at this point in the history
Refs: #1834
  • Loading branch information
erkannt committed Sep 4, 2024
1 parent 26b47df commit 84f187d
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,30 @@ 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(
expressServerLifecycle,
Layer.scopedDiscard,
Layer.launch,
Effect.provideServiceEffect(Express, expressServer),
Effect.provideServiceEffect(Redis, Effect.succeed(redis)),
Effect.provideServiceEffect(Redis, redisLifecycle),
Effect.scoped,
NodeRuntime.runMain,
)

0 comments on commit 84f187d

Please sign in to comment.