|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Nyx |
|
4
|
|
|
* |
|
5
|
|
|
* (The MIT license) |
|
6
|
|
|
* Copyright (c) 2016 Luke Steadman |
|
7
|
|
|
* |
|
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
9
|
|
|
* of this software and associated * documentation files (the "Software"), to |
|
10
|
|
|
* deal in the Software without restriction, including without limitation the |
|
11
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
|
12
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is |
|
13
|
|
|
* furnished to do so, subject to the following conditions: |
|
14
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
15
|
|
|
* all copies or substantial portions of the Software. |
|
16
|
|
|
* |
|
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
22
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
|
23
|
|
|
* IN THE SOFTWARE. |
|
24
|
|
|
* |
|
25
|
|
|
* @package Nyx |
|
26
|
|
|
*/ |
|
27
|
|
|
namespace Nyx; |
|
28
|
|
|
|
|
29
|
|
|
class Pool implements PoolInterface, OutputableInterface |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* Default amount of workers to spawn |
|
33
|
|
|
* |
|
34
|
|
|
* @var int |
|
35
|
|
|
*/ |
|
36
|
|
|
const DEFAULT_WORKER_COUNT = 1; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Immutable. Number of workers this pool maintain |
|
40
|
|
|
* |
|
41
|
|
|
* @var int |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $numberOfWorkers; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Pool of worker objects |
|
47
|
|
|
* |
|
48
|
|
|
* @var array |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $workers = array(); |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var WorkerInterface |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $workerInstance; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var OutputInterface |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $output; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Timestamp of when the pool first started |
|
64
|
|
|
* |
|
65
|
|
|
* @var |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $started; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Pool constructor. |
|
71
|
|
|
* |
|
72
|
|
|
* @param null $numberOfWorkers |
|
73
|
|
|
*/ |
|
74
|
9 |
|
public function __construct($numberOfWorkers = null) |
|
75
|
|
|
{ |
|
76
|
9 |
|
if (is_null($numberOfWorkers)) { |
|
77
|
9 |
|
$numberOfWorkers = static::DEFAULT_WORKER_COUNT; |
|
78
|
9 |
|
} |
|
79
|
|
|
|
|
80
|
9 |
|
$this->numberOfWorkers = $numberOfWorkers; |
|
81
|
9 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* {@inheritdoc} |
|
85
|
|
|
*/ |
|
86
|
8 |
|
public function add(WorkerInterface $worker) |
|
87
|
|
|
{ |
|
88
|
|
|
try { |
|
89
|
8 |
|
$this->setWorkerInstance($worker); |
|
90
|
8 |
|
} catch (\Exception $e) { |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
8 |
|
$cmd1 = $worker->getProcess()->getCommand()->getCmd(); |
|
94
|
8 |
|
$cmd2 = $this->workerInstance->getProcess()->getCommand()->getCmd(); |
|
95
|
|
|
|
|
96
|
8 |
|
if ($cmd1 === $cmd2) { |
|
97
|
8 |
|
$this->workers[] = $worker; |
|
98
|
8 |
|
} else { |
|
99
|
1 |
|
$msg = '$worker must be an instance of WorkerInterface and the same as'; |
|
100
|
1 |
|
throw new \Exception($msg . get_class($worker)); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
8 |
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* {@inheritdoc} |
|
108
|
|
|
*/ |
|
109
|
8 |
|
public function setWorkerInstance(WorkerInterface $worker) |
|
110
|
|
|
{ |
|
111
|
8 |
|
if (is_null($this->workerInstance)) { |
|
112
|
8 |
|
$this->workerInstance = clone $worker; |
|
113
|
8 |
|
} else { |
|
114
|
7 |
|
throw new \Exception('setWorkerInstance already called'); |
|
115
|
|
|
} |
|
116
|
8 |
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* {@inheritdoc} |
|
120
|
|
|
*/ |
|
121
|
1 |
|
public function rebuild() |
|
122
|
|
|
{ |
|
123
|
|
|
// Close |
|
124
|
1 |
|
$this->killAll(); |
|
125
|
|
|
|
|
126
|
|
|
// Then spawn new ones |
|
127
|
1 |
|
for ($i = 0; $i < $this->numberOfWorkers; $i++) { |
|
128
|
1 |
|
$this->spawn(); |
|
129
|
1 |
|
} |
|
130
|
|
|
|
|
131
|
1 |
|
return true; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* {@inheritdoc} |
|
136
|
|
|
*/ |
|
137
|
5 |
|
public function spawn() |
|
138
|
|
|
{ |
|
139
|
|
|
// Create a clone worker |
|
140
|
5 |
|
if ($this->numberOfWorkers > count($this->getWorkers())) { |
|
141
|
5 |
|
$worker = clone $this->workerInstance; |
|
142
|
5 |
|
$this->add($worker); |
|
143
|
5 |
|
$worker->start(); |
|
144
|
5 |
|
} else { |
|
145
|
1 |
|
throw new \Exception('Maximum number of workers allocated for this pool ('.$this->numberOfWorkers.').'); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
5 |
|
return $this; |
|
|
|
|
|
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* {@inheritdoc} |
|
153
|
|
|
*/ |
|
154
|
2 |
|
public function kill($pid = null) |
|
155
|
|
|
{ |
|
156
|
2 |
|
$worker = false; |
|
157
|
|
|
|
|
158
|
2 |
|
if (is_null($pid)) { |
|
159
|
1 |
|
$worker = reset($this->workers); |
|
160
|
1 |
|
$pid = key($this->workers); |
|
161
|
2 |
|
} elseif (array_key_exists($pid, $this->workers)) { |
|
162
|
1 |
|
$worker = $this->workers[$pid]; |
|
163
|
1 |
|
} |
|
164
|
|
|
|
|
165
|
2 |
|
if ($worker) { |
|
166
|
2 |
|
$worker->getProcess()->close(); |
|
167
|
2 |
|
unset($this->workers[$pid]); |
|
168
|
2 |
|
$this->getOutput()->write('[x] Worker closed.'); |
|
169
|
2 |
|
} |
|
170
|
|
|
|
|
171
|
2 |
|
return $this; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* {@inheritdoc} |
|
176
|
|
|
*/ |
|
177
|
1 |
|
public function killAll() |
|
178
|
|
|
{ |
|
179
|
1 |
|
foreach ($this->workers as $pid => $worker) { |
|
180
|
1 |
|
$this->kill($pid); |
|
181
|
1 |
|
} |
|
182
|
1 |
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* {@inheritdoc} |
|
186
|
|
|
*/ |
|
187
|
3 |
|
public function boot() |
|
188
|
|
|
{ |
|
189
|
3 |
|
if (!$this->started) { |
|
190
|
|
|
// Spawn the amount of workers required, if not already set. |
|
191
|
3 |
|
while (count($this->workers) < $this->numberOfWorkers) { |
|
192
|
3 |
|
$this->spawn(); |
|
193
|
3 |
|
} |
|
194
|
|
|
|
|
195
|
3 |
|
foreach ($this->workers as $worker) { |
|
196
|
|
|
// Only start the worker if it isn't running |
|
197
|
3 |
|
$worker->start(); |
|
198
|
3 |
|
} |
|
199
|
|
|
|
|
200
|
3 |
|
$this->started = true; |
|
201
|
3 |
|
} |
|
202
|
3 |
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* {@inheritdoc} |
|
206
|
|
|
*/ |
|
207
|
|
|
public function ping() |
|
208
|
|
|
{ |
|
209
|
|
|
foreach ($this->getWorkers() as $pid => $worker) { |
|
210
|
|
|
// If the worker has stopped, restart it |
|
211
|
|
|
if (!$worker->getProcess()->isRunning()) { |
|
212
|
|
|
$this->getOutput()->write('[x] Worker stopped. Spawning a new one.'); |
|
213
|
|
|
$this->kill($pid)->spawn(); |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* {@inheritdoc} |
|
220
|
|
|
*/ |
|
221
|
7 |
|
public function getWorkers() |
|
222
|
|
|
{ |
|
223
|
7 |
|
return (array) $this->workers; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* {@inheritdoc} |
|
228
|
|
|
*/ |
|
229
|
9 |
|
public function setOutput(OutputInterface $output) |
|
230
|
|
|
{ |
|
231
|
9 |
|
$this->output = $output; |
|
232
|
9 |
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* {@inheritdoc} |
|
236
|
|
|
*/ |
|
237
|
2 |
|
public function getOutput() |
|
238
|
|
|
{ |
|
239
|
2 |
|
if (is_null($this->output)) { |
|
240
|
|
|
$this->setOutput(new Console()); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
2 |
|
return $this->output; |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
|