stock-bot/libs/core/queue/src/queue.ts

611 lines
18 KiB
TypeScript

import { Queue as BullQueue, QueueEvents, Worker, type Job } from 'bullmq';
// Handler registry will be injected
import type { HandlerRegistry } from '@stock-bot/handler-registry';
import type { LoggerLike as Logger } from '@stock-bot/logger';
import type { QueueManager } from './queue-manager';
import type { ExtendedJobOptions, JobData, JobOptions, QueueStats, RedisConfig } from './types';
import { getRedisConnection } from './utils';
export interface QueueWorkerConfig {
workers?: number;
concurrency?: number;
startWorker?: boolean;
handlerRegistry?: HandlerRegistry;
serviceName?: string;
queueManager?: Pick<QueueManager, 'checkRateLimit'>; // Reference to queue manager for rate limiting
}
/**
* Consolidated Queue class that handles both job operations and optional worker management
* Can be used as a simple job queue or with workers for automatic processing
*/
export class Queue {
private bullQueue: BullQueue;
private workers: Worker[] = [];
private queueEvents?: QueueEvents;
private closePromise: Promise<void> | null = null;
private queueName: string;
private redisConfig: RedisConfig;
private readonly logger: Logger;
private readonly handlerRegistry?: HandlerRegistry;
private serviceName?: string;
private queueManager?: Pick<QueueManager, 'checkRateLimit'>;
constructor(
queueName: string,
redisConfig: RedisConfig,
defaultJobOptions: JobOptions = {},
config: QueueWorkerConfig = {},
logger?: Logger
) {
this.queueName = queueName;
this.redisConfig = redisConfig;
this.logger = logger || console;
this.handlerRegistry = config.handlerRegistry;
this.serviceName = config.serviceName;
this.queueManager = config.queueManager;
this.logger.debug('Queue constructor called', {
queueName,
serviceName: this.serviceName,
hasHandlerRegistry: !!config.handlerRegistry,
handlerRegistryType: config.handlerRegistry ? typeof config.handlerRegistry : 'undefined',
configKeys: Object.keys(config),
});
const connection = getRedisConnection(redisConfig);
// Initialize BullMQ queue
this.bullQueue = new BullQueue(queueName, {
// Publishing must fail promptly during outages; durable producers retain their intent.
connection: { ...connection, maxRetriesPerRequest: 1, enableOfflineQueue: false },
defaultJobOptions: {
removeOnComplete: 100,
removeOnFail: 100,
attempts: 3,
backoff: {
type: 'exponential',
delay: 1000,
},
...defaultJobOptions,
},
});
this.bullQueue.on('error', error =>
this.logger.error('Queue connection error', { queueName, error })
);
// Initialize queue events if workers will be used
if (config.workers && config.workers > 0) {
this.queueEvents = new QueueEvents(queueName, { connection });
}
// Start workers if requested and not explicitly disabled
if (config.workers && config.workers > 0 && config.startWorker !== false) {
this.logger.info('Starting workers for queue', {
queueName,
workers: config.workers,
concurrency: config.concurrency || 1,
hasHandlerRegistry: !!this.handlerRegistry,
});
this.startWorkers(config.workers, config.concurrency || 1);
} else {
this.logger.info('Not starting workers for queue', {
queueName,
workers: config.workers || 0,
startWorker: config.startWorker,
hasHandlerRegistry: !!this.handlerRegistry,
});
}
this.logger.trace('Queue created', {
queueName,
workers: config.workers || 0,
concurrency: config.concurrency || 1,
});
}
/**
* Get the queue name
*/
getName(): string {
return this.queueName;
}
/**
* Get the underlying BullMQ queue instance (for monitoring/admin purposes)
*/
getBullQueue(): BullQueue {
return this.bullQueue;
}
/**
* Add a single job to the queue
*/
async add(name: string, data: JobData, options: JobOptions = {}): Promise<Job> {
this.logger.trace('Adding job', { queueName: this.queueName, jobName: name });
return await this.bullQueue.add(name, data, options);
}
/**
* Add multiple jobs to the queue in bulk
*/
async addBulk(jobs: Array<{ name: string; data: JobData; opts?: JobOptions }>): Promise<Job[]> {
this.logger.trace('Adding bulk jobs', {
queueName: this.queueName,
jobCount: jobs.length,
});
return await this.bullQueue.addBulk(jobs);
}
/**
* Add a scheduled job with cron-like pattern
*/
async addScheduledJob(
name: string,
data: JobData,
cronPattern: string,
options: ExtendedJobOptions = {}
): Promise<Job> {
const scheduledOptions: ExtendedJobOptions = {
...options,
repeat: {
pattern: cronPattern,
// Use job name as repeat key to prevent duplicates
key: `${this.queueName}:${name}`,
...options.repeat,
},
};
this.logger.info('Adding scheduled job', {
queueName: this.queueName,
jobName: name,
cronPattern,
repeatKey: scheduledOptions.repeat?.key,
immediately: scheduledOptions.repeat?.immediately,
});
return await this.bullQueue.add(name, data, scheduledOptions);
}
/**
* Get queue statistics
*/
async getStats(): Promise<QueueStats> {
const [waiting, active, completed, failed, delayed] = await Promise.all([
this.bullQueue.getWaiting(),
this.bullQueue.getActive(),
this.bullQueue.getCompleted(),
this.bullQueue.getFailed(),
this.bullQueue.getDelayed(),
]);
const isPaused = await this.bullQueue.isPaused();
// Get actual active worker count from BullMQ
const activeWorkerCount = await this.getActiveWorkerCount();
return {
waiting: waiting.length,
active: active.length,
completed: completed.length,
failed: failed.length,
delayed: delayed.length,
paused: isPaused,
workers: activeWorkerCount, // Use BullMQ's tracked count instead of local array
};
}
/**
* Get a specific job by ID
*/
async getJob(jobId: string): Promise<Job | undefined> {
return await this.bullQueue.getJob(jobId);
}
/** Wait for a result using a QueueEvents connection owned by this queue. */
async waitForJob<T>(job: Job<unknown, T>, timeout = 30000): Promise<T> {
if (!this.queueEvents) {
this.queueEvents = new QueueEvents(this.queueName, {
connection: getRedisConnection(this.redisConfig),
});
}
await this.queueEvents.waitUntilReady();
return job.waitUntilFinished(this.queueEvents, timeout);
}
/**
* Get jobs by state
*/
async getJobs(
states: Array<'waiting' | 'active' | 'completed' | 'failed' | 'delayed'>,
start = 0,
end = 100
): Promise<Job[]> {
return await this.bullQueue.getJobs(states, start, end);
}
/**
* Pause the queue (stops processing new jobs)
*/
async pause(): Promise<void> {
await this.bullQueue.pause();
this.logger.info('Queue paused', { queueName: this.queueName });
}
/**
* Resume the queue
*/
async resume(): Promise<void> {
await this.bullQueue.resume();
this.logger.info('Queue resumed', { queueName: this.queueName });
}
/**
* Drain the queue (remove all jobs)
*/
async drain(delayed = false): Promise<void> {
await this.bullQueue.drain(delayed);
this.logger.info('Queue drained', { queueName: this.queueName, delayed });
}
/**
* Clean completed and failed jobs
*/
async clean(
grace: number = 0,
limit: number = 100,
type: 'completed' | 'failed' = 'completed'
): Promise<void> {
await this.bullQueue.clean(grace, limit, type);
this.logger.debug('Queue cleaned', { queueName: this.queueName, type, grace, limit });
}
/**
* Wait until the queue is ready
*/
async waitUntilReady(): Promise<void> {
await this.bullQueue.waitUntilReady();
}
/**
* Close the queue (cleanup resources)
*/
close(): Promise<void> {
// Retain the actual drain promise. The application owns any deadline that
// forces process exit; a queue must not report completion with active jobs.
return (this.closePromise ??= Promise.resolve().then(() => this.performClose()));
}
private async performClose(): Promise<void> {
try {
// Stop taking jobs and wait for every active worker before its transports.
if (this.workers.length > 0) {
const workerClosePromises = this.workers.map(async (worker, idx) => {
try {
await worker.close();
} catch (error) {
this.logger.warn('Error closing worker', {
queueName: this.queueName,
workerId: idx,
error,
});
}
});
await Promise.all(workerClosePromises);
this.workers = [];
this.logger.debug('Workers closed', { queueName: this.queueName });
}
// Close queue events
if (this.queueEvents) {
try {
await this.queueEvents.close();
this.logger.debug('Queue events closed', { queueName: this.queueName });
} catch (error) {
this.logger.warn('Error closing queue events', { queueName: this.queueName, error });
}
}
// Close the queue itself
await this.bullQueue.close();
this.logger.info('Queue closed', { queueName: this.queueName });
} catch (error) {
this.logger.error('Error closing queue', { queueName: this.queueName, error });
throw error;
}
}
/**
* Create a child logger with additional context
* Useful for batch processing and other queue operations
*/
createChildLogger(name: string, context?: Record<string, unknown>) {
if (this.logger && typeof this.logger.child === 'function') {
return this.logger.child(name, context);
}
// Fallback to main logger if child not supported (e.g., console)
return this.logger;
}
/**
* Start workers for this queue
*/
private startWorkers(workerCount: number, concurrency: number): void {
const connection = getRedisConnection(this.redisConfig);
for (let i = 0; i < workerCount; i++) {
const worker: Worker = new Worker(
this.queueName,
async (job: Job) => this.processJob(job, worker),
{
connection,
concurrency,
maxStalledCount: 3,
stalledInterval: 30000,
// Add a name to identify the worker
name: `${this.serviceName || 'unknown'}_worker_${i}`,
// Enable manual rate limiting
limiter: {
max: 100,
duration: 1,
},
}
);
this.logger.info(`Starting worker ${i + 1}/${workerCount} for queue`, {
queueName: this.queueName,
workerName: worker.name,
concurrency,
});
// Setup worker event handlers
worker.on('completed', (job: Job) => {
this.logger.trace('Job completed', {
queueName: this.queueName,
jobId: job.id,
handler: job.data?.handler,
operation: job.data?.operation,
});
});
worker.on('failed', (job: Job | undefined, err: Error) => {
// Only log as error if it's not a rate limit error
if (err.name !== 'RateLimitError') {
this.logger.error('Job failed', {
queueName: this.queueName,
jobId: job?.id,
handler: job?.data?.handler,
operation: job?.data?.operation,
error: err.message,
});
} else {
this.logger.debug('Job rate limited', {
queueName: this.queueName,
jobId: job?.id,
handler: job?.data?.handler,
operation: job?.data?.operation,
});
}
});
worker.on('error', (error: Error) => {
this.logger.error('Worker error', {
queueName: this.queueName,
workerId: i,
error: error.message,
});
});
this.workers.push(worker);
}
this.logger.info('Workers started', {
queueName: this.queueName,
workerCount,
concurrency,
});
}
/**
* Process a job using the handler registry with rate limiting
*/
private async processJob(job: Job, worker: Worker): Promise<unknown> {
this.logger.debug('Processing job', {
jobId: job.id,
jobName: job.name,
queueName: this.queueName,
hasQueueManager: !!this.queueManager,
jobData: job.data,
});
const { handler, operation, payload }: JobData = job.data;
this.logger.trace('Processing job', {
id: job.id,
handler,
operation,
queueName: this.queueName,
});
try {
// Look up handler in registry
if (!this.handlerRegistry) {
throw new Error('Handler registry not configured for worker processing');
}
// Check rate limits if available
if (this.queueManager && this.queueManager.checkRateLimit) {
this.logger.debug('Checking rate limits', {
handler,
operation,
queueManager: this.queueManager.constructor.name,
});
const rateLimitCheck = await this.queueManager.checkRateLimit(handler, operation);
this.logger.debug('Rate limit check result', {
allowed: rateLimitCheck.allowed,
retryDelayMs: rateLimitCheck.retryDelayMs,
handler,
operation,
});
if (!rateLimitCheck.allowed) {
this.logger.trace('Rate limit exceeded, throwing Worker.RateLimitError', {
id: job.id,
handler,
operation,
retryDelayMs: rateLimitCheck.retryDelayMs,
});
// Use BullMQ's manual rate limiting
// First inform the worker about the rate limit delay
await worker.rateLimit(rateLimitCheck.retryDelayMs || 1000);
// Then throw the RateLimitError
throw Worker.RateLimitError();
}
}
this.logger.debug('Looking up handler in registry', {
handler,
operation,
queueName: this.queueName,
registeredHandlers: this.handlerRegistry.getHandlerNames(),
});
const jobHandler = this.handlerRegistry.getOperation(handler, operation);
if (!jobHandler) {
throw new Error(`No handler found for ${handler}:${operation}`);
}
const result = await jobHandler(payload);
this.logger.info(`[Queue] Job ${job.id} - ${handler}:${operation} completed successfully`);
this.logger.trace('Job completed successfully', {
id: job.id,
handler,
operation,
queueName: this.queueName,
});
return result;
} catch (error) {
// Re-throw RateLimitError without logging as error
if (
error === Worker.RateLimitError() ||
(error instanceof Error ? error.name : undefined) === 'RateLimitError'
) {
throw error;
}
this.logger.error('Job processing failed', {
id: job.id,
handler,
operation,
queueName: this.queueName,
error: error instanceof Error ? error.message : String(error),
});
throw error;
}
}
/**
* Start workers manually (for delayed initialization)
*/
startWorkersManually(workerCount: number, concurrency: number = 1): void {
if (this.workers.length > 0) {
this.logger.warn('Workers already started for queue', { queueName: this.queueName });
return;
}
this.logger.info('Starting workers manually', {
queueName: this.queueName,
workerCount,
concurrency,
hasHandlerRegistry: !!this.handlerRegistry,
});
// Initialize queue events if not already done
if (!this.queueEvents) {
const connection = getRedisConnection(this.redisConfig);
this.queueEvents = new QueueEvents(this.queueName, { connection });
}
this.startWorkers(workerCount, concurrency);
}
/**
* Get the number of active workers
*/
getWorkerCount(): number {
return this.workers.length;
}
/**
* Get active workers from BullMQ
* This uses BullMQ's built-in worker tracking
*/
async getActiveWorkers(): ReturnType<BullQueue['getWorkers']> {
try {
const workers = await this.bullQueue.getWorkers();
return workers;
} catch (error) {
this.logger.error('Failed to get active workers', {
queueName: this.queueName,
error,
});
return [];
}
}
/**
* Get count of active workers from BullMQ
*/
async getActiveWorkerCount(): Promise<number | undefined> {
try {
const workers = await this.bullQueue.getWorkers();
return workers.length;
} catch (error) {
this.logger.error('Failed to get active worker count', {
queueName: this.queueName,
error,
});
// The optional QueueStats.workers capability distinguishes an unavailable
// observation from a successful query that found zero workers.
return undefined;
}
}
/**
* Get detailed worker information
*/
async getWorkerDetails(): Promise<
Array<{
id: string;
name?: string;
addr?: string;
age?: number;
idle?: number;
started?: number;
}>
> {
try {
const workers = await this.bullQueue.getWorkers();
return workers.map(worker => ({
id: worker.id || 'unknown',
name: worker.name,
addr: worker.addr,
age: typeof worker.age === 'string' ? parseInt(worker.age) : worker.age,
idle: typeof worker.idle === 'string' ? parseInt(worker.idle) : worker.idle,
started: typeof worker.started === 'string' ? parseInt(worker.started) : worker.started,
}));
} catch (error) {
this.logger.error('Failed to get worker details', {
queueName: this.queueName,
error,
});
return [];
}
}
}