Digging Into Node.js Child Processes Exercise
Transcript from the "Child Processes Exercise" Lesson
>> Kyle Simpson: So now that we've written our child process, your final exercise here, and you're gonna have to put on your thinking caps to figure this one out. What you're going to be doing is trying to run your child process, check to see if it was successful, and then wait 1,000 milliseconds and then try it again.
[00:00:22] So we basically just wanna keep running by spinning up a child process. And actually, eventually we wanna be able to spin up multiple child processes. So you're gonna want to have essentially, I'll uncomment this. You're gonna wanna essentially have a while (true) loop, so that it basically runs forever until somebody stops the program.
[00:00:47] And inside your while (true) loop, you're gonna wanna have a for loop. And that for loop is gonna loop up to the number of MAX_CHILDREN, and for each iteration of it, it's going to spin off new child process, you're gonna spin off five, or 10, or 20 of these.
[00:01:05] But the trick is for each of these child processes, you need to listen to their exit code. You're not gonna be doing a console statement, but you need to be checking their exit code to see if it's 0. And only if all of the children that you just spun off in this particular iteration return a 0, should you mark that as a success.
[00:01:28] So for example, you can say process.stdout.write, trying.
>> Kyle Simpson: And then you're going to either print success with a consoled.log statement down here. You're gonna say, console.log("success") if all five or 10 of those were able to succeed, or a failure if one or more of them failed. And then you're gonna want to, and we have the delay function here, you're gonna want to do in a wait and delay by, let's just say every 500 milliseconds we'll retry against the server.
[00:02:21] So, your task is to figure out how do I set up this for loop so that it can keep making sets of these children, and how do I collect all of their results and figure out if all of them finished. And your hint is that we earlier on talked about a way of turning event handlers into promises.
[00:02:44] And if you were to collect every group of promises and then check to see if all those promises succeeded correctly, then you would be able to know if all of your children finished. So, we'll take about ten minutes or so on this. It's a little tricky to figure out the mechanics but try your hand at spinning off multiple children, and then we'll come back and talk about this solution.