nezzario

PidControl.php

Nov 12th, 2014
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.22 KB | None | 0 0
  1. <?php
  2.     namespace PidControl;
  3.  
  4.     class PidControl {
  5.         protected $maxRunSec;
  6.         protected $pidFile;
  7.  
  8.         function __construct($maxRunSec, $pidFile = false) {
  9.             $this->setMaxRunSec($maxRunSec);
  10.             if($pidFile === false) {
  11.                 $this->setPidFile(__FILE__ . '.pid');
  12.             } else {
  13.                 $this->setPidFile($pidFile);
  14.             }
  15.         }
  16.  
  17.         /*
  18.          * Use symlinking/atomic operations to prevent rare race conditions
  19.          */
  20.         public function writePid() {
  21.             if(is_dir(dirname($this->getPidFile())) && is_writable(dirname($this->getPidFile()))) {
  22.                 return file_put_contents($this->getPidFile(), time() . ';' . getmypid());
  23.             } else {
  24.                 throw new \Exception('PID File Not Writable: ' . $this->getPidFile());
  25.             }
  26.         }
  27.  
  28.         public function cleanupPid() {
  29.             unlink($this->getPidFile());
  30.         }
  31.  
  32.         public function start() {
  33.             if(!is_file($this->getPidFile())) {
  34.                 $this->writePid();
  35.                 return true;
  36.             } else {
  37.                 $pidData = file_get_contents($this->getPidFile());
  38.                 $pidPieces = explode(';', $pidData);
  39.  
  40.                 if(!empty($pidData) && count($pidPieces) === 2) {
  41.                     list($time, $existingPid) = $pidPieces;
  42.                     if(is_dir("/proc/" . $existingPid)) {
  43.                         throw new \Exception("PID File and /proc both say process id $existingPid is still running.  "
  44.                             . "PID file indicates it has been running since " . date('r', $time));
  45.                     } else {
  46.                         if(($time + $this->getMaxRunSec()) < time()) {
  47.                             $this->writePid();
  48.                             trigger_error("PID file exists but seems to be stale, bravely proceeding and updating PID "
  49.                                 . "file.", E_USER_WARNING);
  50.                             return true;
  51.                         } else {
  52.                             throw new \Exception("PID file exists, the PID no longer exists but I have not exceeded my "
  53.                                 . "timeout of " . $this->getMaxRunSec() . " seconds.");
  54.                         }
  55.                     }
  56.                 } else {
  57.                     throw new \Exception("PID file exists but isn't the proper format, I don't know what to do!");
  58.                 }
  59.             }
  60.             return false;
  61.         }
  62.  
  63.         public function end() {
  64.             $this->cleanupPid();
  65.         }
  66.  
  67.         public function setMaxRunSec($secs) {
  68.             $this->maxRunSec = $secs;
  69.         }
  70.  
  71.         public function getMaxRunSec() {
  72.             return $this->maxRunSec;
  73.         }
  74.  
  75.         public function setPidFile($file) {
  76.             $this->pidFile = $file;
  77.         }
  78.  
  79.         public function getPidFile() {
  80.             return $this->pidFile;
  81.         }
  82.     }
  83.  
  84.     // EXAMPLE USAGE:
  85.     $pidControl = new PidControl(3600);
  86.     try {
  87.         $pidControl->start();
  88.     } catch(Exception $e) {
  89.         mail('[email protected]', "PID File Issue", $e->getMessage());
  90.     }
  91.  
  92.     // CODE CODE
  93.  
  94.     $pidControl->end();
Advertisement
Add Comment
Please, Sign In to add comment