Skip to content

Commit

Permalink
refactor: add debug logs
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jul 28, 2023
1 parent 8512c87 commit dc1c6da
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/connections/redis_cluster_connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import Redis, { type Cluster, type NodeRole } from 'ioredis'

import debug from '../debug.js'
import { ioMethods } from './io_methods.js'
import { AbstractConnection } from './abstract_connection.js'
import type { IORedisCommands, RedisClusterConnectionConfig } from '../types/main.js'
Expand All @@ -22,6 +23,7 @@ export class RedisClusterConnection extends AbstractConnection<Cluster> {
#config: RedisClusterConnectionConfig

constructor(connectionName: string, config: RedisClusterConnectionConfig) {
debug('creating cluster connection %s: %O', connectionName, config)
super(connectionName)

this.#config = config
Expand All @@ -37,6 +39,7 @@ export class RedisClusterConnection extends AbstractConnection<Cluster> {
* invoke this method when first subscription is created.
*/
protected makeSubscriberConnection() {
debug('creating subscriber connection')
this.ioSubscriberConnection = new Redis.Cluster(
this.#config.clusters as [],
this.#config.clusterOptions
Expand Down
3 changes: 3 additions & 0 deletions src/connections/redis_connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import { Redis, RedisOptions } from 'ioredis'

import debug from '../debug.js'
import { ioMethods } from './io_methods.js'
import { AbstractConnection } from './abstract_connection.js'
import { IORedisCommands, RedisConnectionConfig } from '../types/main.js'
Expand All @@ -23,6 +24,7 @@ export class RedisConnection extends AbstractConnection<Redis> {
#config: RedisOptions

constructor(connectionName: string, config: RedisConnectionConfig) {
debug('creating connection %s: %O', connectionName, config)
super(connectionName)
this.#config = this.#normalizeConfig(config)

Expand All @@ -45,6 +47,7 @@ export class RedisConnection extends AbstractConnection<Redis> {
* invoke this method when first subscription is created.
*/
protected makeSubscriberConnection() {
debug('creating subscriber connection')
this.ioSubscriberConnection = new Redis(this.#config)
this.monitorSubscriberConnection()
}
Expand Down
12 changes: 12 additions & 0 deletions src/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* @adonisjs/redis
*
* (c) AdonisJS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import { debuglog } from 'node:util'

export default debuglog('adonisjs:redis')
6 changes: 6 additions & 0 deletions src/redis_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
*/

import { RuntimeException } from '@poppinss/utils'

import debug from './debug.js'
import RedisConnection from './connections/redis_connection.js'
import RedisClusterConnection from './connections/redis_cluster_connection.js'
import type { GetConnectionType, RedisConnectionsList } from './types/main.js'
Expand Down Expand Up @@ -53,11 +55,13 @@ export default class RedisManager<ConnectionsList extends RedisConnectionsList>
connectionName?: ConnectionName
): GetConnectionType<ConnectionsList, ConnectionName> {
const name = connectionName || this.#config.connection
debug('resolving connection %s', name)

/**
* Return existing connection if already exists
*/
if (this.activeConnections[name]) {
debug('reusing existing connection %s', name)
return this.activeConnections[name] as GetConnectionType<ConnectionsList, ConnectionName>
}

Expand All @@ -72,6 +76,7 @@ export default class RedisManager<ConnectionsList extends RedisConnectionsList>
/**
* Instantiate the connection based upon the config
*/
debug('creating new connection %s', name)
const connection =
'clusters' in config
? new RedisClusterConnection(name as string, config)
Expand All @@ -81,6 +86,7 @@ export default class RedisManager<ConnectionsList extends RedisConnectionsList>
* Remove connection from the list of tracked connections
*/
connection.on('end', ($connection) => {
debug('%s connection closed. Removing from tracked connections list', name)
delete this.activeConnections[$connection.connectionName]
})

Expand Down

0 comments on commit dc1c6da

Please sign in to comment.