1. /**
  2. * Initializes log file components for scheduled runs with fixed retry intervals.
  3. *
  4. * @param {object} config Configuration object containing log file details and retry settings.
  5. * config.logFilePath: Path to the log file.
  6. * config.retryIntervalMs: Retry interval in milliseconds.
  7. * config.maxRetries: Maximum number of retry attempts.
  8. */
  9. function initializeLogFileComponents(config) {
  10. const logFilePath = config.logFilePath;
  11. const retryIntervalMs = config.retryIntervalMs;
  12. const maxRetries = config.maxRetries;
  13. // Initialize retry counter
  14. let retryCount = 0;
  15. // Function to attempt to read the log file
  16. async function attemptReadLogFile() {
  17. try {
  18. const data = await readLogFile(logFilePath);
  19. return data; // Return data if successful
  20. } catch (error) {
  21. retryCount++;
  22. if (retryCount <= maxRetries) {
  23. console.log(`Attempt ${retryCount} failed. Retrying in ${retryIntervalMs}ms...`);
  24. await delay(retryIntervalMs); // Wait before retrying
  25. return attemptReadLogFile(); // Recursively retry
  26. } else {
  27. console.error(`Failed to read log file after ${maxRetries} attempts.`);
  28. throw error; // Re-throw the error if all retries fail
  29. }
  30. }
  31. }
  32. // Function to read the log file (implementation depends on your environment)
  33. async function readLogFile(filePath) {
  34. // Replace with your actual file reading logic (e.g., using fs, axios, etc.)
  35. // This is a placeholder implementation.
  36. console.log(`Reading log file: ${filePath}`);
  37. await delay(500); // Simulate file reading time
  38. return `Log data from ${filePath}`;
  39. }
  40. // Function to delay execution
  41. function delay(ms) {
  42. return new Promise(resolve => setTimeout(resolve, ms));
  43. }
  44. // Return the retryCount (can be used by other components)
  45. return {
  46. retryCount: retryCount,
  47. attemptReadLogFile: attemptReadLogFile
  48. };
  49. }

Add your comment