Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a restart for hosting that kills user processes #14

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions node.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

define("ADMIN_MODE", false); //set to true to allow unsafe operations, set back to false when finished

//set to true for shared hosts that periodically kill user's processes (such as node)
define("RESTART_PROCESS", false);

define("NODE_VER", "v5.7.0");

define("NODE_ARCH", "x" . substr(php_uname("m"), -2)); //x86 or x64
Expand Down Expand Up @@ -72,6 +75,9 @@ function node_start($file) {
$node_pid = exec("PORT=" . NODE_PORT . " " . NODE_DIR . "/bin/node $file >nodeout 2>&1 & echo $!");
echo $node_pid > 0 ? "Done. PID=$node_pid\n" : "Failed.\n";
file_put_contents("nodepid", $node_pid, LOCK_EX);
if($node_pid>0){
file_put_contents('nodestart', $file, LOCK_EX);
}
sleep(1); //Wait for node to spin up
echo file_get_contents("nodeout");
}
Expand Down Expand Up @@ -118,6 +124,17 @@ function node_serve($path = "") {
echo "Node.js is not yet running. Switch to Admin Mode and <a href='?start'>Start it</a>\n";
node_foot();
return;
}elseif($RESTART_PROCESS && $node_pid && !posix_getpgid($node_pid)){
$nodestart = file_get_contents('nodestart');
if($nodestart){
node_start($nodestart);
//wait for node process to start, then retry to node_serve
sleep(5);
node_serve($path);
return;
}
echo "Please switch to Admin Mode and manually restart the server. <a href='?start'>Start it</a>\n";
return;
}
$curl = curl_init("http://127.0.0.1:" . NODE_PORT . "/$path");
curl_setopt($curl, CURLOPT_HEADER, 1);
Expand Down