Re-bind to spawned process via nodejs

I create a small game server manager in Node.js; he is currently launching the game by spawning through child_process :

 var server = spawn(cmd, args, { cwd: 'something' }); 

While the manager continues to work, I can execute commands and process child elements as I would like. However, keep in mind that my manager crashes or is closed. How would I then reconnect to the previously spawned child process (which still worked while the manager was not working)? I can store pidfiles to try connecting again based on pid; but I'm not sure how to get a child_process object with access to stdio child objects.

I really wish it were possible; any help is appreciated, thanks!


Please Note :: Game servers are property, some examples are Minecraft, Source DS, etc. Suppose I do not have access to the source.


EDIT

After reading some source code from node child_process , it looks like if you specify a property in the parameters with the name stdinStream , stdoutStream or stderrStream , it should just open the socket for it. (See Lines 428 - 496 ). So the problem is, how can I stop spawning from actually creating caviar and instead, it just sets its values ​​based on the given pid and the streams that I transmit. (I would get stdinStream by running fs.createWriteStream('/proc/PID/fd/0'); which should work, since fd is created as a channel.)

+7
source share
2 answers

After talking with some of the guys on the node project, it seems like I can do this by dealing with file descriptors in proc fs. I will probably mock up something similar to a child_process object and create its threads, for example:

 var child = {}; child.stdin = fs.createWriteStream('/proc/PID/fd/0'); child.stdout = fs.createReadStream('/proc/PID/fd/1'); child.stderr = fs.createReadStream('/proc/PID/fd/2'); 
0
source

To expand on what someone said in the comment above, you can use http://nodejs.org/api/net.html , where each child process creates a server ( net.createServer() ), and you save a list of what the children listen on in which ports, and then when your wizard reboots, he goes and finds this list of children and connects to each of his servers. Sockets , which you get from net.createConnection() , replace the child_process objects on your host.

net servers and sockets implement the same Readable and Writable Stream interfaces as stdio , so after setting up and connecting, you should be able to write(...) and pipe() events you made.

This may not be the best solution, but I think it will work.

+1
source

All Articles