/**
* Syncs resources of configuration files for data migration with fixed retry intervals.
*
* @param {Array<string>} configFiles An array of configuration file paths.
* @param {object} options Configuration options.
* @param {number} options.retryIntervalMs The retry interval in milliseconds.
* @param {number} options.maxRetries The maximum number of retry attempts.
* @returns {Promise<Array<object>>} A promise that resolves with an array of success/failure objects.
*/
async function syncConfigs(configFiles, options = {}) {
const retryIntervalMs = options.retryIntervalMs || 1000; // Default retry interval: 1 second
const maxRetries = options.maxRetries || 3; // Default max retries: 3
const results = [];
for (const configFile of configFiles) {
let success = false;
let retryCount = 0;
while (!success && retryCount < maxRetries) {
try {
const config = await syncConfigFile(configFile); // Helper function to sync a single config file
results.push({ file: configFile, success: true, data: config });
success = true;
} catch (error) {
console.error(`Failed to sync ${configFile}. Retry attempt ${retryCount + 1}/${maxRetries}.`, error);
retryCount++;
await new Promise(resolve => setTimeout(resolve, retryIntervalMs)); // Wait before retrying
}
}
if (!success) {
results.push({ file: configFile, success: false, error: "Failed after multiple retries" });
}
}
return results;
}
/**
* Helper function to sync a single configuration file.
* @param {string} filePath The path to the configuration file.
* @returns {Promise<object>} A promise that resolves with the parsed configuration object.
* @throws {Error} If the file cannot be read or parsed.
*/
async function syncConfigFile(filePath) {
try {
const fs = require('fs').promises; // Use promises-based fs API
const fileContent = await fs.readFile(filePath, 'utf8');
const config = JSON.parse(fileContent); // Assuming JSON format
return config;
} catch (error) {
throw new Error(`Error syncing config file ${filePath}: ${error.message}`);
}
}
module.exports = syncConfigs;
Add your comment