1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Async\Process; |
4
|
|
|
|
5
|
|
|
use Spatie\Async\Output\ParallelError; |
6
|
|
|
use Spatie\Async\Output\SerializableException; |
7
|
|
|
use Spatie\Async\Pool; |
8
|
|
|
use Spatie\Async\Runtime\ParentRuntime; |
9
|
|
|
use Symfony\Component\Process\Process; |
10
|
|
|
use Throwable; |
11
|
|
|
|
12
|
|
|
class ParallelProcess implements Runnable |
13
|
|
|
{ |
14
|
|
|
protected $process; |
15
|
|
|
protected $binary = Pool::DEFAULT_PHP_BINARY; |
16
|
|
|
protected $task; |
17
|
|
|
protected $id; |
18
|
|
|
protected $pid; |
19
|
|
|
|
20
|
|
|
protected $output; |
21
|
|
|
protected $errorOutput; |
22
|
|
|
|
23
|
|
|
protected $startTime; |
24
|
|
|
|
25
|
|
|
private $outputLength; |
26
|
|
|
|
27
|
|
|
use ProcessCallbacks; |
28
|
|
|
|
29
|
|
|
public function __construct(callable $task, Process $process, int $id, ?int $outputLength) |
30
|
|
|
{ |
31
|
|
|
$this->process = $process; |
32
|
|
|
$this->task = $task; |
33
|
|
|
$this->id = $id; |
34
|
|
|
$this->outputLength = $outputLength; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public static function create(callable $task, Process $process, int $id, ?int $outputLength): self |
38
|
|
|
{ |
39
|
|
|
return new self($task, $process, $id, $outputLength); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function start(): self |
43
|
|
|
{ |
44
|
|
|
$this->startTime = microtime(true); |
45
|
|
|
|
46
|
|
|
$this->process->start(); |
47
|
|
|
|
48
|
|
|
$this->pid = $this->process->getPid(); |
49
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function stop(): self |
54
|
|
|
{ |
55
|
|
|
$this->process->stop(10, SIGKILL); |
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function withBinary(string $binary = Pool::DEFAULT_PHP_BINARY): self |
61
|
|
|
{ |
62
|
|
|
$this->binary = $binary; |
63
|
|
|
$this->process = ParentRuntime::createProcessExecutable($this->task, $this->outputLength, $this->binary); |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function isRunning(): bool |
69
|
|
|
{ |
70
|
|
|
return $this->process->isRunning(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function isSuccessful(): bool |
74
|
|
|
{ |
75
|
|
|
return $this->process->isSuccessful(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function isTerminated(): bool |
79
|
|
|
{ |
80
|
|
|
return $this->process->isTerminated(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getOutput() |
84
|
|
|
{ |
85
|
|
|
if (! $this->output) { |
86
|
|
|
$processOutput = $this->process->getOutput(); |
87
|
|
|
|
88
|
|
|
$this->output = @unserialize(base64_decode($processOutput)); |
89
|
|
|
|
90
|
|
|
if (! $this->output) { |
91
|
|
|
$this->errorOutput = $processOutput; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $this->output; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function getErrorOutput() |
99
|
|
|
{ |
100
|
|
|
if (! $this->errorOutput) { |
101
|
|
|
$processOutput = $this->process->getErrorOutput(); |
102
|
|
|
|
103
|
|
|
$this->errorOutput = @unserialize(base64_decode($processOutput)); |
104
|
|
|
|
105
|
|
|
if (! $this->errorOutput) { |
106
|
|
|
$this->errorOutput = $processOutput; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $this->errorOutput; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function getProcess(): Process |
114
|
|
|
{ |
115
|
|
|
return $this->process; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function getId(): int |
119
|
|
|
{ |
120
|
|
|
return $this->id; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function getPid(): ?int |
124
|
|
|
{ |
125
|
|
|
return $this->pid; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function getCurrentExecutionTime(): float |
129
|
|
|
{ |
130
|
|
|
return microtime(true) - $this->startTime; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
protected function resolveErrorOutput(): Throwable |
134
|
|
|
{ |
135
|
|
|
$exception = $this->getErrorOutput(); |
136
|
|
|
|
137
|
|
|
if ($exception instanceof SerializableException) { |
138
|
|
|
$exception = $exception->asThrowable(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
if (! $exception instanceof Throwable) { |
142
|
|
|
$exception = ParallelError::fromException($exception); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $exception; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|