Repository navigation
Expand file tree
/
Copy pathAbstractProcess.php
More file actions
199 lines (169 loc) · 6.48 KB
/
Copy pathAbstractProcess.php
File metadata and controls
199 lines (169 loc) · 6.48 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Process;
use Hyperf\Contract\ProcessInterface;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Coordinator\Constants;
use Hyperf\Coordinator\CoordinatorManager;
use Hyperf\Coroutine\Coroutine;
use Hyperf\Engine\Channel;
use Hyperf\Engine\Constant;
use Hyperf\ExceptionHandler\Formatter\FormatterInterface;
use Hyperf\Process\Event\AfterCoroutineHandle;
use Hyperf\Process\Event\AfterProcessHandle;
use Hyperf\Process\Event\BeforeCoroutineHandle;
use Hyperf\Process\Event\BeforeProcessHandle;
use Hyperf\Process\Event\PipeMessage;
use Hyperf\Process\Exception\ServerInvalidException;
use Hyperf\Process\Exception\SocketAcceptException;
use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Swoole\Coroutine\Socket;
use Swoole\Event;
use Swoole\Process as SwooleProcess;
use Swoole\Server;
use Swoole\Timer;
use Throwable;
abstract class AbstractProcess implements ProcessInterface
{
public string $name = 'process';
public int $nums = 1;
public bool $redirectStdinStdout = false;
public int $pipeType = SOCK_DGRAM;
public bool $enableCoroutine = true;
protected ?EventDispatcherInterface $event = null;
protected ?SwooleProcess $process = null;
protected int $recvLength = 65535;
protected float $recvTimeout = 10.0;
protected int $restartInterval = 5;
public function __construct(protected ContainerInterface $container)
{
if ($container->has(EventDispatcherInterface::class)) {
$this->event = $container->get(EventDispatcherInterface::class);
}
}
public function isEnable($server): bool
{
return true;
}
public function bind($server): void
{
if (Constant::isCoroutineServer($server)) {
$this->bindCoroutineServer($server);
return;
}
if ($server instanceof Server) {
$this->bindServer($server);
return;
}
throw new ServerInvalidException(sprintf('Server %s is invalid.', get_class($server)));
}
protected function bindServer(Server $server): void
{
$num = $this->nums;
for ($i = 0; $i < $num; ++$i) {
$process = new SwooleProcess(function (SwooleProcess $process) use ($i) {
try {
$this->event?->dispatch(new BeforeProcessHandle($this, $i));
$this->process = $process;
if ($this->enableCoroutine) {
$quit = new Channel(1);
$this->listen($quit);
}
$this->handle();
} catch (Throwable $throwable) {
$this->logThrowable($throwable);
} finally {
$this->event?->dispatch(new AfterProcessHandle($this, $i));
if (isset($quit)) {
$quit->push(true);
}
Timer::clearAll();
CoordinatorManager::until(Constants::WORKER_EXIT)->resume();
sleep($this->restartInterval);
}
}, $this->redirectStdinStdout, $this->pipeType, $this->enableCoroutine);
$process->setBlocking(false);
$server->addProcess($process);
if ($this->enableCoroutine) {
ProcessCollector::add($this->name, $process);
}
}
}
protected function bindCoroutineServer($server): void
{
$num = $this->nums;
Coroutine::create(static function () {
if (CoordinatorManager::until(Constants::WORKER_EXIT)->yield()) {
ProcessManager::setRunning(false);
}
});
for ($i = 0; $i < $num; ++$i) {
$handler = function () use ($i) {
$this->event?->dispatch(new BeforeCoroutineHandle($this, $i));
while (true) {
try {
$this->handle();
} catch (Throwable $throwable) {
$this->logThrowable($throwable);
}
if (CoordinatorManager::until(Constants::WORKER_EXIT)->yield($this->restartInterval)) {
break;
}
}
$this->event?->dispatch(new AfterCoroutineHandle($this, $i));
};
Coroutine::create($handler);
}
}
/**
* Added event for listening data from worker/task.
*/
protected function listen(Channel $quit)
{
Coroutine::create(function () use ($quit) {
while ($quit->pop(0.001) !== true) {
try {
/** @var Socket $sock */
$sock = $this->process->exportSocket();
$recv = $sock->recv($this->recvLength, $this->recvTimeout);
if ($recv === '') {
throw new SocketAcceptException('Socket is closed', $sock->errCode);
}
if ($recv === false && $sock->errCode !== SOCKET_ETIMEDOUT) {
throw new SocketAcceptException('Socket is closed', $sock->errCode);
}
if ($this->event && $recv !== false && $data = unserialize($recv)) {
$this->event->dispatch(new PipeMessage($data));
}
} catch (Throwable $exception) {
$this->logThrowable($exception);
if ($exception instanceof SocketAcceptException) {
// TODO: Reconnect the socket.
break;
}
}
}
$quit->close();
});
}
protected function logThrowable(Throwable $throwable): void
{
if ($this->container->has(StdoutLoggerInterface::class) && $this->container->has(FormatterInterface::class)) {
$logger = $this->container->get(StdoutLoggerInterface::class);
$formatter = $this->container->get(FormatterInterface::class);
$logger->error($formatter->format($throwable));
if ($throwable instanceof SocketAcceptException) {
$logger->critical('Socket of process is unavailable, please restart the server');
}
}
}
}