Skip to content

Commit

Permalink
print more debug output
Browse files Browse the repository at this point in the history
  • Loading branch information
aadityabhatia committed Aug 14, 2023
1 parent 8baec4d commit 62f88d9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@corsaircoalition/common",
"version": "1.3.2",
"version": "1.3.3",
"description": "common helper modules for a modular generals.io bot framework",
"main": "out/index.js",
"type": "module",
Expand Down Expand Up @@ -28,7 +28,7 @@
"homepage": "https://corsaircoalition.github.io/",
"devDependencies": {
"@sindresorhus/tsconfig": "^4.0.0",
"@types/node": "^20.4.10",
"@types/node": "^20.5.0",
"typescript": "^5.1.6"
},
"dependencies": {
Expand Down
28 changes: 22 additions & 6 deletions src/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export default class Redis {
redisConfig.TLS = process.env['REDIS_TLS'] === "1" || process.env['REDIS_TLS'] === "true"
}

Log.debugObject('Redis config:', redisConfig)

const connectionObj = {
username: redisConfig.USERNAME,
password: redisConfig.PASSWORD,
Expand All @@ -38,7 +40,7 @@ export default class Redis {
tls: redisConfig.TLS,
servername: redisConfig.HOST,
reconnectStrategy: (retries: number) => {
if(retries > 5) {
if (retries > 5) {
return false
}
// reconnect after 50ms, 150ms, 250ms, 350ms...
Expand All @@ -50,10 +52,13 @@ export default class Redis {
const handleConnectionError = (error: Error) => {
Log.stderr(`[Redis] ${error}`)
// if connection error is not resolved within 1 second, quit
Log.debug('[Redis] Checking connection status in 1 second...')
later(1000).then(() => {
if(!(Redis.subscriber.isReady && Redis.publisher.isReady)) {
if (!(Redis.subscriber.isReady && Redis.publisher.isReady)) {
Log.stderr('[Redis] Connection issues still unresolved. Quitting.')
process.exit(3)
} else {
Log.debug('[Redis] Connection issues resolved. Moving on.')
}
})
}
Expand All @@ -68,6 +73,10 @@ export default class Redis {
Redis.subscriber.on(event, Redis.redisEventHandler(event))
}

Redis.connectionEventEmitter.on('status', (type: string, data: any) => {
Log.debug('[Redis]', type, data)
})

return Promise.all([
Redis.subscriber.connect(),
Redis.publisher.connect()
Expand All @@ -77,19 +86,23 @@ export default class Redis {
private static redisEventHandler = (type: string) => (data: any) => Redis.connectionEventEmitter.emit('status', type, data)

public static async ping() {
await Redis.publisher.ping()
Log.debug('[Redis] sending PING...')
return Redis.publisher.ping().then((response: string) => {
Log.debug('[Redis] received:', response)
return response
})
}

public static async listPush(keyspace: string, list: RedisData.LIST, data: any) {
Log.debug ('[Redis] listPush: ', keyspace + '-' + list)
Log.debug('[Redis] listPush: ', keyspace + '-' + list)
return Promise.all([
Redis.publisher.rPush(keyspace + '-' + list, JSON.stringify(data)),
Redis.publisher.expire(keyspace + '-' + list, Redis.EXPIRATION_TIME)
])
}

public static async setKeys(keyspace: string, keyValues: Record<string, any>) {
Log.debug ('[Redis] setKeys: ', keyspace)
Log.debug('[Redis] setKeys: ', keyspace)
// JSON.stringify each value
for (let key in keyValues) {
keyValues[key] = JSON.stringify(keyValues[key])
Expand All @@ -101,6 +114,7 @@ export default class Redis {
}

public static async getKeys(keyspace: string, ...keys: Array<string>) {
Log.debug('[Redis] getKeys: ', keyspace)
// JSON.parse each value
let values = await Redis.publisher.hmGet(keyspace, keys)
for (let key in values) {
Expand All @@ -110,6 +124,7 @@ export default class Redis {
}

public static async getAllKeys(keyspace: string) {
Log.debug('[Redis] getAllKeys: ', keyspace)
// JSON.parse each value
let values = await Redis.publisher.hGetAll(keyspace)
for (let key in values) {
Expand All @@ -120,7 +135,7 @@ export default class Redis {

public static publish(botId: string, channel: RedisData.CHANNEL, data: any) {
const CHANNEL_NAME: string = `${botId}-${channel}`
Log.debug ('[Redis] publish: ', CHANNEL_NAME)
Log.debug('[Redis] publish: ', CHANNEL_NAME)
return Redis.publisher.publish(CHANNEL_NAME, JSON.stringify(data))
}

Expand All @@ -144,6 +159,7 @@ export default class Redis {

public static async quit() {
let promises = []
Log.debug('[Redis] Closing Redis connections...')
if (Redis.subscriber.isReady) {
Log.stdout('Closing Redis subscriber...')
promises.push(Redis.subscriber.quit())
Expand Down

0 comments on commit 62f88d9

Please sign in to comment.