-
-
Notifications
You must be signed in to change notification settings - Fork 448
Expand file tree
/
Copy pathParallelProcess.php
More file actions
159 lines (130 loc) · 4.21 KB
/
Copy pathParallelProcess.php
File metadata and controls
159 lines (130 loc) · 4.21 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
<?php
declare(strict_types=1);
namespace Rector\Parallel\ValueObject;
use Clue\React\NDJson\Decoder;
use Clue\React\NDJson\Encoder;
use Exception;
use React\ChildProcess\Process;
use React\EventLoop\LoopInterface;
use React\EventLoop\TimerInterface;
use Rector\Parallel\Enum\Action;
use Rector\Parallel\Enum\Content;
use Rector\Parallel\Enum\ReactCommand;
use Rector\Parallel\Enum\ReactEvent;
use Rector\Parallel\Exception\ParallelShouldNotHappenException;
use Throwable;
/**
* Inspired at @see https://raw.githubusercontent.com/phpstan/phpstan-src/master/src/Parallel/Process.php
*/
final class ParallelProcess
{
private Process $process;
private Encoder $encoder;
/**
* @var resource|null
*/
private $stdErr;
/**
* @var callable(mixed[]) : void
*/
private $onData;
/**
* @var callable(Throwable): void
*/
private $onError;
private ?TimerInterface $timer = null;
public function __construct(
private readonly string $command,
private readonly LoopInterface $loop,
private readonly int $timetoutInSeconds
) {
}
/**
* @param callable(mixed[] $onData) : void $onData
* @param callable(Throwable $onError) : void $onError
* @param callable(?int $onExit, string $output) : void $onExit
*/
public function start(callable $onData, callable $onError, callable $onExit): void
{
$tmp = tmpfile();
if ($tmp === false) {
throw new ParallelShouldNotHappenException('Failed creating temp file.');
}
$this->stdErr = $tmp;
$this->process = new Process($this->command, null, null, [
2 => $this->stdErr,
// todo is it fine to not have 0 and 1 FD?
]);
$this->process->start($this->loop);
$this->onData = $onData;
$this->onError = $onError;
$this->process->on(ReactEvent::EXIT, function ($exitCode) use ($onExit): void {
$stdErr = $this->stdErr;
if ($stdErr === null) {
throw new ParallelShouldNotHappenException();
}
$this->cancelTimer();
rewind($stdErr);
/** @var string $streamContents */
$streamContents = stream_get_contents($stdErr);
$onExit($exitCode, $streamContents);
fclose($stdErr);
});
}
/**
* @param mixed[] $data
*/
public function request(array $data): void
{
$this->cancelTimer();
$this->encoder->write($data);
$this->timer = $this->loop->addTimer($this->timetoutInSeconds, function (): void {
$onError = $this->onError;
$errorMessage = sprintf('Child process timed out after %d seconds', $this->timetoutInSeconds);
$onError(new Exception($errorMessage));
});
}
public function quit(): void
{
$this->cancelTimer();
if (! $this->process->isRunning()) {
return;
}
foreach ($this->process->pipes as $pipe) {
$pipe->close();
}
// the process can be quit before its connection is bound, e.g. on quitAll() after an error;
// in that case the encoder was never set
if (isset($this->encoder)) {
$this->encoder->end();
}
}
public function bindConnection(Decoder $decoder, Encoder $encoder): void
{
$decoder->on(ReactEvent::DATA, function (array $json): void {
$this->cancelTimer();
if ($json[ReactCommand::ACTION] !== Action::RESULT) {
return;
}
$onData = $this->onData;
$onData($json[Content::RESULT]);
});
$this->encoder = $encoder;
$decoder->on(ReactEvent::ERROR, function (Throwable $throwable): void {
$onError = $this->onError;
$onError($throwable);
});
$encoder->on(ReactEvent::ERROR, function (Throwable $throwable): void {
$onError = $this->onError;
$onError($throwable);
});
}
private function cancelTimer(): void
{
if (! $this->timer instanceof TimerInterface) {
return;
}
$this->loop->cancelTimer($this->timer);
$this->timer = null;
}
}