Closed Dylankjy closed 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.
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
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
.
Hmm, why would the next action be process.end? I’ve started, stopped and restarted the server within the same process
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);
}
Perhaps you could start the process ID and check if the process is running
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.
Cross platform pid usage library. This should work.
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.
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
@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!
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
If you are using the module scriptserver-event
, the event stop
should fire when the server shuts down.
Alternatively you could also use the child process events on server.spawn
, like exit
and close
Ok, thanks @garrettjoecox & @ImBaedin
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?