mirror of
https://code.mensbeam.com/MensBeam/Arsse.git
synced 2024-12-22 21:22:40 +00:00
Separate PID conflict checking from PID claiming
This commit is contained in:
parent
822158d1bd
commit
bab64add9b
1 changed files with 29 additions and 25 deletions
|
@ -74,23 +74,24 @@ class Daemon {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function checkPID(string $pidfile, bool $lock) {
|
protected function checkPID(string $pidfile) {
|
||||||
if (!$lock) {
|
|
||||||
if (file_exists($pidfile)) {
|
if (file_exists($pidfile)) {
|
||||||
$pid = @file_get_contents($pidfile);
|
$pid = @file_get_contents($pidfile);
|
||||||
if (preg_match("/^\d+$/s", (string) $pid)) {
|
if (preg_match("/^\d+$/s", (string) $pid)) {
|
||||||
if (@posix_kill((int) $pid, 0)) {
|
if ($this->processExists((int) $pid)) {
|
||||||
throw new \Exception("Process already exists");
|
throw new \Exception("Process already exists");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
|
||||||
|
public function writePID(string $pidfile): void {
|
||||||
if ($f = @fopen($pidfile, "c+")) {
|
if ($f = @fopen($pidfile, "c+")) {
|
||||||
if (@flock($f, \LOCK_EX | \LOCK_NB)) {
|
if (@flock($f, \LOCK_EX | \LOCK_NB)) {
|
||||||
// confirm that some other process didn't get in before us
|
// confirm that some other process didn't get in before us
|
||||||
$pid = fread($f, 100);
|
$pid = fread($f, 100);
|
||||||
if (preg_match("/^\d+$/s", (string) $pid)) {
|
if (preg_match("/^\d+$/s", (string) $pid)) {
|
||||||
if (@posix_kill((int) $pid, 0)) {
|
if ($this->processExists((int) $pid)) {
|
||||||
throw new \Exception("Process already exists");
|
throw new \Exception("Process already exists");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -103,9 +104,12 @@ class Daemon {
|
||||||
throw new \Exception("Process already exists");
|
throw new \Exception("Process already exists");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new Exception("Could not write to PID file");
|
throw new \Exception("Could not write to PID file");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function processExists(int $pid): bool {
|
||||||
|
return @posix_kill($pid, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Resolves the PID file path and ensures the file or parent directory is writable */
|
/** Resolves the PID file path and ensures the file or parent directory is writable */
|
||||||
|
|
Loading…
Reference in a new issue