Skip to content

Commit

Permalink
fix: Add optional debug output to redis pubsub
Browse files Browse the repository at this point in the history
  • Loading branch information
parzival418 committed Jun 28, 2024
1 parent fc1584e commit 508a76b
Showing 1 changed file with 35 additions and 28 deletions.
63 changes: 35 additions & 28 deletions packages/server/redis-pubsub/src/lib/redis-pubsub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ function stringify(obj: Record<string, any>) {
return str
}

export type PubsubOptions = {
debug?: boolean
}

/**
* A class for managing Redis Publish/Subscribe operations.
*
Expand Down Expand Up @@ -52,6 +56,7 @@ export class RedisPubSub extends EventEmitter {
private redisCloudUrl: string
private publisher!: Redis
private subscriber!: Redis
private options: PubsubOptions

private channelRefCount = new Map<string, number>()
private patternRefCount = new Map<string, number>()
Expand Down Expand Up @@ -123,9 +128,10 @@ export class RedisPubSub extends EventEmitter {
})
}

constructor(redisCloudUrl: string) {
constructor(redisCloudUrl: string, options: PubsubOptions = {}) {
super()
this.redisCloudUrl = redisCloudUrl
this.options = options
}

/**
Expand Down Expand Up @@ -158,8 +164,6 @@ export class RedisPubSub extends EventEmitter {
// numberOfKeys: -1,
// lua: 'return redis.call("del", unpack(KEYS))',
// })

console.log('Connecting to redis pubsub')
// await this.client.connect()
// await this.subscriber.connect()
this.connectEventListeners(this.publisher, 'publisher')
Expand All @@ -175,42 +179,45 @@ export class RedisPubSub extends EventEmitter {

// "wait" | "reconnecting" | "connecting" | "connect" | "ready" | "close" | "end";

client.on('connecting', () => {
console.log(`Connecting to Redis ${clientType}...`)
})
if (this.options.debug) {
client.on('connecting', () => {
console.log(`Connecting to Redis ${clientType}...`)
})

client.on('connect', () => {
console.log(`Redis ${clientType} connected successfully.`)
})
client.on('connect', () => {
console.log(`Redis ${clientType} connected successfully.`)
})

client.on('close', () => {
console.log(`Redis ${clientType} connection closed.`)
})
client.on('close', () => {
console.log(`Redis ${clientType} connection closed.`)
})

client.on('reconnecting', () => {
console.warn(`Reconnecting to Redis ${clientType}...`)
})
client.on('reconnecting', () => {
console.warn(`Reconnecting to Redis ${clientType}...`)
})

client.on('end', () => {
console.log(`Redis ${clientType} connection ended.`)
})
client.on('end', () => {
console.log(`Redis ${clientType} connection ended.`)
})

client.on('ready', () => {
console.log(`Redis ${clientType} is ready.`)
})
client.on('ready', () => {
console.log(`Redis ${clientType} is ready.`)
})

client.on('reconnecting', () => {
console.log(`Redis ${clientType} reconnecing.`)
})
client.on('reconnecting', () => {
console.log(`Redis ${clientType} reconnecing.`)
})

client.on('wait', (time: string) => {
console.log(`Redis ${clientType} is waiting:`, time)
})
client.on('wait', (time: string) => {
console.log(`Redis ${clientType} is waiting:`, time)
})
}

// handle subscriber reconnection

if (clientType === 'subscriber') {
console.log('Setting up subscriber event listeners...')
if (this.options.debug)
console.log('Setting up subscriber event listeners...')
client.on('message', (channel, message) => {
this.emit(channel, message)
})
Expand Down

0 comments on commit 508a76b

Please sign in to comment.