improve AI

This commit is contained in:
overcuriousity
2025-08-01 22:29:38 +02:00
parent 1b9d9b437b
commit 8693cd87d4
6 changed files with 426 additions and 269 deletions

View File

@@ -1,4 +1,4 @@
// src/utils/rateLimitedQueue.ts
// src/utils/rateLimitedQueue.ts - FIXED: Memory leak and better cleanup
import dotenv from "dotenv";
@@ -31,6 +31,43 @@ class RateLimitedQueue {
private delayMs = RATE_LIMIT_DELAY_MS;
private lastProcessedAt = 0;
private currentlyProcessingTaskId: string | null = null;
private cleanupInterval: NodeJS.Timeout;
private readonly TASK_RETENTION_MS = 30000;
constructor() {
this.cleanupInterval = setInterval(() => {
this.cleanupOldTasks();
}, 30000);
}
private cleanupOldTasks(): void {
const now = Date.now();
const initialLength = this.tasks.length;
this.tasks = this.tasks.filter(task => {
if (task.status === 'queued' || task.status === 'processing') {
return true;
}
if (task.completedAt && (now - task.completedAt) > this.TASK_RETENTION_MS) {
return false;
}
return true;
});
const cleaned = initialLength - this.tasks.length;
if (cleaned > 0) {
console.log(`[QUEUE] Cleaned up ${cleaned} old tasks, ${this.tasks.length} remaining`);
}
}
public shutdown(): void {
if (this.cleanupInterval) {
clearInterval(this.cleanupInterval);
}
}
add<T>(task: Task<T>, taskId?: string): Promise<T> {
const id = taskId || this.generateTaskId();
@@ -103,7 +140,6 @@ class RateLimitedQueue {
const processingOffset = processingTasks.length > 0 ? 1 : 0;
status.currentPosition = processingOffset + positionInQueue + 1;
}
} else if (task.status === 'completed' || task.status === 'failed') {
}
} else {
const taskTimestamp = taskId.match(/ai_(\d+)_/)?.[1];
@@ -152,7 +188,6 @@ class RateLimitedQueue {
this.currentlyProcessingTaskId = nextTask.id;
this.lastProcessedAt = Date.now();
try {
await nextTask.task();
nextTask.status = 'completed';
@@ -166,14 +201,6 @@ class RateLimitedQueue {
this.currentlyProcessingTaskId = null;
setTimeout(() => {
const index = this.tasks.findIndex(t => t.id === nextTask.id);
if (index >= 0) {
console.log(`[QUEUE] Removing completed task ${nextTask.id}`);
this.tasks.splice(index, 1);
}
}, 10000);
const hasMoreQueued = this.tasks.some(t => t.status === 'queued');
if (hasMoreQueued) {
console.log(`[QUEUE] Waiting ${this.delayMs}ms before next task`);
@@ -201,4 +228,8 @@ export function getQueueStatus(taskId?: string): QueueStatus {
return queue.getStatus(taskId);
}
export function shutdownQueue(): void {
queue.shutdown();
}
export default queue;