-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWorkerPool.php
224 lines (201 loc) · 5.82 KB
/
WorkerPool.php
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/*
* This file is part of the Worker package.
*
* (c) EXSyst
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace EXSyst\Component\Worker;
use ArrayAccess;
use Countable;
use EXSyst\Component\IO\Selectable;
use EXSyst\Component\Worker\Bootstrap\WorkerBootstrapProfile;
use IteratorAggregate;
use React\EventLoop\LoopInterface;
use Symfony\Component\Process\ExecutableFinder;
class WorkerPool implements ArrayAccess, Countable, IteratorAggregate
{
/**
* @var array
*/
private $workers;
/**
* @param WorkerBootstrapProfile $bootstrapProfile
* @param string $implementationExpression
* @param int|null $workerCount
*
* @throws Exception\InvalidArgumentException
* @throws Exception\RuntimeException
*/
protected function __construct(WorkerBootstrapProfile $bootstrapProfile, $implementationExpression, $workerCount = null)
{
$workerCount = ($workerCount === null) ? self::getProcessorCount() : intval($workerCount);
if ($workerCount <= 0) {
throw new Exception\InvalidArgumentException('The worker count must be a strictly positive number !');
}
$this->workers = [];
while ($workerCount-- > 0) {
$this->workers[] = Worker::withExpression($bootstrapProfile, $implementationExpression);
}
}
/**
* @param WorkerBootstrapProfile $bootstrapProfile
* @param string $implementationClassName
* @param int|null $workerCount
*
* @throws Exception\InvalidArgumentException
* @throws Exception\RuntimeException
*
* @return static
*/
public static function withClass(WorkerBootstrapProfile $bootstrapProfile, $implementationClassName, $workerCount = null)
{
return new static($bootstrapProfile, $bootstrapProfile->generateExpression($implementationClassName), $workerCount);
}
/**
* @param WorkerBootstrapProfile $bootstrapProfile
* @param string $implementationExpression
* @param int|null $workerCount
*
* @throws Exception\InvalidArgumentException
* @throws Exception\RuntimeException
*
* @return static
*/
public static function withExpression(WorkerBootstrapProfile $bootstrapProfile, $implementationExpression, $workerCount = null)
{
return new static($bootstrapProfile, $implementationExpression, $workerCount);
}
/**
* @throws Exception\RuntimeException
*
* @return int
*/
public static function getProcessorCount()
{
$getconfPath = self::findGetconf();
return intval(trim(shell_exec(escapeshellarg($getconfPath).' _NPROCESSORS_ONLN')));
}
/**
* @throws Exception\RuntimeException
*
* @return string
*/
private static function findGetconf()
{
$finder = new ExecutableFinder();
$getconfPath = $finder->find('getconf');
if ($getconfPath === null) {
throw new Exception\RuntimeException('Unable to find the "getconf" executable.');
}
return $getconfPath;
}
/**
* @param int $offset
*
* @return bool
*/
public function offsetExists($offset)
{
return isset($this->workers[$offset]);
}
/**
* @param int $offset
*
* @return Worker
*/
public function offsetGet($offset)
{
return $this->workers[$offset];
}
/**
* @param int $offset
* @param mixed $value
*
* @throws Exception\LogicException Always
*/
public function offsetSet($offset, $value)
{
throw new Exception\LogicException('A WorkerPool is a read-only container');
}
/**
* @param int $offset
*
* @throws Exception\LogicException Always
*/
public function offsetUnset($offset)
{
throw new Exception\LogicException('A WorkerPool is a read-only container');
}
/**
* @return int
*/
public function count()
{
return count($this->workers);
}
/**
* @return \Iterator
*/
public function getIterator()
{
foreach ($this->workers as $i => $worker) {
yield $i => $worker;
}
}
/**
* @param Worker $worker
*
* @throws Exception\RuntimeException
*
* @return mixed
*/
public function receiveMessage(&$worker)
{
$workers = $this->workers;
Selectable::selectRead($workers, null);
if (!count($workers)) {
throw new Exception\RuntimeException('selectRead returned an empty array (this should not happen)');
}
$worker = reset($workers);
return $worker->receiveMessage();
}
/**
* @param LoopInterface $loop
* @param callable $listener
* @param bool $once
*
* @return $this
*/
public function registerRead(LoopInterface $loop, callable $listener, $once = false)
{
if ($once) {
$listener2 = function ($worker, $loop) use ($listener) {
$this->unregisterRead($loop);
call_user_func($listener, $worker, $loop);
};
foreach ($this->workers as $worker) {
Selectable::registerRead($loop, $worker, $listener2);
}
} else {
foreach ($this->workers as $worker) {
Selectable::registerRead($loop, $worker, $listener);
}
}
return $this;
}
/**
* @param LoopInterface $loop
*
* @return $this
*/
public function unregisterRead(LoopInterface $loop)
{
foreach ($this->workers as $worker) {
Selectable::unregisterRead($loop, $worker);
}
return $this;
}
}