|
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
|
|
|
public function getOutput() |
|
68
|
|
|
{ |
|
69
|
|
|
if (! $this->output) { |
|
70
|
|
|
$processOutput = $this->process->getOutput(); |
|
71
|
|
|
|
|
72
|
|
|
$this->output = @unserialize(base64_decode($processOutput)); |
|
73
|
|
|
|
|
74
|
|
|
if (! $this->output) { |
|
75
|
|
|
$this->errorOutput = $processOutput; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $this->output; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function getErrorOutput() |
|
83
|
|
|
{ |
|
84
|
|
|
if (! $this->errorOutput) { |
|
85
|
|
|
$processOutput = $this->process->getErrorOutput(); |
|
86
|
|
|
|
|
87
|
|
|
$this->errorOutput = @unserialize(base64_decode($processOutput)); |
|
88
|
|
|
|
|
89
|
|
|
if (! $this->errorOutput) { |
|
90
|
|
|
$this->errorOutput = $processOutput; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $this->errorOutput; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function getProcess(): Process |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->process; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function getId(): int |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->id; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function getPid(): ?int |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->pid; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function getCurrentExecutionTime(): float |
|
113
|
|
|
{ |
|
114
|
|
|
return microtime(true) - $this->startTime; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
protected function resolveErrorOutput(): Throwable |
|
118
|
|
|
{ |
|
119
|
|
|
$exception = $this->getErrorOutput(); |
|
120
|
|
|
|
|
121
|
|
|
if ($exception instanceof SerializableException) { |
|
122
|
|
|
$exception = $exception->asThrowable(); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
if (! $exception instanceof Throwable) { |
|
126
|
|
|
$exception = ParallelError::fromException($exception); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return $exception; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|