Вопрос

To make downtime's caused by long lasting app-reboots less painful, I thought of something like:

  1. Main App #1 on Port 80.
  2. Fail-over App #2 on Port 80 too, but answer requests only in case Application #1 isn't working.
  3. Let App #2 serve a 'maintaining' message for active users.

Running two processes on the same port ends up in an Error: EADDRINUSE - so the simple way isn't working. I stumbled upon the server.on('error') event and decided to let App #2 wait until App #1 might stop, so the port becomes available:

function tryPitchIn(){
     var server = http.createServer(app);

     server.on('listening', function(){
        console.log('Application #1 crashed/ended');
        console.log('Pitching in...');
     });

     server.on('error', function(){
        console.log('noting to do');
        setTimeout(tryPitchIn, 250);
     });

     server.listen(80);
}

tryPitchIn();

Although the above is working great, I have to struggle with ending App #2 on the initialization of App #1, which isn't easy to do on different operating systems.

Is it possible to give a node process (inited by npm start) an static ID to terminate it from another process - preferably cross os? Or other ideas for the scenario?

Это было полезно?

Решение

You can serve your App #1 on another port, and write a microapp that will proxy requests to it, and return something else if the request to App #1 fails. You can use node-http-proxy like here, or roll your own solution like this and just adding an "on error" clause.

Другие советы

I like Amadan's answer. On top of that I have to mention that doing much other than logging in .on('error') (and also for unchaughtException) is not recommended. The system could be in a volatile state (internal states of modules could be inconsistent, for example), and you don't want to keep running for long like that.

Either do what Amadan says, or use node.js domains, or use several processes behind a load balancer (which is basically what Amadan says).

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top