Checking if a child process is running
Asked Answered
A

1

9

I've built a portal, one of the pages lists releases from Octopus Deploy. However, some information needed about each release is held inside its zip in a data store (shared drive).

I currently get releases from Octopus's API then check a mongo db where ve stored information from the zip files ive already accessed so we only have to read the zip files once, the first time they are created.

The logic for this currently all works however when it's reading the zip files for the first time it is completely blocking. So ive put it within a child process which runs and works great.

Currently, the logic for all of this runs every time a user hits the release page. If all results from octopus deploy are in the database then it just returns those results. If not it'll return the results it has then used socket io to return the information read from the zip files once the child process has inserted it into the database.

Now the actual problem/question. As its run every time the user hits the page if user A hits the page and gets a release from octopus that is not in the database it will start the child process to read the zip. If user B hits the page before user A's child process has finished they will also get a release from octopus that is not in the database and so will try and hit the child process to do the same thing as user A. However because the code to read the zip file is blocking it'll wait until all the zip files are read from user A then repeat the proccess for user B.

How would I check if the child process is already performing the zip reading and if it isn't then executed if it is then do something else?

Ive tried setting a 'inuse' variable and checking it when trying to perform the read zips function in the child process and also in the module that calls it. Trouble is in the child process the zip reading functions blocks it, once it's complete sets the in-use variable to false and so the queued call runs the same thing. In the module its loaded every time the user loads the page so its reset to the default value of false so the read zip function is always called.

Antevert answered 21/1, 2014 at 10:16 Comment(5)
check exit event listener. nodejs.org/api/child_process.html#child_process_event_exitKarissakarita
@KamranShahid Isn't that fired when the child process completes? How would that work in the case where user A started the child proccess. The 'read zip files' function is in progress. Then user B hits the page and wants to find if the 'read zip files' function is in progress?Antevert
Some sort of global object to maintain the state. Like we set a flag when we start the child and set it to off when ext event firesKarissakarita
@KamranShahid That's perfect, exactly what I did, created a higher scoped object which contained running information and a few other bits about the current zips being read etc. Thanks.Antevert
Great. I am adding this in answer.please accept it as answer so that it might also help othersKarissakarita
K
9

check exit event listener. http://nodejs.org/api/child_process.html#child_process_event_exit

Then you make some logic as some sort of global object to maintain the state. Like we set a flag when we start the child and set it to off when ext event fires.

Karissakarita answered 23/1, 2014 at 11:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.