/** * Проверка PID-файла перед запуском Мастер-процесса. * * @throws Exceptions\PID_Open_Fail_Exception * @throws Exceptions\PID_Read_Fail_Exception * @throws Exceptions\PID_Lock_Fail_Exception * @throws Exceptions\PID_Unlink_Fail_Exception * @return void * @access public * @static */ public static function PreLock() { // поиск файла - не должно быть if (file_exists(PINGER_PIDFILE)) { // файл существует // попытка открыть для чтения // ожидается успешное открытие как признак мусорного файла static::$handle = @fopen(PINGER_PIDFILE, 'rb'); if (!static::$handle) { throw new Exceptions\PID_Open_Fail_Exception(); } // успешно открыт для чтения // попытка заблокировать // ожидается успешная блокировка как признак мусорного файла $l = @flock(static::$handle, LOCK_EX | LOCK_NB); if (!$l) { // блокировка не удалась // возможно существует запущенный процесс // попытка прочитать идентификатор процесса @flock(static::$handle, LOCK_UN); $pid = @fread(static::$handle, 8); if (false === $pid) { // попытка чтения не удалась throw new Exceptions\PID_Read_Fail_Exception(); } // процесс существует static::$pid = (int) $pid; throw new Exceptions\PID_Lock_Fail_Exception(); } // успешно открыт для чтения и заблокирован // признается мусором и удаляется @flock(static::$handle, LOCK_UN); @fclose(static::$handle); $u = @unlink(PINGER_PIDFILE); if (!$u) { // попытка удаления не удалась throw new Exceptions\PID_Unlink_Fail_Exception(); } } }
/** * разбираемся с pid-файлом */ public static function getPid() { try { Helper::checkFile(static::$pidfile, true); if (!file_exists(static::$pidfile) && !touch(static::$pidfile)) { throw new \Exception("Failed to create file '" . static::$pidfile . "'"); } } catch (\Exception $e) { static::throwException(sprintf("Failed to create or find pid-file: %s", $e->getMessage()), Logger::L_FATAL); } static::$pid = (int) file_get_contents(static::$pidfile); }