stock-bot/libs/queue/src/queue-factory.ts
2025-06-19 07:20:14 -04:00

112 lines
No EOL
2.9 KiB
TypeScript

import { getLogger } from '@stock-bot/logger';
import { QueueManager } from './queue-manager';
import { Queue } from './queue-instance';
import type { ProcessOptions, BatchResult } from './types';
const logger = getLogger('queue-factory');
// Global queue manager (manages workers and handlers)
let queueManager: QueueManager | null = null;
// Registry of individual queues
const queues = new Map<string, Queue>();
let globalRedisConfig: any = null;
/**
* Initialize the queue system with global configuration
*/
export async function initializeQueueSystem(config: {
redis: any;
defaultJobOptions?: any;
workers?: number;
concurrency?: number;
}): Promise<void> {
logger.info('Initializing global queue system...');
globalRedisConfig = config.redis;
// Initialize the global queue manager for worker management
queueManager = new QueueManager({
queueName: 'system-queue-manager',
redis: globalRedisConfig,
workers: config.workers || 5,
concurrency: config.concurrency || 20,
defaultJobOptions: config.defaultJobOptions,
handlers: [], // Will be set by individual services
});
await queueManager.initialize();
logger.info('Queue system initialized');
}
/**
* Get or create a queue for the given queue name
*/
export function getQueue(queueName: string): Queue {
if (!globalRedisConfig) {
throw new Error('Queue system not initialized. Call initializeQueueSystem() first.');
}
if (!queues.has(queueName)) {
logger.info(`Creating new queue: ${queueName}`);
const queue = new Queue(queueName, globalRedisConfig);
queues.set(queueName, queue);
}
return queues.get(queueName)!;
}
/**
* Process items using the specified queue
*/
export async function processItemsWithQueue<T>(
queueName: string,
items: T[],
options: ProcessOptions
): Promise<BatchResult> {
const queue = getQueue(queueName);
return queue.processItems(items, options);
}
/**
* Get all active queue names
*/
export function getActiveQueueNames(): string[] {
return Array.from(queues.keys());
}
/**
* Get the global queue manager (for advanced operations)
*/
export function getQueueManager(): QueueManager {
if (!queueManager) {
throw new Error('Queue system not initialized. Call initializeQueueSystem() first.');
}
return queueManager;
}
/**
* Shutdown all queues and the queue manager
*/
export async function shutdownAllQueues(): Promise<void> {
logger.info('Shutting down all queues...');
// Shutdown individual queues
const queueShutdownPromises = Array.from(queues.values()).map(queue =>
queue.shutdown().catch(error => {
logger.error('Error shutting down queue', { error });
})
);
await Promise.all(queueShutdownPromises);
queues.clear();
// Shutdown the global queue manager
if (queueManager) {
await queueManager.shutdown();
queueManager = null;
}
logger.info('All queues shut down');
}