/**
* Tracks execution of directories with fixed retry intervals.
* @param {string[]} directories An array of directory paths to track.
* @param {object} options Configuration options.
* @param {number} options.retryIntervalMs The retry interval in milliseconds. Defaults to 5000.
* @param {number} options.maxRetries The maximum number of retries. Defaults to 3.
* @param {function} options.executeFunction A function to execute within each directory. Takes directory path as argument.
* @returns {Promise<object>} A promise that resolves with an object containing the results of each directory execution.
*/
async function trackDirectoryExecution(directories, options = {}) {
const { retryIntervalMs = 5000, maxRetries = 3, executeFunction } = options;
const results = {};
for (const directory of directories) {
let retryCount = 0;
while (retryCount < maxRetries) {
try {
// Execute the function for the current directory
console.log(`Executing directory: ${directory}`);
const result = await executeFunction(directory);
console.log(`Directory ${directory} executed successfully.`);
results[directory] = result;
break; // Exit the retry loop if successful
} catch (error) {
console.error(`Error executing directory ${directory}:`, error);
retryCount++;
if (retryCount < maxRetries) {
console.log(`Retrying directory ${directory} in ${retryIntervalMs}ms...`);
await new Promise(resolve => setTimeout(resolve, retryIntervalMs));
} else {
console.error(`Max retries reached for directory ${directory}.`);
}
}
}
}
return results;
}
Add your comment