1. /**
  2. * Tracks execution of directories with fixed retry intervals.
  3. * @param {string[]} directories An array of directory paths to track.
  4. * @param {object} options Configuration options.
  5. * @param {number} options.retryIntervalMs The retry interval in milliseconds. Defaults to 5000.
  6. * @param {number} options.maxRetries The maximum number of retries. Defaults to 3.
  7. * @param {function} options.executeFunction A function to execute within each directory. Takes directory path as argument.
  8. * @returns {Promise<object>} A promise that resolves with an object containing the results of each directory execution.
  9. */
  10. async function trackDirectoryExecution(directories, options = {}) {
  11. const { retryIntervalMs = 5000, maxRetries = 3, executeFunction } = options;
  12. const results = {};
  13. for (const directory of directories) {
  14. let retryCount = 0;
  15. while (retryCount < maxRetries) {
  16. try {
  17. // Execute the function for the current directory
  18. console.log(`Executing directory: ${directory}`);
  19. const result = await executeFunction(directory);
  20. console.log(`Directory ${directory} executed successfully.`);
  21. results[directory] = result;
  22. break; // Exit the retry loop if successful
  23. } catch (error) {
  24. console.error(`Error executing directory ${directory}:`, error);
  25. retryCount++;
  26. if (retryCount < maxRetries) {
  27. console.log(`Retrying directory ${directory} in ${retryIntervalMs}ms...`);
  28. await new Promise(resolve => setTimeout(resolve, retryIntervalMs));
  29. } else {
  30. console.error(`Max retries reached for directory ${directory}.`);
  31. }
  32. }
  33. }
  34. }
  35. return results;
  36. }

Add your comment