|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\Async; |
|
4
|
|
|
|
|
5
|
|
|
use Throwable; |
|
6
|
|
|
use Symfony\Component\Process\Process; |
|
7
|
|
|
use Spatie\Async\Output\SerializableException; |
|
8
|
|
|
|
|
9
|
|
|
class ParallelProcess |
|
10
|
|
|
{ |
|
11
|
|
|
protected $process; |
|
12
|
|
|
protected $id; |
|
13
|
|
|
protected $pid; |
|
14
|
|
|
|
|
15
|
|
|
protected $successCallbacks = []; |
|
16
|
|
|
protected $errorCallbacks = []; |
|
17
|
|
|
protected $timeoutCallbacks = []; |
|
18
|
|
|
|
|
19
|
|
|
protected $output; |
|
20
|
|
|
protected $errorOutput; |
|
21
|
|
|
|
|
22
|
|
|
protected $startTime; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct(Process $process) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->process = $process; |
|
27
|
|
|
$this->id = uniqid(getmypid()); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public static function create(Process $process): self |
|
31
|
|
|
{ |
|
32
|
|
|
return new self($process); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function then(callable $callback): self |
|
36
|
|
|
{ |
|
37
|
|
|
$this->successCallbacks[] = $callback; |
|
38
|
|
|
|
|
39
|
|
|
return $this; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function catch(callable $callback): self |
|
|
|
|
|
|
43
|
|
|
{ |
|
44
|
|
|
$this->errorCallbacks[] = $callback; |
|
45
|
|
|
|
|
46
|
|
|
return $this; |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function timeout(callable $callback): self |
|
50
|
|
|
{ |
|
51
|
|
|
$this->timeoutCallbacks[] = $callback; |
|
52
|
|
|
|
|
53
|
|
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function start(): self |
|
57
|
|
|
{ |
|
58
|
|
|
$this->startTime = microtime(true); |
|
59
|
|
|
|
|
60
|
|
|
$this->process->start(); |
|
61
|
|
|
|
|
62
|
|
|
$this->pid = $this->process->getPid(); |
|
63
|
|
|
|
|
64
|
|
|
return $this; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function stop(): self |
|
68
|
|
|
{ |
|
69
|
|
|
$this->process->stop(10, SIGKILL); |
|
70
|
|
|
|
|
71
|
|
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function isRunning(): bool |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->process->isRunning(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function isSuccessful(): bool |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->process->isSuccessful(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function isTerminated(): bool |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->process->isTerminated(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function getOutput() |
|
90
|
|
|
{ |
|
91
|
|
|
if (! $this->output) { |
|
92
|
|
|
$processOutput = $this->process->getOutput(); |
|
93
|
|
|
|
|
94
|
|
|
$this->output = @unserialize(base64_decode($processOutput)); |
|
95
|
|
|
|
|
96
|
|
|
if (! $this->output) { |
|
97
|
|
|
$this->errorOutput = $processOutput; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $this->output; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function getErrorOutput() |
|
105
|
|
|
{ |
|
106
|
|
|
if (! $this->errorOutput) { |
|
107
|
|
|
$processOutput = $this->process->getErrorOutput(); |
|
108
|
|
|
|
|
109
|
|
|
$this->errorOutput = @unserialize(base64_decode($processOutput)); |
|
110
|
|
|
|
|
111
|
|
|
if (! $this->errorOutput) { |
|
112
|
|
|
$this->errorOutput = $processOutput; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $this->errorOutput; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function getProcess(): Process |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->process; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function getId(): string |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->id; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function getPid(): ?string |
|
130
|
|
|
{ |
|
131
|
|
|
return $this->pid; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function getCurrentExecutionTime(): float |
|
135
|
|
|
{ |
|
136
|
|
|
return microtime(true) - $this->startTime; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public function triggerSuccess() |
|
140
|
|
|
{ |
|
141
|
|
|
if ($this->getErrorOutput()) { |
|
142
|
|
|
$this->triggerError(); |
|
143
|
|
|
|
|
144
|
|
|
return; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
$output = $this->getOutput(); |
|
148
|
|
|
|
|
149
|
|
|
foreach ($this->successCallbacks as $callback) { |
|
150
|
|
|
call_user_func_array($callback, [$output]); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return $output; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function triggerError() |
|
157
|
|
|
{ |
|
158
|
|
|
$exception = $this->getErrorOutput(); |
|
159
|
|
|
|
|
160
|
|
|
if ($exception instanceof SerializableException) { |
|
161
|
|
|
$exception = $exception->asThrowable(); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
foreach ($this->errorCallbacks as $callback) { |
|
165
|
|
|
call_user_func_array($callback, [$exception]); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
if (! $this->errorCallbacks) { |
|
|
|
|
|
|
169
|
|
|
if ($exception instanceof Throwable) { |
|
170
|
|
|
throw $exception; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
throw ParallelError::fromException($exception); |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
public function triggerTimeout() |
|
178
|
|
|
{ |
|
179
|
|
|
foreach ($this->timeoutCallbacks as $callback) { |
|
180
|
|
|
call_user_func_array($callback, []); |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|