Reattaching to spawned process via nodejs
Asked Answered
H

2

9

I am creating a small proprietary game server manager in Node.js; currently it runs the game by spawning via child_process:

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

So long as the manager continues to run I can pipe commands and deal with the child as I would like. However, consider the possibility that my manager crashes or is closed. How would I then reattach to the previously spawned child process (which was still running while the manager was down)? I can store pidfiles to try and reconnect based on pid; but I'm not sure how to get a child_process object with access to the child's stdio objects.

I really would like this to be recoverable; any help is appreciated, thanks!


Please note: The game servers are proprietary, some examples are Minecraft, Source DS, etc. Assume i do not have access to the sever source.


EDIT

After reading some source code from node's child_process it looks like if you specify a property in the options called stdinStream, stdoutStream, or stderrStream it should just open a socket to it. (See lines 428 - 496). So the issue then is, how do I stop spawn from actually doing a spawn and instead just settings its values based on a specified pid and streams I pass. (I would get my stdinStream by doing fs.createWriteStream('/proc/PID/fd/0'); which should work since that fd is created as a pipe.)

Heydon answered 11/4, 2012 at 19:43 Comment(3)
hmm. I wonder if you could do that in ANY programming language, let alone js / node. If it were me, I'd take a look at using sockets to solve thisTummy
I pass through some commands via stdin of the child process, since I have little experience with *nix sockets how could I accomplish regaining a 'pipe' to the child's stdin?Heydon
Anyone have any ideas on this?Heydon
H
1

After speaking with some of the guys on the node project, it looks like the only way I can accomplish this is by dealing with the raw file descriptors in the proc fs. I will probably mock up something similar to a child_process object and create its streams like:

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');
Heydon answered 23/4, 2012 at 12:37 Comment(4)
Does this method still work? I can't seem to get it to work with Node v0.10. I am trying to make a Minecraft server admin and would like some insight if you have any. :)Weeping
I never got this to work 100% on 0.6 so I abandoned trying to do something like this unfortunately :/Heydon
Sad day, looks like I may have to follow suit.Weeping
Have either of you ever looked at this again? Reconnecting to child processes? @DominicBarnesHanrahan
S
1

To extend what someone said in a comment above, you may be able to use http://nodejs.org/api/net.html where each child process creates a server (net.createServer()) and you keep a list of what children are listening at what ports somewhere, and then when your master restarts, it goes and finds that list of children and connects to each their servers. The Sockets that you get from net.createConnection() replace the child_process objects in your master.

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 like you've been doing.

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

Sisera answered 11/4, 2012 at 22:43 Comment(4)
Unfortunately this would mean spinning up a node instance for each game server I would need to run, and if any of those children crash or are closed I'm in the exact same boat.Heydon
True, its a lot of new node instances, but I thought spawn did that too. I don't follow about being in the same boat if one crashes, of course you'd have to restart it, but this way you can reconnect with the existing stuff.Sisera
You are talking about have a master node proc spawn children node procs and communicate with them through sockets, which is fine and recoverable. But that child node proc would spawn its game server and communicate with it through stdin. So if that child crashes you can reconnect to a new child but that child cant reconnect to the game server, which is the entire problem. A game server can be a java process, a source dedicated server, anything really.Heydon
Aah, I was thinking pure-node. However, the net/socket idea might still work for the node-to-game communication if you're also writing the game code. If it must be stdin though, then my answer is pretty useless. It's a bit outside of my knowledge, but #5374755 might have some useful info.Sisera
H
1

After speaking with some of the guys on the node project, it looks like the only way I can accomplish this is by dealing with the raw file descriptors in the proc fs. I will probably mock up something similar to a child_process object and create its streams like:

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');
Heydon answered 23/4, 2012 at 12:37 Comment(4)
Does this method still work? I can't seem to get it to work with Node v0.10. I am trying to make a Minecraft server admin and would like some insight if you have any. :)Weeping
I never got this to work 100% on 0.6 so I abandoned trying to do something like this unfortunately :/Heydon
Sad day, looks like I may have to follow suit.Weeping
Have either of you ever looked at this again? Reconnecting to child processes? @DominicBarnesHanrahan

© 2022 - 2024 — McMap. All rights reserved.