123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- <?php
- namespace KIF\Cli;
- use KIF\Verify;
- use Exception;
- use KIF\Core\Config;
- class SingleProcess {
-
- private $pid;
-
- private $timeout;
-
- private $checkSystemProcess;
- public function __construct($pid, $timeout = 600, $checkSystemProcess = false) {
- if (!self::isValidPid($pid)) {
- throw new Exception('invalid $pid');
- }
- if (!Verify::int($timeout) || $timeout < 1) {
- throw new Exception('invalid $lifetime');
- }
- if (!is_bool($checkSystemProcess)) {
- throw new Exception('invalid $checkSystemProcess');
- }
- $this->pid = $pid;
- $this->timeout = (int) $timeout;
- $this->checkSystemProcess = $checkSystemProcess;
- }
-
- public function isRun() {
- $pf = $this->getPidPath();
-
- if (!file_exists( $pf )) {
- return false;
- }
-
- if ($this->isTimeout()) {
- $this->kill();
- return false;
- }
-
- if ($this->checkSystemProcess) {
- $spid = $this->getSystemPid();
- if (!self::existSystemProcess($spid)) {
- return false;
- }
- }
- return true;
- }
-
- public function run() {
- file_put_contents($this->getPidPath(), getmypid());
- return true;
- }
-
- public function active($activeFrequency = 300) {
- static $time_prev = null;
- if (is_null($time_prev)) {
- $time_prev = time();
- }
- $time_now = time();
-
- if ($time_now - $time_prev >= $activeFrequency) {
- $time_prev = $time_now;
- $this->run();
- return true;
- }
- return false;
- }
-
- public function complete() {
- @unlink($this->getPidPath());
- return true;
- }
-
- public function kill() {
- $pf = $this->getPidPath();
- if (!file_exists($pf)) {
- return true;
- }
- if ($this->checkSystemProcess) {
- $spid = $this->getSystemPid();
- if ($spid) {
- exec("kill -9 {$spid}");
- }
- }
- @unlink($pf);
- sleep(1);
- return true;
- }
-
- static public function cleanup($checkSystemProcess = false) {
- $path = self::getPath().'*';
- foreach (glob($path) as $filename) {
- if ($checkSystemProcess) {
- $spid = file_get_contents($filename);
- if (self::existSystemProcess($spid)) {
- exec("kill -9 {$spid}");
- }
- }
- @unlink($filename);
- }
- sleep(10);
- return true;
- }
-
- static private function getPath() {
-
- $logPath = Config::getInstance ()->get ( 'Log_Path' );
-
- if(empty($logPath)){
- throw new Exception('Config Log_Path is empty');
- }
-
- $path = $logPath . DS . 'sspfiles' . DS;
- if (!file_exists($path)) {
- if (!mkdir($path, 0777, true)) {
- return false;
- }
- }
- return $path;
- }
-
- private function isTimeout() {
- if ((time() - filemtime($this->getPidPath())) > $this->timeout) {
- return true;
- }
- return false;
- }
-
- private function getPidPath() {
- return self::getPath() . md5($this->pid);
- }
-
- private function getSystemPid() {
- $spid = @file_get_contents($this->getPidPath());
- if ($spid) {
- return $spid;
- }
- return false;
- }
-
- static private function existSystemProcess($spid) {
- if (!Verify::int($spid) || $spid < 1) {
- return false;
- }
- $cmd = 'ps -eo pid | egrep ^\ *'.$spid.'$';
- $out = shell_exec($cmd);
- return empty($out) ? false : true;
- }
-
- static private function isValidPid($pid) {
- if (!is_string($pid) || empty($pid)) {
- return false;
- }
- return true;
- }
- }
|