fixed up ratelimiting
This commit is contained in:
parent
a616c92656
commit
a7146a3f57
15 changed files with 912 additions and 186 deletions
|
|
@ -20,6 +20,7 @@ export interface QueueWorkerConfig {
|
|||
startWorker?: boolean;
|
||||
handlerRegistry?: HandlerRegistry;
|
||||
serviceName?: string;
|
||||
queueManager?: any; // Reference to queue manager for rate limiting
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -35,6 +36,7 @@ export class Queue {
|
|||
private readonly logger: Logger;
|
||||
private readonly handlerRegistry?: HandlerRegistry;
|
||||
private serviceName?: string;
|
||||
private queueManager?: any;
|
||||
|
||||
constructor(
|
||||
queueName: string,
|
||||
|
|
@ -48,6 +50,7 @@ export class Queue {
|
|||
this.logger = logger || console;
|
||||
this.handlerRegistry = config.handlerRegistry;
|
||||
this.serviceName = config.serviceName;
|
||||
this.queueManager = config.queueManager;
|
||||
|
||||
this.logger.debug('Queue constructor called', {
|
||||
queueName,
|
||||
|
|
@ -332,13 +335,18 @@ export class Queue {
|
|||
const connection = getRedisConnection(this.redisConfig);
|
||||
|
||||
for (let i = 0; i < workerCount; i++) {
|
||||
const worker = new Worker(this.queueName, this.processJob.bind(this), {
|
||||
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`, {
|
||||
|
|
@ -348,7 +356,7 @@ export class Queue {
|
|||
});
|
||||
|
||||
// Setup worker event handlers
|
||||
worker.on('completed', job => {
|
||||
worker.on('completed', (job: Job) => {
|
||||
this.logger.trace('Job completed', {
|
||||
queueName: this.queueName,
|
||||
jobId: job.id,
|
||||
|
|
@ -357,17 +365,27 @@ export class Queue {
|
|||
});
|
||||
});
|
||||
|
||||
worker.on('failed', (job, err) => {
|
||||
this.logger.error('Job failed', {
|
||||
queueName: this.queueName,
|
||||
jobId: job?.id,
|
||||
handler: job?.data?.handler,
|
||||
operation: job?.data?.operation,
|
||||
error: err.message,
|
||||
});
|
||||
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 => {
|
||||
worker.on('error', (error: Error) => {
|
||||
this.logger.error('Worker error', {
|
||||
queueName: this.queueName,
|
||||
workerId: i,
|
||||
|
|
@ -386,9 +404,16 @@ export class Queue {
|
|||
}
|
||||
|
||||
/**
|
||||
* Process a job using the handler registry
|
||||
* Process a job using the handler registry with rate limiting
|
||||
*/
|
||||
private async processJob(job: Job): Promise<unknown> {
|
||||
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', {
|
||||
|
|
@ -404,6 +429,42 @@ export class Queue {
|
|||
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,
|
||||
|
|
@ -419,6 +480,7 @@ export class Queue {
|
|||
|
||||
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,
|
||||
|
|
@ -428,6 +490,11 @@ export class Queue {
|
|||
|
||||
return result;
|
||||
} catch (error) {
|
||||
// Re-throw RateLimitError without logging as error
|
||||
if (error === Worker.RateLimitError() || (error as any).name === 'RateLimitError') {
|
||||
throw error;
|
||||
}
|
||||
|
||||
this.logger.error('Job processing failed', {
|
||||
id: job.id,
|
||||
handler,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue