Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node js get and set data from different process

What you need is to listen for the first response on your proxy and look at its status code to determine whether your app started successfully or not. Here's how you do that:

proxy.on('proxyRes', function (proxyRes, req, res) {
  // Your target app is definitely up and running now because it just sent a response;
  // Use the status code now to determine whether the app started successfully or not
  var status = res.statusCode;
});

Hope this helps.


Not sure if it make sense , In your Main App the experience should start with a html page and each child process should have is own loader.

So basically , you need a http Handler, which linger the request until the the child process is ready. So just make and ajax call from the html , and show loading animation till the service is ready .

//Ajax call for each process  and update the UI accordingly   
$.get('/services/status/100').then(function(resp) {
   $('#service-100').html(resp.responseText);
})

//server side code (express syntax)
app.get('/services/status/:id ', function(req,res) {
     // Check if service is ready 
     serviceManager.isReady(req.params.id, function(err, serviceStats) {
         if(err) {
            //do logic err here , maybe notify the ui if an error occurred
            res.send(err);
            return;
         }
         // notify the ui , that the service is ready to run , and hide loader
         res.send(serviceStats);
     });
})

I am not sure i understand the question correctly, but you want to wait for a child process to spin on request and you want this request to wait for this child process and then be send to it? If that is so a simple solution will be to use something like this

    var count = 0;//Counter to check
    var maxDelay = 45;//In Seconds
    var checkEverySeconds = 1;//In seconds
    async.whilst(
        function () {
            return count < maxDelay;
        },
        function (callback) {
            count++;
            self.getAppStatus(apiKey, function (err, status) {
                if (status != 200) {
                    return setTimeout(callback, checkEverySeconds * 1000);

                } 
                continueWithRequest();

            });
        },
        function (err) {
            if (err) {
                return continueWithRequest(new Error('process cannot spin!'));
            }
        }
    );

The function continueWithRequest() will forward the request to the child process and the getAppStatus will return 200 when the child process has started and some other code when it is not. The general idea is that whilst will check every second if the process is running and after 45 second if not it will return an error. Waiting time and check intervals can be easily adjusted. This is a bit crude, but will work for delaying the request and setTimeout will start a new stack and will not block. Hope this helps.


If you know (around) how much time the app takes to get up and running, simply add setTimeout(proxyRequest, <Time it takes the app to start in MS>)

(There are most likely more smart/complicated solutions, but this one is the easiest.)