1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Async\Process; |
4
|
|
|
|
5
|
|
|
use Throwable; |
6
|
|
|
use Spatie\Async\Output\ParallelError; |
7
|
|
|
use Symfony\Component\Process\Process; |
8
|
|
|
use Spatie\Async\Output\SerializableException; |
9
|
|
|
|
10
|
|
|
class ParallelProcess implements Runnable |
11
|
|
|
{ |
12
|
|
|
protected $process; |
13
|
|
|
protected $id; |
14
|
|
|
protected $pid; |
15
|
|
|
|
16
|
|
|
protected $output; |
17
|
|
|
protected $errorOutput; |
18
|
|
|
|
19
|
|
|
protected $startTime; |
20
|
|
|
|
21
|
|
|
use ProcessCallbacks; |
22
|
|
|
|
23
|
|
|
public function __construct(Process $process, int $id) |
24
|
|
|
{ |
25
|
|
|
$this->process = $process; |
26
|
|
|
$this->id = $id; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public static function create(Process $process, int $id): self |
30
|
|
|
{ |
31
|
|
|
return new self($process, $id); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function start(): self |
35
|
|
|
{ |
36
|
|
|
$this->startTime = microtime(true); |
37
|
|
|
|
38
|
|
|
$this->process->start(); |
39
|
|
|
|
40
|
|
|
$this->pid = $this->process->getPid(); |
41
|
|
|
|
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function stop(): self |
46
|
|
|
{ |
47
|
|
|
$this->process->stop(10, SIGKILL); |
48
|
|
|
|
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function isRunning(): bool |
53
|
|
|
{ |
54
|
|
|
return $this->process->isRunning(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function isSuccessful(): bool |
58
|
|
|
{ |
59
|
|
|
return $this->process->isSuccessful(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function isTerminated(): bool |
63
|
|
|
{ |
64
|
|
|
return $this->process->isTerminated(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Flush the process's stdout output buffer (it is then kept by Symfony's copy of stdout). |
69
|
|
|
* Due to stdout output buffering, it is possible for a program to hang once it fills the buffer for stdout. At this point, the program wouldn't do anything else. By forcing a check on the program's output, we can ensure that we actually read through the output buffer. |
70
|
|
|
*/ |
71
|
|
|
public function seekOutput() |
72
|
|
|
{ |
73
|
|
|
$this->process->getOutput(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getOutput() |
77
|
|
|
{ |
78
|
|
|
if (! $this->output) { |
79
|
|
|
$processOutput = $this->process->getOutput(); |
80
|
|
|
|
81
|
|
|
$this->output = @unserialize(base64_decode($processOutput)); |
82
|
|
|
|
83
|
|
|
if (! $this->output) { |
84
|
|
|
$this->errorOutput = $processOutput; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $this->output; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getErrorOutput() |
92
|
|
|
{ |
93
|
|
|
if (! $this->errorOutput) { |
94
|
|
|
$processOutput = $this->process->getErrorOutput(); |
95
|
|
|
|
96
|
|
|
$this->errorOutput = @unserialize(base64_decode($processOutput)); |
97
|
|
|
|
98
|
|
|
if (! $this->errorOutput) { |
99
|
|
|
$this->errorOutput = $processOutput; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $this->errorOutput; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function getProcess(): Process |
107
|
|
|
{ |
108
|
|
|
return $this->process; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function getId(): int |
112
|
|
|
{ |
113
|
|
|
return $this->id; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function getPid(): ?int |
117
|
|
|
{ |
118
|
|
|
return $this->pid; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function getCurrentExecutionTime(): float |
122
|
|
|
{ |
123
|
|
|
return microtime(true) - $this->startTime; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
protected function resolveErrorOutput(): Throwable |
127
|
|
|
{ |
128
|
|
|
$exception = $this->getErrorOutput(); |
129
|
|
|
|
130
|
|
|
if ($exception instanceof SerializableException) { |
131
|
|
|
$exception = $exception->asThrowable(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if (! $exception instanceof Throwable) { |
135
|
|
|
$exception = ParallelError::fromException($exception); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $exception; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|