garrettjoecox / scriptserver

A Minecraft server wrapper, allows for simple plugins in vanilla
GNU General Public License v3.0
68 stars 10 forks source link

Is there a way to detect whether a server has stopped? #26

Closed Dylankjy closed 6 years ago

Dylankjy commented 6 years ago

The title says it all. Is there a proper way to detect whether the server has stopped such as when the /stop command has been sent and that the server is no longer active?

garrettjoecox commented 6 years ago

Hmm, with the current implementation the best solution would be to use scriptserver-event, and listen for the start and stop events and keep track of it with a variable.

It would be nice to have this in the core, but I kind of like having the events module separated, not built into the core unless someone doesnt desire that functionality.

garrettjoecox commented 6 years ago

I suppose one solution would be to add it to the scriptserver-event module’s functionality.. but I’m not sure if that makes a whole lot sense as far as what that module should accomplish

Dylankjy commented 6 years ago

There's a problem with this. Because if I were to detect for a /stop, there would be no delay to allow the server to stop properly would just close node since the next action is process.end.

garrettjoecox commented 6 years ago

Hmm, why would the next action be process.end? I’ve started, stopped and restarted the server within the same process

Dylankjy commented 6 years ago

This is a section of code that will be executed when Ctrl + C is pressed. Basically to handle the exit of the app. Currently when I try to do a server.send('/stop'), it would stop the server and afterwards process.exit(0). So is there a way to actually delay the process.exit(0)? Such as maybe a callback from ScriptServer saying that it has completely stopped.

function exitHandler(options, err) {
    < insert server stop code here to stop server>
    console.log('\n\x1b[32m\x1b[1mShutting down Conflux Daemon\n\x1b[0m\n')
    process.exit(0);
}
ImBaedin commented 6 years ago

Perhaps you could start the process ID and check if the process is running

Dylankjy commented 6 years ago

I don't really think that good for cross compatibility since my app is meant to be able to be used on more than 1 operating system.

ImBaedin commented 6 years ago

Cross platform pid usage library. This should work.

garrettjoecox commented 6 years ago

I’m not sure I follow, if you don’t want the process to exit after the server stops, don’t run “process.exit”, as that seems to be in your code, not within the functionality of scriptserver.

garrettjoecox commented 6 years ago

If what you are trying to achieve is the server stopping but not the process closing on ctrl + C i would not recommend doing that, as it’s a normal practice to completely exit the process upon ctrl+c

Dylankjy commented 6 years ago

@ImBaedin Could you advise me on how should I detect the pid of the Minecraft Server? Is there a code snippet you could provide? Thanks!

ImBaedin commented 6 years ago
const server = new ScriptServer({
    core: {
        jar: "server.jar",
        args: ["-Xmx2G"],
        rcon: {
            port: "25575",
            password: "password"
        },
        spawnOpts: {
            cwd: "./server/"
        }
    }
});

 setInterval(function(){
     if(server.spawn){
         pidusage(server.spawn.pid, function(err, stats){
             console.log('memory usage: '+Math.floor(stats.memory/1024/1024)+'MB');
             console.log('uptime: '+Math.floor(stats.elapsed/1000));
         });
     }
 },1000);

This will spawn a server and output its usage every second. But accessing the pid of the server is as easy as server.spawn.pid

garrettjoecox commented 6 years ago

If you are using the module scriptserver-event, the event stop should fire when the server shuts down.

garrettjoecox commented 6 years ago

Alternatively you could also use the child process events on server.spawn, like exit and close

Dylankjy commented 6 years ago

Ok, thanks @garrettjoecox & @ImBaedin