|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\Async; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Spatie\Async\Output\ErrorProcessOutput; |
|
7
|
|
|
use Spatie\Async\Output\ProcessOutput; |
|
8
|
|
|
use Throwable; |
|
9
|
|
|
|
|
10
|
|
|
class Runtime |
|
11
|
|
|
{ |
|
12
|
|
|
protected $maximumExecutionTime = 300; |
|
13
|
|
|
|
|
14
|
|
|
public function maximumExecutionTime(int $maximumExecutionTime): self |
|
15
|
|
|
{ |
|
16
|
|
|
$this->maximumExecutionTime = $maximumExecutionTime; |
|
17
|
|
|
|
|
18
|
|
|
return $this; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function start(Process $process): Process |
|
22
|
|
|
{ |
|
23
|
|
|
socket_create_pair(AF_UNIX, SOCK_STREAM, 0, $sockets); |
|
24
|
|
|
|
|
25
|
|
|
[$parentSocket, $childSocket] = $sockets; |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
if (($pid = pcntl_fork()) == 0) { |
|
28
|
|
|
socket_close($childSocket); |
|
29
|
|
|
|
|
30
|
|
|
$this->executeChildProcess($parentSocket, $process); |
|
31
|
|
|
|
|
32
|
|
|
exit; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
socket_close($parentSocket); |
|
36
|
|
|
|
|
37
|
|
|
return $process |
|
38
|
|
|
->setPid($pid) |
|
39
|
|
|
->setSocket($childSocket) |
|
40
|
|
|
->setStartTime(time()); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function handleFinishedProcess(Process $process): bool |
|
44
|
|
|
{ |
|
45
|
|
|
$output = $this->readProcessOutputFromSocket($process); |
|
46
|
|
|
|
|
47
|
|
|
if (!$output->isSuccess()) { |
|
48
|
|
|
$process->triggerError($output->payload()); |
|
49
|
|
|
|
|
50
|
|
|
return false; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$process->triggerSuccess($output->payload()); |
|
54
|
|
|
|
|
55
|
|
|
return true; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function handleRunningProcess(Process $process, $status): bool |
|
59
|
|
|
{ |
|
60
|
|
|
if ($process->startTime() + $this->maximumExecutionTime < time() || pcntl_wifstopped($status)) { |
|
61
|
|
|
if (! posix_kill($process->pid(), SIGKILL)) { |
|
62
|
|
|
throw new Exception("Failed to kill {$process->pid()}: ".posix_strerror(posix_get_last_error())); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$process->triggerTimeout(); |
|
66
|
|
|
|
|
67
|
|
|
return false; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return true; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
protected function executeChildProcess($parentSocket, Process $process): void |
|
74
|
|
|
{ |
|
75
|
|
|
try { |
|
76
|
|
|
$output = ProcessOutput::create($process->execute())->setSuccess(); |
|
77
|
|
|
} catch (Throwable $e) { |
|
78
|
|
|
$output = ErrorProcessOutput::create($e); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
socket_write($parentSocket, $output->serialize()); |
|
82
|
|
|
|
|
83
|
|
|
socket_close($parentSocket); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
protected function readProcessOutputFromSocket(Process $process): ProcessOutput |
|
87
|
|
|
{ |
|
88
|
|
|
$rawOutput = ''; |
|
89
|
|
|
|
|
90
|
|
|
while ($buffer = socket_read($process->socket(), 1024)) { |
|
91
|
|
|
$rawOutput .= $buffer; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$output = unserialize($rawOutput); |
|
95
|
|
|
|
|
96
|
|
|
socket_close($process->socket()); |
|
97
|
|
|
|
|
98
|
|
|
return $output; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.