/**
* Configures file processing parameters for batch execution with retries.
*
* @param {Array<object>} files An array of file objects. Each object should have a 'path' property (string)
* and any other necessary metadata.
* @param {object} options Configuration options.
* @param {number} options.retryIntervalMs The interval (in milliseconds) between retry attempts.
* @param {number} options.maxRetries The maximum number of retry attempts.
* @param {function} options.processFile Function to process a single file. Takes the file object as input. Returns a promise.
* @returns {Promise<Array<object>>} A promise that resolves to an array of processed files, or rejects if all files fail after retries.
*/
async function processFilesBatch(files, options) {
const { retryIntervalMs, maxRetries, processFile } = options;
if (!files || !Array.isArray(files)) {
throw new Error("Files must be a non-empty array.");
}
if (typeof retryIntervalMs !== 'number' || retryIntervalMs <= 0) {
throw new Error("retryIntervalMs must be a positive number.");
}
if (typeof maxRetries !== 'number' || maxRetries <= 0) {
throw new Error("maxRetries must be a positive number.");
}
if (typeof processFile !== 'function') {
throw new Error("processFile must be a function.");
}
const processedFiles = [];
for (const file of files) {
let retryCount = 0;
let fileProcessed = false;
while (!fileProcessed && retryCount < maxRetries) {
try {
const result = await processFile(file); // Process the file
processedFiles.push(result);
fileProcessed = true;
} catch (error) {
retryCount++;
console.log(`File ${file.path} failed. Retry attempt ${retryCount}/${maxRetries}. Error: ${error.message}`);
await new Promise(resolve => setTimeout(resolve, retryIntervalMs)); // Wait before retrying
}
}
if (!fileProcessed) {
console.error(`File ${file.path} failed after ${maxRetries} retries.`);
//Consider rejecting the entire batch here, depending on desired behavior.
//For now, we continue with the next file.
}
}
return processedFiles;
}
Add your comment