1
0
Fork 0
forked from svrjs/svrjs

Make SVR.JS gracefully exit on "stop" command.

This commit is contained in:
Dorian Niemiec 2023-08-10 01:14:10 +02:00
parent 08cc0ac042
commit 3d4acae311
3 changed files with 61 additions and 14 deletions

View file

@ -3,7 +3,7 @@
"port": 80, "port": 80,
"pubport": 80, "pubport": 80,
"page404": "404.html", "page404": "404.html",
"timestamp": 1691613309162, "timestamp": 1691622823922,
"blacklist": [], "blacklist": [],
"nonStandardCodes": [], "nonStandardCodes": [],
"enableCompression": true, "enableCompression": true,

View file

@ -12,7 +12,7 @@
</style> </style>
</head> </head>
<body> <body>
<h1>Welcome to SVR.JS Nightly-GitMaster</h1> <h1>Welcome to SVR.JS Nightly-GitMain</h1>
<div style="background-color: #ff0; border-color: #f70; border-width: 5px; border-style: solid; padding: 5px; display: inline-block;"> <div style="background-color: #ff0; border-color: #f70; border-width: 5px; border-style: solid; padding: 5px; display: inline-block;">
<b style="font-size: 20pt">WARNING!</b><br/> <b style="font-size: 20pt">WARNING!</b><br/>
This version is only for test purposes and may be unstable. This version is only for test purposes and may be unstable.

71
svr.js
View file

@ -4772,9 +4772,13 @@ function start(init) {
var commands = { var commands = {
close: function () { close: function () {
try { try {
server.close(); if(server.listening || server2.listening) {
if (secure && !disableNonEncryptedServer) { server.close();
server2.close(); if (secure && !disableNonEncryptedServer) {
server2.close();
}
} else {
throw new Error("Server is already closed.");
} }
if (cluster.isPrimary === undefined) serverconsole.climessage("Server closed."); if (cluster.isPrimary === undefined) serverconsole.climessage("Server closed.");
else { else {
@ -4788,11 +4792,15 @@ function start(init) {
}, },
open: function () { open: function () {
try { try {
if (secure) { if(!server.listening) {
server.listen(sport); if (secure) {
if (!disableNonEncryptedServer) server2.listen(port); server.listen(sport);
if (!disableNonEncryptedServer) server2.listen(port);
} else {
server.listen(port); // Reopen Server
}
} else { } else {
server.listen(port); // Reopen Server throw new Error("Server is already opened.");
} }
if (cluster.isPrimary === undefined) serverconsole.climessage("Server opened."); if (cluster.isPrimary === undefined) serverconsole.climessage("Server opened.");
else { else {
@ -4821,10 +4829,46 @@ function start(init) {
}, },
stop: function (retcode) { stop: function (retcode) {
reallyExiting = true; reallyExiting = true;
if (typeof retcode == "number") { if((!cluster.isPrimary && cluster.isPrimary !== undefined) && server.listening) {
process.exit(retcode); try {
server.close(function() {
if(server2.listening) {
try {
server2.close(function() {
if (typeof retcode == "number") {
process.exit(retcode);
} else {
process.exit(0);
}
});
} catch(err) {
if (typeof retcode == "number") {
process.exit(retcode);
} else {
process.exit(0);
}
}
} else {
if (typeof retcode == "number") {
process.exit(retcode);
} else {
process.exit(0);
}
}
});
} catch(err) {
if (typeof retcode == "number") {
process.exit(retcode);
} else {
process.exit(0);
}
}
} else { } else {
process.exit(0); if (typeof retcode == "number") {
process.exit(retcode);
} else {
process.exit(0);
}
} }
}, },
clear: function () { clear: function () {
@ -4915,8 +4959,11 @@ function start(init) {
} }
process.send("\x12END"); process.send("\x12END");
} else if (line == "\x14KILLPING") { } else if (line == "\x14KILLPING") {
process.send("\x12KILLOK"); if(!reallyExiting) {
process.send("\x12END"); process.send("\x12KILLOK");
process.send("\x12END");
}
// Refuse to send, when it's really exiting. Main process will treat the worker as hung up anyway...
} else if (line == "\x14KILLREQ") { } else if (line == "\x14KILLREQ") {
if(reqcounter - reqcounterKillReq < 2) { if(reqcounter - reqcounterKillReq < 2) {
process.send("\x12KILLTERMMSG"); process.send("\x12KILLTERMMSG");