Skip to content

Instantly share code, notes, and snippets.

@cosmos-sajal
Created September 13, 2018 10:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cosmos-sajal/e3ba27104f647f226d61619cbc7bcdf8 to your computer and use it in GitHub Desktop.
Save cosmos-sajal/e3ba27104f647f226d61619cbc7bcdf8 to your computer and use it in GitHub Desktop.
The complete one!
/**
* 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