2025-05-31 10:07:32 +08:00

45 lines
1.4 KiB
JavaScript

import Queue from 'bull';
// Create a Bull queue instance
const asnafAnalysisQueue = new Queue('asnaf-analysis', {
redis: {
host: process.env.REDIS_HOST || 'localhost',
port: process.env.REDIS_PORT || 6379,
password: process.env.REDIS_PASSWORD
}
});
export default defineEventHandler(async (event) => {
try {
// Get all jobs from different states
const [waiting, active, completed, failed] = await Promise.all([
asnafAnalysisQueue.getWaiting(),
asnafAnalysisQueue.getActive(),
asnafAnalysisQueue.getCompleted(),
asnafAnalysisQueue.getFailed()
]);
// Combine all jobs and format them
const allJobs = [...waiting, ...active, ...completed, ...failed]
.map(job => ({
id: job.id,
status: job.finishedOn ? 'completed' :
job.failedReason ? 'failed' :
job.processedOn ? 'active' : 'waiting',
data: job.data,
timestamp: job.timestamp,
processedOn: job.processedOn,
finishedOn: job.finishedOn,
failedReason: job.failedReason
}))
.sort((a, b) => b.timestamp - a.timestamp); // Sort by timestamp, newest first
return allJobs;
} catch (error) {
console.error('Error fetching queue data:', error);
throw createError({
statusCode: 500,
statusMessage: 'Failed to fetch queue data'
});
}
});