switched to new config and removed old
This commit is contained in:
parent
6b69bcbcaa
commit
269364fbc8
70 changed files with 889 additions and 2978 deletions
112
libs/queue/src/queue-factory.ts
Normal file
112
libs/queue/src/queue-factory.ts
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
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 providers)
|
||||
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,
|
||||
providers: [], // 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');
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue