Fix hanging IORedis connections; add extollo.wontstop debugging helper

This commit is contained in:
2022-03-29 02:30:48 -05:00
parent 8f08b94f74
commit cdecb7e628
5 changed files with 43 additions and 11 deletions

View File

@@ -8,13 +8,14 @@ import {
StandardLogger,
universalPath,
UniversalPath,
FileLogger,
ifDebugging,
} from '../util'
import {Logging} from '../service/Logging'
import {RunLevelErrorHandler} from './RunLevelErrorHandler'
import {Unit, UnitStatus} from './Unit'
import * as dotenv from 'dotenv'
import {CacheFactory} from '../support/cache/CacheFactory'
import {FileLogger} from '../util/logging/FileLogger'
/**
* Helper function that resolves and infers environment variable values.
@@ -260,6 +261,13 @@ export class Application extends Container {
async run(): Promise<void> {
try {
await this.up()
ifDebugging('extollo.wontstop', () => {
setTimeout(() => {
import('wtfnode').then(wtf => wtf.dump())
}, 1000)
})
await this.down()
} catch (e: unknown) {
if ( e instanceof Error ) {

View File

@@ -121,7 +121,8 @@ export class RedisBus implements EventBus {
}
down(): Awaitable<void> {
this.subscriberConnection?.disconnect()
this.publisherConnection?.disconnect()
// The Redis service will clean up the connections when the framework exits,
// so we don't need to do anything here.
return undefined
}
}

View File

@@ -4,7 +4,7 @@ import * as IORedis from 'ioredis'
import {RedisOptions} from 'ioredis'
import {Logging} from '../../service/Logging'
import {Unit} from '../../lifecycle/Unit'
import {AsyncPipe} from '../../util'
import {AsyncPipe, Collection} from '../../util'
export {RedisOptions} from 'ioredis'
@@ -28,19 +28,25 @@ export class Redis extends Unit {
*/
private connection?: IORedis.Redis
/**
* Collection of all Redis connections opened by this service.
* We keep track of these here so we can make sure -all- of them get closed
* when the framework tries to shut down.
* @private
*/
private spawnedConnections: Collection<IORedis.Redis> = new Collection()
async up(): Promise<void> {
this.logging.info('Attempting initial connection to Redis...')
this.logging.debug('Config:')
this.logging.debug(Config)
this.logging.debug(this.config)
await this.getConnection()
}
async down(): Promise<void> {
this.logging.info('Disconnecting Redis...')
if ( this.connection?.status === 'ready' ) {
await this.connection.disconnect()
}
await this.spawnedConnections
.where('status', '=', 'ready')
.awaitMapCall('disconnect')
}
/**
@@ -59,7 +65,9 @@ export class Redis extends Unit {
*/
public async getNewConnection(): Promise<IORedis.Redis> {
const options = this.config.get('redis.connection') as RedisOptions
return new IORedis(options)
const inst = new IORedis(options)
this.spawnedConnections.push(inst)
return inst
}
/**