From 934a7dda58c003d30fe0431e9bf15d8fc0d654e0 Mon Sep 17 00:00:00 2001 From: jacques42 Date: Wed, 14 Oct 2020 22:21:43 +0100 Subject: [PATCH] USB sync to drive: enhanced services_start / services_stop scripts to be more robust --- lib/services_start.php | 82 ++++++++++++++++++++++++++--------------- lib/services_stop.php | 61 +++++++++++------------------- src/js/sync-to-drive.js | 2 +- 3 files changed, 75 insertions(+), 70 deletions(-) diff --git a/lib/services_start.php b/lib/services_start.php index b889b6bed..8421239cf 100644 --- a/lib/services_start.php +++ b/lib/services_start.php @@ -1,35 +1,59 @@ -\n"); +function processIsRunning ($pName, $pidFile) { + if (file_exists($pidFile)) + { + exec("pgrep -F ".$pidFile, $output, $return); + if ($return == 0) { return true; } // process is active + unlink ($pidFile); // remove stale PID file + } - proc_close(proc_open ($config['nodebin']['cmd']." resources/js/remotebuzzer_server.js 1>>".$logfile." 2>&1 &", array(), $foo)); + exec("pgrep -a -f ".$pName, $output, $return); + return count($output)-1 ? true : false ; // true if process is active +} - } else { - print ("\t\n"); - } -?> - - -\n"); - proc_close(proc_open ($config['nodebin']['cmd']." resources/js/sync-to-drive.js 1>>".$logfile." 2>&1 &", array(), $foo)); - } +if ($config['remotebuzzer_enabled']) { + $connection = @fsockopen('127.0.0.1', $config['remotebuzzer_port']); + + if (! is_resource($connection)) + { + if ($config['dev']) + { + $logfile = $config['foldersAbs']['tmp']."/".$config['remotebuzzer_logfile']; + } + else + { $logfile = "/dev/null"; } + + print ("\t\n"); + + proc_close(proc_open ($config['nodebin']['cmd']." resources/js/remotebuzzer_server.js 1>>".$logfile." 2>&1 &", array(), $foo)); + + } else { + print ("\t\n"); + } + + print("\t\n"); +} + +if ($config['synctodrive_enabled']) { + if ($config['dev']) { + $logfile = $config['foldersAbs']['tmp']."/".$config['synctodrive_logfile']; + } + else { + $logfile = "/dev/null"; + } + + if ( processIsRunning("sync-to-drive.js",$config['foldersAbs']['tmp'].'/synctodrive_server.pid')) + { + print ("\t\n"); + } + else + { + print ("\t\n"); + proc_close(proc_open ($config['nodebin']['cmd']." resources/js/sync-to-drive.js 1>>".$logfile." 2>&1 &", array(), $foo)); + } +} ?> diff --git a/lib/services_stop.php b/lib/services_stop.php index 9e153ec77..739aa8399 100644 --- a/lib/services_stop.php +++ b/lib/services_stop.php @@ -5,48 +5,29 @@ function killProcessIfActive($pName, $pidFile, $logfileName) { - global $config; - - if (file_exists($pidFile)) - { - $myfile = fopen($pidFile, "r"); - $procPID = fread($myfile,100); - fclose($myfile); - - posix_kill($procPID, 9); - - unlink ($pidFile); - - if ($config['dev']) - { - $logfile = $config['folders']['tmp']."/".$logfileName; - $fp = fopen("../".$logfile, 'a');//opens file in append mode. - fwrite($fp, "Service Control [ config ]: Photobooth config has changed, kill existing $pName process (PID ".$procPID.") and remove PID file\n"); - fclose($fp); - } - } - else - { - exec("pgrep -f ".$pName,$pids); - - if (count($pids) > 1) { - foreach ($pids as $procPID) { - if ($config['dev']) - { - $logfile = $config['folders']['tmp']."/".$logfileName; - $fp = fopen("../".$logfile, 'a');//opens file in append mode. - fwrite($fp, "Service Control [ config ]: Photobooth config has changed, killed processes by name ".$pName." -> ".$procPID."\n"); - fclose($fp); - } - - posix_kill($procPID, 9); - } - } - - } -} + global $config; + + exec("pgrep -f ".$pName,$pids); + + if (count($pids) > 1) { + foreach ($pids as $procPID) { + if ($config['dev']) + { + $logfile = $config['foldersAbs']['tmp']."/".$logfileName; + $fp = fopen($logfile, 'a');//opens file in append mode. + fwrite($fp, "Service Control [ config ]: Photobooth config has changed, killed processes by name ".$pName." -> PID ".$procPID."\n"); + fclose($fp); + } + + posix_kill($procPID, 9); + } + } + if (file_exists($pidFile)) { unlink ($pidFile); } + +} killProcessIfActive('remotebuzzer_server.js','../'.$config['folders']['tmp'].'/remotebuzzer_server.pid',$config['remotebuzzer_logfile']); killProcessIfActive('sync-to-drive.js','../'.$config['folders']['tmp'].'/synctodrive_server.pid',$config['synctodrive_logfile']); + ?> diff --git a/src/js/sync-to-drive.js b/src/js/sync-to-drive.js index 9a21da844..a7116f088 100755 --- a/src/js/sync-to-drive.js +++ b/src/js/sync-to-drive.js @@ -182,7 +182,7 @@ const isProcessRunning = (processName) => { const writePIDFile = (filename) => { try { - fs.writeFileSync(filename, PID, {mode: 'wx'}); + fs.writeFileSync(filename, PID, {flag: 'w'}); log(`PID file created [${filename}]`); } catch (err) { throw new Error(`Unable to write PID file [${filename}] - ${err.message}`);