removed config from queue, will be injected
This commit is contained in:
parent
0d8119e3de
commit
e924c8b6bc
2 changed files with 65 additions and 357 deletions
|
|
@ -25,9 +25,11 @@ export class QueueManager {
|
|||
private rateLimiter?: QueueRateLimiter;
|
||||
private redisConnection: ReturnType<typeof getRedisConnection>;
|
||||
private isShuttingDown = false;
|
||||
private isInitialized = false;
|
||||
private shutdownPromise: Promise<void> | null = null;
|
||||
private config: QueueManagerConfig;
|
||||
|
||||
private constructor(private config: QueueManagerConfig) {
|
||||
private constructor(config: QueueManagerConfig) {
|
||||
this.config = config;
|
||||
this.redisConnection = getRedisConnection(config.redis);
|
||||
|
||||
// Initialize rate limiter if rules are provided
|
||||
|
|
@ -38,25 +40,27 @@ export class QueueManager {
|
|||
});
|
||||
}
|
||||
|
||||
this.isInitialized = true;
|
||||
logger.info('QueueManager singleton initialized');
|
||||
logger.info('QueueManager singleton initialized', {
|
||||
redis: `${config.redis.host}:${config.redis.port}`,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the singleton instance
|
||||
* @throws Error if not initialized - use initialize() first
|
||||
*/
|
||||
static getInstance(config?: QueueManagerConfig): QueueManager {
|
||||
static getInstance(): QueueManager {
|
||||
if (!QueueManager.instance) {
|
||||
if (!config) {
|
||||
throw new Error('QueueManager not initialized. Provide config on first call.');
|
||||
}
|
||||
QueueManager.instance = new QueueManager(config);
|
||||
throw new Error(
|
||||
'QueueManager not initialized. Call QueueManager.initialize(config) first.'
|
||||
);
|
||||
}
|
||||
return QueueManager.instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the singleton with config
|
||||
* Must be called before getInstance()
|
||||
*/
|
||||
static initialize(config: QueueManagerConfig): QueueManager {
|
||||
if (QueueManager.instance) {
|
||||
|
|
@ -67,6 +71,32 @@ export class QueueManager {
|
|||
return QueueManager.instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get or initialize the singleton
|
||||
* Convenience method that combines initialize and getInstance
|
||||
*/
|
||||
static getOrInitialize(config?: QueueManagerConfig): QueueManager {
|
||||
if (QueueManager.instance) {
|
||||
return QueueManager.instance;
|
||||
}
|
||||
|
||||
if (!config) {
|
||||
throw new Error(
|
||||
'QueueManager not initialized and no config provided. ' +
|
||||
'Either call initialize(config) first or provide config to getOrInitialize(config).'
|
||||
);
|
||||
}
|
||||
|
||||
return QueueManager.initialize(config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the QueueManager is initialized
|
||||
*/
|
||||
static isInitialized(): boolean {
|
||||
return QueueManager.instance !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the singleton (mainly for testing)
|
||||
*/
|
||||
|
|
@ -327,9 +357,14 @@ export class QueueManager {
|
|||
|
||||
|
||||
/**
|
||||
* Shutdown all queues and workers
|
||||
* Shutdown all queues and workers (thread-safe)
|
||||
*/
|
||||
async shutdown(): Promise<void> {
|
||||
// If already shutting down, return the existing promise
|
||||
if (this.shutdownPromise) {
|
||||
return this.shutdownPromise;
|
||||
}
|
||||
|
||||
if (this.isShuttingDown) {
|
||||
return;
|
||||
}
|
||||
|
|
@ -337,6 +372,15 @@ export class QueueManager {
|
|||
this.isShuttingDown = true;
|
||||
logger.info('Shutting down QueueManager...');
|
||||
|
||||
// Create shutdown promise
|
||||
this.shutdownPromise = this.performShutdown();
|
||||
return this.shutdownPromise;
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform the actual shutdown
|
||||
*/
|
||||
private async performShutdown(): Promise<void> {
|
||||
try {
|
||||
// Close all queues (this now includes workers since they're managed by Queue class)
|
||||
const queueShutdownPromises = Array.from(this.queues.values()).map(async (queue) => {
|
||||
|
|
@ -375,6 +419,10 @@ export class QueueManager {
|
|||
} catch (error) {
|
||||
logger.error('Error during shutdown', { error: error.message });
|
||||
throw error;
|
||||
} finally {
|
||||
// Reset shutdown state
|
||||
this.shutdownPromise = null;
|
||||
this.isShuttingDown = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -392,4 +440,11 @@ export class QueueManager {
|
|||
getRedisConfig() {
|
||||
return this.config.redis;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current configuration
|
||||
*/
|
||||
getConfig(): Readonly<QueueManagerConfig> {
|
||||
return { ...this.config };
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue