-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPidManager.php
More file actions
65 lines (54 loc) · 1.46 KB
/
PidManager.php
File metadata and controls
65 lines (54 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
declare(strict_types=1);
namespace Queue\Swoole;
use Queue\Swoole\Exception\RuntimeException;
use function dirname;
use function explode;
use function file_get_contents;
use function file_put_contents;
use function is_readable;
use function is_writable;
use function sprintf;
use function unlink;
class PidManager
{
public function __construct(private string $pidFile)
{
}
/**
* Write master pid and manager pid to pid file
*
* @throws RuntimeException When $pidFile is not writable.
*/
public function write(int $masterPid, int $managerPid): void
{
if (! is_writable($this->pidFile) && ! is_writable(dirname($this->pidFile))) {
throw new RuntimeException(sprintf('Pid file "%s" is not writable', $this->pidFile));
}
file_put_contents($this->pidFile, $masterPid . ',' . $managerPid);
}
/**
* Read master pid and manager pid from pid file
*
* @return string[] Array with master and manager PID values as strings
*/
public function read(): array
{
$pids = [];
if (is_readable($this->pidFile)) {
$content = file_get_contents($this->pidFile);
$pids = explode(',', $content);
}
return $pids;
}
/**
* Delete pid file
*/
public function delete(): bool
{
if (is_writable($this->pidFile)) {
return unlink($this->pidFile);
}
return false;
}
}