1. import { Queue } from 'bullmq';
  2. /**
  3. * Archives task queue content for diagnostics with fixed retry intervals.
  4. * @param {Queue} queue The BullMQ queue to archive.
  5. * @param {number} retryIntervalMs The interval in milliseconds between archive attempts.
  6. */
  7. async function archiveQueueContent(queue, retryIntervalMs) {
  8. let attempts = 0;
  9. const maxAttempts = 5; // Limit the number of retry attempts
  10. while (attempts < maxAttempts) {
  11. try {
  12. // Get all jobs from the queue
  13. const jobs = await queue.getJobs();
  14. if (jobs && jobs.length > 0) {
  15. console.log(`Archiving ${jobs.length} jobs...`);
  16. // Iterate over each job and archive its data
  17. for (const job of jobs) {
  18. try {
  19. // Simulate archiving (replace with your actual archiving logic)
  20. console.log(`Archiving job ${job.id}`);
  21. // Example: Serializing the job data to JSON and saving to a file
  22. // const jobData = JSON.stringify(job.data);
  23. // await fs.promises.writeFile(`archive_${job.id}.json`, jobData);
  24. } catch (error) {
  25. console.error(`Error archiving job ${job.id}:`, error);
  26. }
  27. }
  28. } else {
  29. console.log('Queue is empty.');
  30. }
  31. console.log('Queue archiving complete.');
  32. break; // Exit the retry loop if successful
  33. } catch (error) {
  34. console.error(`Error archiving queue content:`, error);
  35. attempts++;
  36. if (attempts < maxAttempts) {
  37. console.log(`Retrying in ${retryIntervalMs}ms... (Attempt ${attempts}/${maxAttempts})`);
  38. await new Promise(resolve => setTimeout(resolve, retryIntervalMs));
  39. } else {
  40. console.error('Archive failed after multiple attempts.');
  41. }
  42. }
  43. }
  44. }
  45. export default archiveQueueContent;

Add your comment