Spaces:
Paused
Paused
File size: 5,710 Bytes
9284ad7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | "use strict";
/**
* Asynchronous Job Queue for Webhook Processing
*
* GitHub webhooks timeout after 10 seconds. Long-running PR reviews
* can exceed this. This queue acknowledges webhooks immediately and
* processes them asynchronously.
*
* TODO: For production with horizontal scaling, replace with Redis-backed queue (Bull/BullMQ)
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.jobQueue = void 0;
const pino_1 = __importDefault(require("pino"));
const logger = (0, pino_1.default)({ level: process.env.LOG_LEVEL || 'info' });
const defaultConfig = {
maxConcurrent: 3,
pollIntervalMs: 1000,
maxAttempts: 3
};
class JobQueue {
jobs = new Map();
handlers = new Map();
processing = new Set();
config;
isRunning = false;
intervalId;
constructor(config = {}) {
this.config = { ...defaultConfig, ...config };
}
/**
* Register a job handler for a specific job type
*/
registerHandler(type, handler) {
this.handlers.set(type, handler);
logger.info({ type }, 'Registered job handler');
}
/**
* Add a job to the queue
*/
async add(type, data, maxAttempts) {
const id = `${type}_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
const job = {
id,
type,
data,
attempts: 0,
maxAttempts: maxAttempts || this.config.maxAttempts,
createdAt: Date.now(),
status: 'pending'
};
this.jobs.set(id, job);
logger.info({ jobId: id, type }, 'Job added to queue');
// Start processing if not already running
if (!this.isRunning) {
this.start();
}
return id;
}
/**
* Get job status
*/
getStatus(jobId) {
return this.jobs.get(jobId);
}
/**
* Get queue metrics
*/
getMetrics() {
const jobs = Array.from(this.jobs.values());
return {
total: jobs.length,
pending: jobs.filter(j => j.status === 'pending').length,
processing: jobs.filter(j => j.status === 'processing').length,
completed: jobs.filter(j => j.status === 'completed').length,
failed: jobs.filter(j => j.status === 'failed').length
};
}
/**
* Start the job processor
*/
start() {
if (this.isRunning)
return;
this.isRunning = true;
logger.info('Job queue started');
this.intervalId = setInterval(() => {
this.processJobs();
}, this.config.pollIntervalMs);
}
/**
* Stop the job processor
*/
stop() {
this.isRunning = false;
if (this.intervalId) {
clearInterval(this.intervalId);
this.intervalId = undefined;
}
logger.info('Job queue stopped');
}
/**
* Process pending jobs
*/
async processJobs() {
if (this.processing.size >= this.config.maxConcurrent) {
return; // At capacity
}
const pendingJobs = Array.from(this.jobs.values())
.filter(j => j.status === 'pending')
.sort((a, b) => a.createdAt - b.createdAt); // FIFO
for (const job of pendingJobs) {
if (this.processing.size >= this.config.maxConcurrent) {
break;
}
this.processJob(job);
}
}
/**
* Process a single job
*/
async processJob(job) {
const handler = this.handlers.get(job.type);
if (!handler) {
logger.error({ jobId: job.id, type: job.type }, 'No handler registered for job type');
job.status = 'failed';
job.error = 'No handler registered';
return;
}
this.processing.add(job.id);
job.status = 'processing';
job.attempts++;
logger.info({ jobId: job.id, type: job.type, attempt: job.attempts }, 'Processing job');
try {
await handler(job);
job.status = 'completed';
logger.info({ jobId: job.id }, 'Job completed successfully');
}
catch (error) {
logger.error({ jobId: job.id, error: error.message }, 'Job failed');
if (job.attempts < job.maxAttempts) {
job.status = 'pending'; // Retry
job.error = error.message;
logger.info({ jobId: job.id, attemptsLeft: job.maxAttempts - job.attempts }, 'Job scheduled for retry');
}
else {
job.status = 'failed';
job.error = error.message;
logger.error({ jobId: job.id }, 'Job failed permanently after max attempts');
}
}
finally {
this.processing.delete(job.id);
}
}
/**
* Cleanup old completed jobs
*/
cleanup(maxAgeMs = 24 * 60 * 60 * 1000) {
const now = Date.now();
let cleaned = 0;
for (const [id, job] of this.jobs.entries()) {
if ((job.status === 'completed' || job.status === 'failed') &&
now - job.createdAt > maxAgeMs) {
this.jobs.delete(id);
cleaned++;
}
}
if (cleaned > 0) {
logger.info({ cleaned }, 'Cleaned up old jobs');
}
}
}
// Singleton instance
exports.jobQueue = new JobQueue();
// Auto-cleanup every hour
setInterval(() => {
exports.jobQueue.cleanup();
}, 60 * 60 * 1000);
//# sourceMappingURL=job-queue.js.map |