Created
September 13, 2018 10:43
-
-
Save cosmos-sajal/e3ba27104f647f226d61619cbc7bcdf8 to your computer and use it in GitHub Desktop.
The complete one!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Class LockHandlerUtil | |
*/ | |
class LockHandlerUtil | |
{ | |
/** | |
* @param String $fileName | |
* @param String $rootDir | |
*/ | |
public function __construct($fileName, $rootDir) | |
{ | |
$this->fileName = $fileName; | |
$this->pidFolderPath = $rootDir.'/../pid_files/'; | |
} | |
/** | |
* @return boolean | |
*/ | |
public function isDuplicateInstanceRunning() | |
{ | |
$filePath = $this->getFilePath(); | |
$fileExists = file_exists($filePath); | |
if ($fileExists) { | |
$pid = file_get_contents($filePath); | |
if (file_exists("/proc/".$pid)) { | |
return true; | |
} | |
} | |
$this->createPIDFile(); | |
return false; | |
} | |
/** | |
* @return null | |
*/ | |
public function releaseInstance() | |
{ | |
$filePath = $this->getFilePath(); | |
unlink($filePath); | |
} | |
/** | |
* @return null | |
*/ | |
private function createPIDFile() | |
{ | |
file_put_contents($this->getFilePath(), (string) $this->getProcessId(), LOCK_EX); | |
} | |
/** | |
* @return Integer | |
*/ | |
private function getProcessId() | |
{ | |
return getmypid(); | |
} | |
/** | |
* @return String | |
*/ | |
private function getFilePath() | |
{ | |
return $this->pidFolderPath.$this->fileName.".pid"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment