Skip to content

Commit

Permalink
Help us see what's happening with the Redis connection
Browse files Browse the repository at this point in the history
  • Loading branch information
thewilkybarkid committed Dec 15, 2023
1 parent 991e54d commit da1db61
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
17 changes: 15 additions & 2 deletions src/Redis.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context, Data, Effect, Layer } from 'effect'
import { Context, Data, Effect, Layer, Runtime } from 'effect'
import IoRedis from 'ioredis'

export type Redis = IoRedis.Redis
Expand All @@ -21,8 +21,21 @@ export const layer: Layer.Layer<RedisConfig, never, Redis> = Layer.scoped(
Effect.acquireRelease(
Effect.gen(function* (_) {
const config = yield* _(RedisConfig)
const runtime = yield* _(Effect.runtime<never>())

return new IoRedis.Redis(config.url.href, { family: config.family })
const redis = new IoRedis.Redis(config.url.href, { family: config.family })

redis.on('connect', () => Runtime.runSync(runtime)(Effect.logDebug('Redis connected')))
redis.on('close', () => Runtime.runSync(runtime)(Effect.logDebug('Redis connection closed')))
redis.on('reconnecting', () => Runtime.runSync(runtime)(Effect.logInfo('Redis reconnecting')))
redis.removeAllListeners('error')
redis.on('error', (error: Error) =>
Runtime.runSync(runtime)(
Effect.logError('Redis connection error').pipe(Effect.annotateLogs({ error: error.message })),
),
)

return redis
}),
redis => Effect.sync(() => redis.disconnect()),
),
Expand Down
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HttpClient, HttpServer, Runtime } from '@effect/platform-node'
import { Schema, TreeFormatter } from '@effect/schema'
import { Effect, Layer } from 'effect'
import { Effect, Layer, LogLevel, Logger } from 'effect'
import { createServer } from 'node:http'
import * as CoarNotify from './CoarNotify.js'
import { ConfigLive } from './Config.js'
Expand Down Expand Up @@ -127,4 +127,8 @@ const HttpLive = Layer.scopedDiscard(serve).pipe(
Layer.provide(ConfigLive),
)

Layer.launch(HttpLive).pipe(Effect.tapErrorCause(Effect.logError), Runtime.runMain)
Layer.launch(HttpLive).pipe(
Effect.tapErrorCause(Effect.logError),
Logger.withMinimumLogLevel(LogLevel.Debug),
Runtime.runMain,
)

0 comments on commit da1db61

Please sign in to comment.