trying to fix startup

This commit is contained in:
Boki 2025-06-10 14:48:46 -04:00
parent 00b21a57d7
commit 32d0eaac2d
7 changed files with 107 additions and 25 deletions

View file

@ -16,6 +16,7 @@ export class RedisConnectionManager {
private static sharedConnections = new Map<string, Redis>();
private static instance: RedisConnectionManager;
private logger = getLogger('redis-connection-manager');
private static readyConnections = new Set<string>();
// Singleton pattern for the manager itself
static getInstance(): RedisConnectionManager {
@ -189,6 +190,85 @@ export class RedisConnectionManager {
return { healthy: allHealthy, details };
}
/**
* Wait for all created connections to be ready
* @param timeout Maximum time to wait in milliseconds
* @returns Promise that resolves when all connections are ready
*/
static async waitForAllConnections(timeout: number = 30000): Promise<void> {
const instance = this.getInstance();
const allConnections = new Map([
...instance.connections,
...this.sharedConnections
]);
if (allConnections.size === 0) {
instance.logger.info('No Redis connections to wait for');
return;
}
instance.logger.info(`Waiting for ${allConnections.size} Redis connections to be ready...`);
const connectionPromises = Array.from(allConnections.entries()).map(([name, redis]) =>
instance.waitForConnection(redis, name, timeout)
);
try {
await Promise.all(connectionPromises);
instance.logger.info('All Redis connections are ready');
} catch (error) {
instance.logger.error('Failed to establish all Redis connections:', error);
throw error;
}
}
/**
* Wait for a specific connection to be ready
*/
private async waitForConnection(redis: Redis, name: string, timeout: number): Promise<void> {
return new Promise((resolve, reject) => {
const timeoutId = setTimeout(() => {
reject(new Error(`Redis connection ${name} failed to be ready within ${timeout}ms`));
}, timeout);
const onReady = () => {
clearTimeout(timeoutId);
RedisConnectionManager.readyConnections.add(name);
this.logger.info(`Redis connection ready: ${name}`);
resolve();
};
const onError = (err: Error) => {
clearTimeout(timeoutId);
this.logger.error(`Redis connection failed for ${name}:`, err);
reject(err);
};
if (redis.status === 'ready') {
onReady();
} else {
redis.once('ready', onReady);
redis.once('error', onError);
}
});
}
/**
* Check if all connections are ready
*/
static areAllConnectionsReady(): boolean {
const instance = this.getInstance();
const allConnections = new Map([
...instance.connections,
...this.sharedConnections
]);
return allConnections.size > 0 &&
Array.from(allConnections.keys()).every(name =>
this.readyConnections.has(name)
);
}
}
export default RedisConnectionManager;