From d34f884d32fb3c80b5b83b1ceecd893f0ac48cbd Mon Sep 17 00:00:00 2001 From: botanic Date: Fri, 5 Sep 2014 19:38:53 -0700 Subject: [PATCH] fixed sync to be more robust --- code/web/private_php/ams/autoload/sync.php | 39 ++++++++++++++++++++-- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/code/web/private_php/ams/autoload/sync.php b/code/web/private_php/ams/autoload/sync.php index 2aaf9b069..a3bf6fdb2 100644 --- a/code/web/private_php/ams/autoload/sync.php +++ b/code/web/private_php/ams/autoload/sync.php @@ -7,6 +7,11 @@ */ class Sync{ + const OS_UNKNOWN = 1; + const OS_WIN = 2; + const OS_LINUX = 3; + const OS_OSX = 4; + /** * performs the actions listed in the querycache. * All entries in the querycache will be read and performed depending on their type. @@ -24,10 +29,10 @@ class Sync{ if(isset($pid) and function_exists('pcntl_fork') ) { // We're the main process. } else { - if(!file_exists($pidfile)) { - $pid = getmypid(); + $pid = getmypid(); + if(!file_exists($pidfile) or (file_exists($pidfile) && Sync::check_pid(file_get_contents($pid)))) { $file = fopen($pidfile, 'w+'); - if (!$file) + if (!$file) { echo $pidfile.' is not writeable.'; error_log($pidfile.' is not writeable.'); throw new SystemExit(); @@ -94,4 +99,32 @@ class Sync{ } } + + public static function check_pid($pid){ + + $OS = Sync::getOS(); + + if ($OS == 2) { + $processes = explode( "\n", shell_exec( "tasklist.exe" )); + foreach( $processes as $process ) + { + if( strpos( "Image Name", $process ) === 0 + || strpos( "===", $process ) === 0 ) + continue; + $matches = false; + preg_match( "/(.*?)\s+(\d+).*$/", $process, $matches ); + $pid = $matches[ 2 ]; + } + } else { + return file_exists( "/proc/$pid" ); + } + } + static public function getOS() { + switch (true) { + case stristr(PHP_OS, 'DAR'): return self::OS_OSX; + case stristr(PHP_OS, 'WIN'): return self::OS_WIN; + case stristr(PHP_OS, 'LINUX'): return self::OS_LINUX; + default : return self::OS_UNKNOWN; + } + } }