From 71943c85dbd50d6d505feb3d4edcf5ccf8efc9c4 Mon Sep 17 00:00:00 2001 From: Sustenance Date: Mon, 29 Feb 2016 22:17:37 -0500 Subject: [PATCH 1/3] Added a restart for hosting that kills user processes --- node.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/node.php b/node.php index 84f2ebc..2e850d1 100644 --- a/node.php +++ b/node.php @@ -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 @@ -114,6 +117,14 @@ function node_serve($path = "") { } $node_pid = intval(file_get_contents("nodepid")); if($node_pid === 0) { + $nodestart = file_get_contents('nodestart'); + if(($RESTART_PROCESS === true) && !is_null($nodestart)){ + node_start($nodestart); + //wait for node process to start, then retry to node_serve + sleep(5); + node_serve($path); + return; + } node_head(); echo "Node.js is not yet running. Switch to Admin Mode and Start it\n"; node_foot(); @@ -164,6 +175,7 @@ function node_dispatch() { } elseif(isset($_GET['uninstall'])) { node_uninstall(); } elseif(isset($_GET['start'])) { + file_put_contents('nodestart', $_GET['start']); node_start($_GET['start']); } elseif(isset($_GET['stop'])) { node_stop(); From ce745dbafef9f02c16af399de93d22fdaf1b5ebb Mon Sep 17 00:00:00 2001 From: Sustenance Date: Tue, 1 Mar 2016 20:08:05 -0500 Subject: [PATCH 2/3] Check if PID is actually running. --- node.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/node.php b/node.php index 2e850d1..083de05 100644 --- a/node.php +++ b/node.php @@ -117,17 +117,20 @@ function node_serve($path = "") { } $node_pid = intval(file_get_contents("nodepid")); if($node_pid === 0) { + node_head(); + echo "Node.js is not yet running. Switch to Admin Mode and Start it\n"; + node_foot(); + return; + }elseif($RESTART_PROCESS && $node_pid && !posix_getpgid($node_pid)){ $nodestart = file_get_contents('nodestart'); - if(($RESTART_PROCESS === true) && !is_null($nodestart)){ + if($nodestart){ node_start($nodestart); //wait for node process to start, then retry to node_serve sleep(5); node_serve($path); return; } - node_head(); - echo "Node.js is not yet running. Switch to Admin Mode and Start it\n"; - node_foot(); + echo "Please switch to Admin Mode and manually restart the server. Start it\n"; return; } $curl = curl_init("http://127.0.0.1:" . NODE_PORT . "/$path"); From 14a8abfef7dcf5efbea24ac7eeba3580316d1bf2 Mon Sep 17 00:00:00 2001 From: Sustenance Date: Tue, 1 Mar 2016 20:13:59 -0500 Subject: [PATCH 3/3] Move file save to within node_start method, out of routing ladder --- node.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/node.php b/node.php index 083de05..8ebf14d 100644 --- a/node.php +++ b/node.php @@ -75,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"); } @@ -178,7 +181,6 @@ function node_dispatch() { } elseif(isset($_GET['uninstall'])) { node_uninstall(); } elseif(isset($_GET['start'])) { - file_put_contents('nodestart', $_GET['start']); node_start($_GET['start']); } elseif(isset($_GET['stop'])) { node_stop();