开发者

NodeJS: how to run three servers acting as one single application?

My application is built with three distincts servers: each one of them serves a different purpose and they must stay separated (at least, for using more than one core). As an example (this is not the real thing) you could think about this set up as one server managing user authentication, another one serving as the game engine, another one as a pubsub server. Logically the "application" is only one and clients connect to one or another server depending on their specific need.

Now I'm trying to figure out the best way to run a setup like this in a production environment.

The simplest way could be to have a bash script that would run each server in background one after the other. One problem with this approach would be that in the case I need to restart the "application", I should have saved each server's pid and kill each one.

Another way would be to use a node process that would run 开发者_运维知识库each servers as its own child (using child_process.spawn). Node spawning nodes. Is that stupid for some reason? This way I'd have a single process to kill when I need to stop/restart the whole application.

What do you think?


If you're on Linux or another *nix OS, you might try writing an init script that start/stop/restart your application. here's an example.


Use specific tools for process monitoring. Monit for example can monitor your processes by their pid and restart them whenever they die, and you can manually restart each process with the monit-cmd or with their web-gui.

So in your example you would create 3 independent processes and tell monit to monitor each of them.


I ended up creating a wrapper/supervisor script in Node that uses child_process.spawn to execute all three processes.

  • it pipes each process stdout/stderr to the its stdout/stderr
  • it intercepts errors of each process, logs them, then exit (as it were its fault)
  • It then forks and daemonize itself

I can stop the whole thing using the start/stop paradigm.

Now that I have a robust daemon, I can create a unix script to start/stop it on boot/shutdown as usual (as @Levi says)

See also my other (related) Q: NodeJS: will this code run multi-core or not?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜