PABX da Simples IP
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.6 KiB

<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of Pid
*
* @author Amarildo Pereira.
*/
final class Pid {
private $fileName;
private $remove;
private $options = [];
public function __construct($date = true) {
$this->options['pidDate'] = $date;
$this->remove = true;
$this->fileName = strstr(basename((debug_backtrace()[0]["file"])), '.', true);
$this->PidCheck();
}
private function PidFile() {
return sprintf('/tmp/%s%s.pid', $this->fileName, ($this->options['pidDate'] ? ("-" . date('Ymd')): ''));
}
private function PidCheck() {
$this->PidRemoveAuto();
if (file_exists($this->PidFile())) {
$this->remove = false;
exit(0);
}
$this->PidCreate();
return true;
}
private function PidCreate() {
file_put_contents($this->PidFile(), getmypid() . "|" . time());
}
public function PidRemove() {
if ($this->remove && file_exists($this->PidFile())) {
unlink($this->PidFile());
}
}
private function PidRemoveAuto(){
if(!file_exists($this->PidFile())){
return false;
}
$strFile = file_get_contents($this->PidFile());
$timeexec = explode("|", $strFile);
if($timeexec[1] <= strtotime('-2 hour')){
unlink($this->PidFile());
}
}
/**
* Connect clone.
*/
private function __clone() {
}
}