NOTE: This is a cronjob so I need to exit with a process.exit command when the processing is completed.
In this one particular case, I'm going to use some dummy code to represent the problem b/c I believe pasting the code from this script would generate more questions and cause confusion.
I'm curious about the framework choices I've made. I know there is a better way to organize the promises to create maximum parallel efficiency and, at the same time, know when to exit.
The code below may not be syntactically perfect since I'm just writing it by hand here for the purposes of showing the corner I painted myself into.
let promises = [];
promises.push(db.get_customers());
let cus_recs = await Promise.all(promises); promises = [];
cus_recs.map((cus_rec) => {
promises.push(db.get_criteria(cus_rec));
});
let criteria_recs = await Promise.all(promises); promises = [];
cus_recs.map((cus_rec, a) => {
let cus_criteria_recs = criteria_recs[a];
cus_criteria_recs.map((cus_criteria_rec) => {
ci.build_lis_match_data(cus_criteria_rec).then((lis_match_data) => {
lis.get_matching_listings(cus_criteria_rec, lis_match_data).then((lis_recs) => {
lis_recs.map((lis_rec) => {
ci.build_lis_match_table(cus_criteria_rec, lis_rec).then((lis_match_table) => {
promises.push(lis.do_the_thing(cus_rec, lis_rec));
});
});
});
});
});
});
if (promises.length > 0) {
await Promise.all(promises); promises = [];
}
process.exit(0);
The issue I am faced with is where to place process.exit(0); If placed as shown, node exists before all the promises are finished.
I am aware I can turn this into for loops and use async/await to just wait on each task.
I was hoping that this method would create extreme parallelism of these intensive tasks. Many of these tasks are > 0.1 secs and this will run for 600,000 (or so) records per day.