import { Queue } from 'bullmq';
/**
* Archives task queue content for diagnostics with fixed retry intervals.
* @param {Queue} queue The BullMQ queue to archive.
* @param {number} retryIntervalMs The interval in milliseconds between archive attempts.
*/
async function archiveQueueContent(queue, retryIntervalMs) {
let attempts = 0;
const maxAttempts = 5; // Limit the number of retry attempts
while (attempts < maxAttempts) {
try {
// Get all jobs from the queue
const jobs = await queue.getJobs();
if (jobs && jobs.length > 0) {
console.log(`Archiving ${jobs.length} jobs...`);
// Iterate over each job and archive its data
for (const job of jobs) {
try {
// Simulate archiving (replace with your actual archiving logic)
console.log(`Archiving job ${job.id}`);
// Example: Serializing the job data to JSON and saving to a file
// const jobData = JSON.stringify(job.data);
// await fs.promises.writeFile(`archive_${job.id}.json`, jobData);
} catch (error) {
console.error(`Error archiving job ${job.id}:`, error);
}
}
} else {
console.log('Queue is empty.');
}
console.log('Queue archiving complete.');
break; // Exit the retry loop if successful
} catch (error) {
console.error(`Error archiving queue content:`, error);
attempts++;
if (attempts < maxAttempts) {
console.log(`Retrying in ${retryIntervalMs}ms... (Attempt ${attempts}/${maxAttempts})`);
await new Promise(resolve => setTimeout(resolve, retryIntervalMs));
} else {
console.error('Archive failed after multiple attempts.');
}
}
}
}
export default archiveQueueContent;
Add your comment