fixed cache keys
This commit is contained in:
parent
db3aa9c330
commit
19dfda2392
13 changed files with 286 additions and 221 deletions
|
|
@ -1,6 +1,5 @@
|
|||
import { createCache } from '@stock-bot/cache';
|
||||
import type { CacheProvider } from '@stock-bot/cache';
|
||||
import { getLogger } from '@stock-bot/logger';
|
||||
import { Queue, type QueueWorkerConfig } from './queue';
|
||||
import { QueueRateLimiter } from './rate-limiter';
|
||||
import type {
|
||||
|
|
@ -12,8 +11,6 @@ import type {
|
|||
} from './types';
|
||||
import { getRedisConnection } from './utils';
|
||||
|
||||
const logger = getLogger('queue-manager');
|
||||
|
||||
/**
|
||||
* QueueManager provides unified queue and cache management
|
||||
* Main entry point for all queue operations with getQueue() method
|
||||
|
|
@ -27,14 +24,16 @@ export class QueueManager {
|
|||
private isShuttingDown = false;
|
||||
private shutdownPromise: Promise<void> | null = null;
|
||||
private config: QueueManagerConfig;
|
||||
private readonly logger: any;
|
||||
|
||||
constructor(config: QueueManagerConfig) {
|
||||
constructor(config: QueueManagerConfig, logger?: any) {
|
||||
this.config = config;
|
||||
this.logger = logger || console;
|
||||
this.redisConnection = getRedisConnection(config.redis);
|
||||
|
||||
// Initialize rate limiter if rules are provided
|
||||
if (config.rateLimitRules && config.rateLimitRules.length > 0) {
|
||||
this.rateLimiter = new QueueRateLimiter(this.redisConnection);
|
||||
this.rateLimiter = new QueueRateLimiter(this.redisConnection, this.logger);
|
||||
config.rateLimitRules.forEach(rule => {
|
||||
if (this.rateLimiter) {
|
||||
this.rateLimiter.addRule(rule);
|
||||
|
|
@ -42,7 +41,7 @@ export class QueueManager {
|
|||
});
|
||||
}
|
||||
|
||||
logger.info('QueueManager initialized', {
|
||||
this.logger.info('QueueManager initialized', {
|
||||
redis: `${config.redis.host}:${config.redis.port}`,
|
||||
});
|
||||
}
|
||||
|
|
@ -53,7 +52,7 @@ export class QueueManager {
|
|||
* @throws Error if not initialized - use initialize() first
|
||||
*/
|
||||
static getInstance(): QueueManager {
|
||||
logger.warn(
|
||||
console.warn(
|
||||
'QueueManager.getInstance() is deprecated. Please use dependency injection instead.'
|
||||
);
|
||||
if (!QueueManager.instance) {
|
||||
|
|
@ -68,11 +67,11 @@ export class QueueManager {
|
|||
* Must be called before getInstance()
|
||||
*/
|
||||
static initialize(config: QueueManagerConfig): QueueManager {
|
||||
logger.warn(
|
||||
console.warn(
|
||||
'QueueManager.initialize() is deprecated. Please use dependency injection instead.'
|
||||
);
|
||||
if (QueueManager.instance) {
|
||||
logger.warn('QueueManager already initialized, returning existing instance');
|
||||
console.warn('QueueManager already initialized, returning existing instance');
|
||||
return QueueManager.instance;
|
||||
}
|
||||
QueueManager.instance = new QueueManager(config);
|
||||
|
|
@ -85,7 +84,7 @@ export class QueueManager {
|
|||
* Convenience method that combines initialize and getInstance
|
||||
*/
|
||||
static getOrInitialize(config?: QueueManagerConfig): QueueManager {
|
||||
logger.warn(
|
||||
console.warn(
|
||||
'QueueManager.getOrInitialize() is deprecated. Please use dependency injection instead.'
|
||||
);
|
||||
if (QueueManager.instance) {
|
||||
|
|
@ -152,7 +151,8 @@ export class QueueManager {
|
|||
queueName,
|
||||
this.config.redis,
|
||||
mergedOptions.defaultJobOptions || {},
|
||||
queueConfig
|
||||
queueConfig,
|
||||
this.logger
|
||||
);
|
||||
|
||||
// Store the queue
|
||||
|
|
@ -172,7 +172,7 @@ export class QueueManager {
|
|||
});
|
||||
}
|
||||
|
||||
logger.info('Queue created with batch cache', {
|
||||
this.logger.info('Queue created with batch cache', {
|
||||
queueName,
|
||||
workers: mergedOptions.workers || 0,
|
||||
concurrency: mergedOptions.concurrency || 1,
|
||||
|
|
@ -207,7 +207,7 @@ export class QueueManager {
|
|||
enableMetrics: true,
|
||||
});
|
||||
this.caches.set(queueName, cacheProvider);
|
||||
logger.trace('Cache created for queue', { queueName });
|
||||
this.logger.trace('Cache created for queue', { queueName });
|
||||
}
|
||||
const cache = this.caches.get(queueName);
|
||||
if (!cache) {
|
||||
|
|
@ -222,7 +222,7 @@ export class QueueManager {
|
|||
async initializeCache(queueName: string): Promise<void> {
|
||||
const cache = this.getCache(queueName);
|
||||
await cache.waitForReady(10000);
|
||||
logger.info('Cache initialized for queue', { queueName });
|
||||
this.logger.info('Cache initialized for queue', { queueName });
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -232,7 +232,7 @@ export class QueueManager {
|
|||
private initializeBatchCacheSync(queueName: string): void {
|
||||
// Just create the cache - it will connect automatically when first used
|
||||
this.getCache(queueName);
|
||||
logger.trace('Batch cache initialized synchronously for queue', { queueName });
|
||||
this.logger.trace('Batch cache initialized synchronously for queue', { queueName });
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -321,7 +321,7 @@ export class QueueManager {
|
|||
async pauseAll(): Promise<void> {
|
||||
const pausePromises = Array.from(this.queues.values()).map(queue => queue.pause());
|
||||
await Promise.all(pausePromises);
|
||||
logger.info('All queues paused');
|
||||
this.logger.info('All queues paused');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -330,7 +330,7 @@ export class QueueManager {
|
|||
async resumeAll(): Promise<void> {
|
||||
const resumePromises = Array.from(this.queues.values()).map(queue => queue.resume());
|
||||
await Promise.all(resumePromises);
|
||||
logger.info('All queues resumed');
|
||||
this.logger.info('All queues resumed');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -365,7 +365,7 @@ export class QueueManager {
|
|||
async drainAll(delayed = false): Promise<void> {
|
||||
const drainPromises = Array.from(this.queues.values()).map(queue => queue.drain(delayed));
|
||||
await Promise.all(drainPromises);
|
||||
logger.info('All queues drained', { delayed });
|
||||
this.logger.info('All queues drained', { delayed });
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -380,7 +380,7 @@ export class QueueManager {
|
|||
queue.clean(grace, limit, type)
|
||||
);
|
||||
await Promise.all(cleanPromises);
|
||||
logger.info('All queues cleaned', { type, grace, limit });
|
||||
this.logger.info('All queues cleaned', { type, grace, limit });
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -397,7 +397,7 @@ export class QueueManager {
|
|||
}
|
||||
|
||||
this.isShuttingDown = true;
|
||||
logger.info('Shutting down QueueManager...');
|
||||
this.logger.info('Shutting down QueueManager...');
|
||||
|
||||
// Create shutdown promise
|
||||
this.shutdownPromise = this.performShutdown();
|
||||
|
|
@ -420,7 +420,7 @@ export class QueueManager {
|
|||
|
||||
// await Promise.race([closePromise, timeoutPromise]);
|
||||
} catch (error) {
|
||||
logger.warn('Error closing queue', { error: (error as Error).message });
|
||||
this.logger.warn('Error closing queue', { error: (error as Error).message });
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -432,7 +432,7 @@ export class QueueManager {
|
|||
// Clear cache before shutdown
|
||||
await cache.clear();
|
||||
} catch (error) {
|
||||
logger.warn('Error clearing cache', { error: (error as Error).message });
|
||||
this.logger.warn('Error clearing cache', { error: (error as Error).message });
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -442,9 +442,9 @@ export class QueueManager {
|
|||
this.queues.clear();
|
||||
this.caches.clear();
|
||||
|
||||
logger.info('QueueManager shutdown complete');
|
||||
this.logger.info('QueueManager shutdown complete');
|
||||
} catch (error) {
|
||||
logger.error('Error during shutdown', { error: (error as Error).message });
|
||||
this.logger.error('Error during shutdown', { error: (error as Error).message });
|
||||
throw error;
|
||||
} finally {
|
||||
// Reset shutdown state
|
||||
|
|
@ -458,7 +458,7 @@ export class QueueManager {
|
|||
*/
|
||||
startAllWorkers(): void {
|
||||
if (!this.config.delayWorkerStart) {
|
||||
logger.info(
|
||||
this.logger.info(
|
||||
'startAllWorkers() called but workers already started automatically (delayWorkerStart is false)'
|
||||
);
|
||||
return;
|
||||
|
|
@ -475,7 +475,7 @@ export class QueueManager {
|
|||
}
|
||||
}
|
||||
|
||||
logger.info('All workers started', {
|
||||
this.logger.info('All workers started', {
|
||||
totalQueues: this.queues.size,
|
||||
queuesWithWorkers: workersStarted,
|
||||
delayWorkerStart: this.config.delayWorkerStart,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue