|
10 | 10 | //define("ADMIN_MODE", true); |
11 | 11 | define("ADMIN_MODE", false); //set to true to allow unsafe operations, set back to false when finished |
12 | 12 |
|
| 13 | +//set to true for shared hosts that periodically kill user's processes (such as node) |
| 14 | +define("RESTART_PROCESS", false); |
| 15 | + |
13 | 16 | error_reporting(E_ALL); |
14 | 17 | set_time_limit(120); |
15 | 18 | define("NODE_VER", "v10.6.0"); |
@@ -90,7 +93,10 @@ function node_start($file) { |
90 | 93 | echo "Starting: node $file\n"; |
91 | 94 | $node_pid = exec("PORT=" . NODE_PORT . " " . NODE_DIR . "/bin/node $file >nodeout 2>&1 & echo $!"); |
92 | 95 | 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 | + } |
94 | 100 | sleep(1); //Wait for node to spin up |
95 | 101 | echo file_get_contents("nodeout"); |
96 | 102 | } |
@@ -130,6 +136,17 @@ function node_serve($path = "") { |
130 | 136 | echo "Node.js is not yet installed. Switch to Admin Mode and <a href='?install'>Install it</a>.\n"; |
131 | 137 | node_foot(); |
132 | 138 | 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; |
133 | 150 | } |
134 | 151 | $node_pid = intval(file_get_contents("nodepid")); |
135 | 152 | if($node_pid === 0) { |
|
0 commit comments