-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPool.php
More file actions
123 lines (101 loc) · 2.69 KB
/
Pool.php
File metadata and controls
123 lines (101 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
namespace Gt\Daemon;
class Pool {
/** @var Process[] Associative array of name=>Process */
protected array $processList;
/** @var array<int, callable(Process):void> */
protected array $completeCallbackList;
public function __construct() {
$this->processList = [];
$this->completeCallbackList = [];
}
public function add(string $name, Process $process):void {
$this->processList[$name] = $process;
$process->onComplete(
function(Process $completedProcess):void {
$this->dispatchCompletionCallback($completedProcess);
}
);
}
public function onComplete(callable $callback):void {
$this->completeCallbackList[] = $callback;
}
/** Starts the execution of all processes */
public function exec():void {
foreach($this->processList as $process) {
$process->exec();
}
}
public function numRunning():int {
$num = 0;
foreach($this->processList as $process) {
$num += (int)$process->isRunning();
}
return $num;
}
/** Returns output for all the processes in the $processList */
public function read(int $pipe = Process::PIPE_OUT):string {
$output = "";
foreach($this->processList as $name => $process) {
$outLines = explode(
PHP_EOL,
$process->getOutput($pipe)
);
foreach($outLines as $i => $line) {
if($line === "") {
unset($outLines[$i]);
}
}
foreach($outLines as $line) {
if($pipe === Process::PIPE_ERROR) {
$output .= "[$name ERROR] $line";
}
else {
$output .= "[$name] $line";
}
$output .= PHP_EOL;
}
}
return $output;
}
/** Returns errors for all the processes in the $processList */
public function readError():string {
return $this->read(Process::PIPE_ERROR);
}
public function readOutputOf(
string $name,
int $pipe = Process::PIPE_OUT
):string {
if(!array_key_exists($name, $this->processList)) {
throw new DaemonException("No process named $name found.");
}
$process = $this->processList[$name];
return $process->getOutput($pipe);
}
public function readErrorOf(string $name):string {
return $this->readOutputOf($name, Process::PIPE_ERROR);
}
/** @return int[] Associative array of each closed process's exit code. */
public function close():array {
foreach($this->processList as $name => $process) {
$process->terminate();
}
$codes = [];
do {
foreach($this->processList as $name => $process) {
$code = $process->getExitCode();
if(!is_null($code)) {
$codes[$name] = $code;
}
}
usleep(100000);
}
while(count($codes) < count($this->processList));
return $codes;
}
protected function dispatchCompletionCallback(Process $process):void {
foreach($this->completeCallbackList as $callback) {
$callback($process);
}
}
}