Skip to content

Commit 120bd1c

Browse files
committed
Added a restart for hosting that kills user processes - merged from niutech#14
1 parent 520d661 commit 120bd1c

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

node.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
//define("ADMIN_MODE", true);
1111
define("ADMIN_MODE", false); //set to true to allow unsafe operations, set back to false when finished
1212

13+
//set to true for shared hosts that periodically kill user's processes (such as node)
14+
define("RESTART_PROCESS", false);
15+
1316
error_reporting(E_ALL);
1417
set_time_limit(120);
1518
define("NODE_VER", "v10.6.0");
@@ -90,7 +93,10 @@ function node_start($file) {
9093
echo "Starting: node $file\n";
9194
$node_pid = exec("PORT=" . NODE_PORT . " " . NODE_DIR . "/bin/node $file >nodeout 2>&1 & echo $!");
9295
echo $node_pid > 0 ? "Done. PID=$node_pid\n" : "Failed.\n";
93-
file_put_contents("nodepid", $node_pid, LOCK_EX);
96+
file_put_contents("nodepid", $node_pid, LOCK_EX);
97+
if($node_pid>0){
98+
file_put_contents('nodestart', $file, LOCK_EX);
99+
}
94100
sleep(1); //Wait for node to spin up
95101
echo file_get_contents("nodeout");
96102
}
@@ -130,6 +136,17 @@ function node_serve($path = "") {
130136
echo "Node.js is not yet installed. Switch to Admin Mode and <a href='?install'>Install it</a>.\n";
131137
node_foot();
132138
return;
139+
} elseif ($RESTART_PROCESS && $node_pid && !posix_getpgid($node_pid)) {
140+
$nodestart = file_get_contents('nodestart');
141+
if($nodestart){
142+
node_start($nodestart);
143+
//wait for node process to start, then retry to node_serve
144+
sleep(5);
145+
node_serve($path);
146+
return;
147+
}
148+
echo "Please switch to Admin Mode and manually restart the server. <a href='?start'>Start it</a>\n";
149+
return;
133150
}
134151
$node_pid = intval(file_get_contents("nodepid"));
135152
if($node_pid === 0) {

0 commit comments

Comments
 (0)