/**
* Wraps user record logic for scheduled runs with memory optimization.
*
* @param {Array<Object>} userRecords An array of user record objects.
* @param {Function} processUserRecord A function to process a single user record.
* @param {number} batchSize The number of records to process in each batch. Defaults to 100.
* @returns {Promise<Array<Object>>} A Promise that resolves to an array of processed user records.
*/
async function processUserRecordsScheduled(userRecords, processUserRecord, batchSize = 100) {
if (!Array.isArray(userRecords)) {
console.error("userRecords must be an array.");
return [];
}
if (typeof processUserRecord !== 'function') {
console.error("processUserRecord must be a function.");
return [];
}
const processedRecords = [];
let startIndex = 0;
while (startIndex < userRecords.length) {
const endIndex = Math.min(startIndex + batchSize, userRecords.length);
const batch = userRecords.slice(startIndex, endIndex);
try {
// Process the batch of user records
const batchResults = await Promise.all(batch.map(processUserRecord));
processedRecords.push(...batchResults); // Combine results from batch
} catch (error) {
console.error("Error processing batch:", error);
// Handle the error appropriately. Consider logging, retrying, or skipping the batch.
}
startIndex = endIndex;
}
return processedRecords;
}
//Example Usage (Illustrative - replace with your actual logic)
// async function processUser(user) {
// // Perform your user record processing here.
// // Example: Modify the user object. Avoid creating large intermediate objects.
// user.processed = true;
// return user;
// }
// async function main() {
// const userRecords = [{ id: 1, name: "Alice" }, { id: 2, name: "Bob" }, ...]; // Replace with your data
// const processed = await processUserRecordsScheduled(userRecords, processUser);
// console.log("Processed Records:", processed);
// }
// main();
Add your comment