forked from skylerkatz/hover
-
Notifications
You must be signed in to change notification settings - Fork 1
/
FpmManager.php
179 lines (136 loc) · 4.36 KB
/
FpmManager.php
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
use hollodotme\FastCGI\Client;
use hollodotme\FastCGI\Exceptions\TimedoutException;
use hollodotme\FastCGI\Interfaces\ProvidesResponseData;
use hollodotme\FastCGI\SocketConnections\UnixDomainSocket;
use Symfony\Component\Process\Process;
class FpmManager
{
const SOCKET_FILE = '/tmp/.hover/php-fpm.sock';
const CONFIG_FILE = '/tmp/.hover/php-fpm.conf';
const PID_FILE = '/tmp/.hover/php-fpm.pid';
public Process $fpmProcess;
public Client $fastCgiClient;
public UnixDomainSocket $socketConnection;
public function start()
{
if ($this->isReady()) {
$this->killExistingFpm();
}
if (! is_dir(dirname(self::SOCKET_FILE))) {
mkdir(dirname(self::SOCKET_FILE));
}
if (! file_exists(self::CONFIG_FILE)) {
file_put_contents(
self::CONFIG_FILE,
file_get_contents(__DIR__.'/php-fpm.conf')
);
}
$this->fpmProcess = new Process([
'php-fpm',
'--nodaemonize',
'--force-stderr',
'--fpm-config',
self::CONFIG_FILE,
]);
$this->fpmProcess->disableOutput()
->setTimeout(null)
->start(function ($type, $line) {
fwrite(STDERR, $line.PHP_EOL);
});
$this->fastCgiClient = new Client();
$this->socketConnection = new UnixDomainSocket(self::SOCKET_FILE, 1000, 900000);
$this->waitUntilReady();
}
public function sendRequest(FpmRequest $request, $timeout): ProvidesResponseData
{
try {
$socketId = $this->fastCgiClient->sendAsyncRequest($this->socketConnection, $request);
$response = $this->fastCgiClient->readResponse($socketId, $timeout - 1000);
} catch (TimedoutException $e) {
$this->stop();
$this->start();
throw new Exception('FPM request timed out 1 second before Lambda times out.');
}
return $response;
}
protected function isReady()
{
clearstatcache(false, self::SOCKET_FILE);
return file_exists(self::SOCKET_FILE);
}
private function killExistingFpm(): void
{
if (! file_exists(self::PID_FILE)) {
unlink(self::SOCKET_FILE);
return;
}
$pid = (int) file_get_contents(self::PID_FILE);
if ($pid <= 0) {
unlink(self::SOCKET_FILE);
unlink(self::PID_FILE);
return;
}
if (posix_getpgid($pid) === false) {
unlink(self::SOCKET_FILE);
unlink(self::PID_FILE);
return;
}
if ($pid === posix_getpid()) {
unlink(self::SOCKET_FILE);
unlink(self::PID_FILE);
return;
}
$result = posix_kill($pid, 15);
if ($result === false) {
unlink(self::SOCKET_FILE);
unlink(self::PID_FILE);
return;
}
$this->waitUntilStopped($pid);
unlink(self::SOCKET_FILE);
unlink(self::PID_FILE);
}
private function waitUntilStopped(int $pid): void
{
$wait = 5000;
$timeout = 1000000;
$elapsed = 0;
while (posix_getpgid($pid) !== false) {
usleep($wait);
$elapsed += $wait;
if ($elapsed > $timeout) {
throw new Exception('Timeout while waiting for PHP-FPM to stop');
}
}
}
private function waitUntilReady(): void
{
$wait = 5000;
$timeout = 5000000;
$elapsed = 0;
while (! $this->isReady()) {
usleep($wait);
$elapsed += $wait;
if ($elapsed > $timeout) {
throw new Exception('Timeout while waiting for PHP-FPM socket');
}
if (! $this->fpmProcess->isRunning()) {
throw new Exception('PHP-FPM failed to start: '.PHP_EOL.$this->fpmProcess->getOutput().PHP_EOL.$this->fpmProcess->getErrorOutput());
}
}
}
public function stop(): void
{
if ($this->fpmProcess && $this->fpmProcess->isRunning()) {
$this->fpmProcess->stop(0.5);
if ($this->isReady()) {
throw new Exception('PHP-FPM cannot be stopped');
}
}
}
public function __destruct()
{
$this->stop();
}
}