commit 2 refactor
This commit is contained in:
parent
588f666943
commit
32e43c9243
310 changed files with 33318 additions and 31189 deletions
|
|
@ -1,7 +1,8 @@
|
|||
import { Queue as BullQueue, QueueEvents, Worker, type Job } from 'bullmq';
|
||||
import type { LoggerLike as Logger } from '@stock-bot/logger';
|
||||
// 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';
|
||||
|
||||
|
|
@ -11,7 +12,7 @@ export interface QueueWorkerConfig {
|
|||
startWorker?: boolean;
|
||||
handlerRegistry?: HandlerRegistry;
|
||||
serviceName?: string;
|
||||
queueManager?: any; // Reference to queue manager for rate limiting
|
||||
queueManager?: Pick<QueueManager, 'checkRateLimit'>; // Reference to queue manager for rate limiting
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -27,7 +28,7 @@ export class Queue {
|
|||
private readonly logger: Logger;
|
||||
private readonly handlerRegistry?: HandlerRegistry;
|
||||
private serviceName?: string;
|
||||
private queueManager?: any;
|
||||
private queueManager?: Pick<QueueManager, 'checkRateLimit'>;
|
||||
|
||||
constructor(
|
||||
queueName: string,
|
||||
|
|
@ -274,26 +275,26 @@ export class Queue {
|
|||
const workerClosePromises = this.workers.map(async (worker, idx) => {
|
||||
try {
|
||||
const closePromise = worker.close();
|
||||
const timeoutPromise = new Promise<void>((resolve) => {
|
||||
const timeoutPromise = new Promise<void>(resolve => {
|
||||
setTimeout(() => {
|
||||
this.logger.warn('Worker close timeout, forcing closure', {
|
||||
queueName: this.queueName,
|
||||
workerId: idx
|
||||
this.logger.warn('Worker close timeout, forcing closure', {
|
||||
queueName: this.queueName,
|
||||
workerId: idx,
|
||||
});
|
||||
resolve();
|
||||
}, 2000); // 2 second timeout per worker
|
||||
});
|
||||
|
||||
|
||||
await Promise.race([closePromise, timeoutPromise]);
|
||||
} catch (error) {
|
||||
this.logger.warn('Error closing worker', {
|
||||
queueName: this.queueName,
|
||||
workerId: idx,
|
||||
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 });
|
||||
|
|
@ -337,19 +338,23 @@ export class Queue {
|
|||
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,
|
||||
},
|
||||
});
|
||||
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,
|
||||
|
|
@ -414,7 +419,7 @@ export class Queue {
|
|||
jobName: job.name,
|
||||
queueName: this.queueName,
|
||||
hasQueueManager: !!this.queueManager,
|
||||
jobData: job.data
|
||||
jobData: job.data,
|
||||
});
|
||||
const { handler, operation, payload }: JobData = job.data;
|
||||
|
||||
|
|
@ -436,19 +441,16 @@ export class Queue {
|
|||
this.logger.debug('Checking rate limits', {
|
||||
handler,
|
||||
operation,
|
||||
queueManager: this.queueManager.constructor.name
|
||||
queueManager: this.queueManager.constructor.name,
|
||||
});
|
||||
|
||||
const rateLimitCheck = await this.queueManager.checkRateLimit(
|
||||
handler,
|
||||
operation
|
||||
);
|
||||
|
||||
|
||||
const rateLimitCheck = await this.queueManager.checkRateLimit(handler, operation);
|
||||
|
||||
this.logger.debug('Rate limit check result', {
|
||||
allowed: rateLimitCheck.allowed,
|
||||
retryDelayMs: rateLimitCheck.retryDelayMs,
|
||||
handler,
|
||||
operation
|
||||
operation,
|
||||
});
|
||||
|
||||
if (!rateLimitCheck.allowed) {
|
||||
|
|
@ -456,7 +458,7 @@ export class Queue {
|
|||
id: job.id,
|
||||
handler,
|
||||
operation,
|
||||
retryDelayMs: rateLimitCheck.retryDelayMs
|
||||
retryDelayMs: rateLimitCheck.retryDelayMs,
|
||||
});
|
||||
|
||||
// Use BullMQ's manual rate limiting
|
||||
|
|
@ -493,10 +495,13 @@ export class Queue {
|
|||
return result;
|
||||
} catch (error) {
|
||||
// Re-throw RateLimitError without logging as error
|
||||
if (error === Worker.RateLimitError() || (error as any).name === 'RateLimitError') {
|
||||
if (
|
||||
error === Worker.RateLimitError() ||
|
||||
(error instanceof Error ? error.name : undefined) === 'RateLimitError'
|
||||
) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
|
||||
this.logger.error('Job processing failed', {
|
||||
id: job.id,
|
||||
handler,
|
||||
|
|
@ -544,7 +549,7 @@ export class Queue {
|
|||
* Get active workers from BullMQ
|
||||
* This uses BullMQ's built-in worker tracking
|
||||
*/
|
||||
async getActiveWorkers(): Promise<any[]> {
|
||||
async getActiveWorkers(): ReturnType<BullQueue['getWorkers']> {
|
||||
try {
|
||||
const workers = await this.bullQueue.getWorkers();
|
||||
return workers;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue