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

USB sync to drive: enhanced services_start / services_stop scripts to… #2

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
82 changes: 53 additions & 29 deletions lib/services_start.php
Original file line number Diff line number Diff line change
@@ -1,35 +1,59 @@
<?php if ($config['remotebuzzer_enabled']):
$connection = @fsockopen('127.0.0.1', $config['remotebuzzer_port']);
<?php

if (! is_resource($connection))
{
if ($config['dev'])
{
$logfile = $config['folders']['tmp']."/".$config['remotebuzzer_logfile'];
}
else
{ $logfile = "/dev/null"; }
require_once(__DIR__ . '/config.php');

print ("\t<!-- Remote Buzzer Enabled --- starting server -->\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<!-- Remote Buzzer Enabled --- server already started (port in use) -->\n");
}

?>
<script type="text/javascript" src="node_modules/socket.io-client/dist/socket.io.slim.js"></script>

<?php endif;

if ($config['synctodrive_enabled'] && !file_exists($config['folders']['tmp'].'/synctodrive_server.pid') {
if ($config['dev']) {
$logfile = $config['folders']['tmp']."/".$config['synctodrive_logfile'];
}
else { $logfile = "/dev/null"; }

print ("\t<!-- Sync To Drive enabled --- starting server -->\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<!-- Remote Buzzer enabled --- starting server -->\n");

proc_close(proc_open ($config['nodebin']['cmd']." resources/js/remotebuzzer_server.js 1>>".$logfile." 2>&1 &", array(), $foo));

} else {
print ("\t<!-- Remote Buzzer Enabled --- server already started (port in use) -->\n");
}

print("\t<script type=\"text/javascript\" src=\"node_modules/socket.io-client/dist/socket.io.slim.js\"></script>\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<!-- Sync To Drive enabled --- server already active -->\n");
}
else
{
print ("\t<!-- Sync To Drive enabled --- starting server -->\n");
proc_close(proc_open ($config['nodebin']['cmd']." resources/js/sync-to-drive.js 1>>".$logfile." 2>&1 &", array(), $foo));
}
}
?>
61 changes: 21 additions & 40 deletions lib/services_stop.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);

?>
2 changes: 1 addition & 1 deletion src/js/sync-to-drive.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down