diff --git a/packages/dota/src/db/RedisClient.ts b/packages/dota/src/db/RedisClient.ts index 73d1cc321..e21c7dde2 100644 --- a/packages/dota/src/db/RedisClient.ts +++ b/packages/dota/src/db/RedisClient.ts @@ -10,6 +10,11 @@ class RedisClient { private constructor() { this.client = createClient({ url: `redis://${process.env.HOST_REDIS}:6379` }) this.subscriber = this.client.duplicate() + + // Handle process exit to close Redis connections properly + process.on('exit', this.closeConnections.bind(this)) + process.on('SIGINT', this.closeConnections.bind(this)) + process.on('SIGTERM', this.closeConnections.bind(this)) } public async connect( @@ -36,6 +41,16 @@ class RedisClient { if (!RedisClient.instance) RedisClient.instance = new RedisClient() return RedisClient.instance } + + private async closeConnections() { + try { + await this.client.quit() + await this.subscriber.quit() + logger.info('Redis connections closed successfully') + } catch (error) { + logger.error('Error closing Redis connections', { error }) + } + } } export default RedisClient diff --git a/packages/dota/src/dota/GSIServer.ts b/packages/dota/src/dota/GSIServer.ts index 45e0ca674..6cb26f3cb 100644 --- a/packages/dota/src/dota/GSIServer.ts +++ b/packages/dota/src/dota/GSIServer.ts @@ -63,6 +63,7 @@ const allowedOrigins = [ ] class GSIServer { io: Server + private inactiveTokenInterval: NodeJS.Timeout | null = null constructor() { logger.info('Starting GSI Server!') @@ -145,12 +146,19 @@ class GSIServer { // Set up the repeating timer // eslint-disable-next-line @typescript-eslint/no-misused-promises - setInterval(checkForInactiveTokens, TOKEN_TIMEOUT) + this.inactiveTokenInterval = setInterval(checkForInactiveTokens, TOKEN_TIMEOUT) } init() { return this } + + close() { + if (this.inactiveTokenInterval) { + clearInterval(this.inactiveTokenInterval) + this.inactiveTokenInterval = null + } + } } export default GSIServer diff --git a/packages/dota/src/dota/events/gsi-events/__tests__/checkCallAndMemory.ts b/packages/dota/src/dota/events/gsi-events/__tests__/checkCallAndMemory.ts index 15cd1055a..f0d7b7b95 100644 --- a/packages/dota/src/dota/events/gsi-events/__tests__/checkCallAndMemory.ts +++ b/packages/dota/src/dota/events/gsi-events/__tests__/checkCallAndMemory.ts @@ -28,5 +28,12 @@ export async function checkCallAndMemory(regexPattern: RegExp, userCount: number prevCallCount = callLength }, 1000) + + // Clear memory after tests + return () => { + clearInterval(interval) + twitchChatSpy.mockClear() + global.gc() // Trigger garbage collection if available + } }) }