Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if a child process is running

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.

like image 327
ste2425 Avatar asked Jan 21 '14 10:01

ste2425


1 Answers

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.

like image 161
Kamran Shahid Avatar answered Sep 19 '22 20:09

Kamran Shahid