1. /**
  2. * Wraps user record logic for scheduled runs with memory optimization.
  3. *
  4. * @param {Array<Object>} userRecords An array of user record objects.
  5. * @param {Function} processUserRecord A function to process a single user record.
  6. * @param {number} batchSize The number of records to process in each batch. Defaults to 100.
  7. * @returns {Promise<Array<Object>>} A Promise that resolves to an array of processed user records.
  8. */
  9. async function processUserRecordsScheduled(userRecords, processUserRecord, batchSize = 100) {
  10. if (!Array.isArray(userRecords)) {
  11. console.error("userRecords must be an array.");
  12. return [];
  13. }
  14. if (typeof processUserRecord !== 'function') {
  15. console.error("processUserRecord must be a function.");
  16. return [];
  17. }
  18. const processedRecords = [];
  19. let startIndex = 0;
  20. while (startIndex < userRecords.length) {
  21. const endIndex = Math.min(startIndex + batchSize, userRecords.length);
  22. const batch = userRecords.slice(startIndex, endIndex);
  23. try {
  24. // Process the batch of user records
  25. const batchResults = await Promise.all(batch.map(processUserRecord));
  26. processedRecords.push(...batchResults); // Combine results from batch
  27. } catch (error) {
  28. console.error("Error processing batch:", error);
  29. // Handle the error appropriately. Consider logging, retrying, or skipping the batch.
  30. }
  31. startIndex = endIndex;
  32. }
  33. return processedRecords;
  34. }
  35. //Example Usage (Illustrative - replace with your actual logic)
  36. // async function processUser(user) {
  37. // // Perform your user record processing here.
  38. // // Example: Modify the user object. Avoid creating large intermediate objects.
  39. // user.processed = true;
  40. // return user;
  41. // }
  42. // async function main() {
  43. // const userRecords = [{ id: 1, name: "Alice" }, { id: 2, name: "Bob" }, ...]; // Replace with your data
  44. // const processed = await processUserRecordsScheduled(userRecords, processUser);
  45. // console.log("Processed Records:", processed);
  46. // }
  47. // main();

Add your comment