@@ -7,64 +7,64 @@ |
||
7 | 7 | |
8 | 8 | class Mediator |
9 | 9 | { |
10 | - /** @var ListenerInterface[][] */ |
|
11 | - private $listeners = []; |
|
10 | + /** @var ListenerInterface[][] */ |
|
11 | + private $listeners = []; |
|
12 | 12 | |
13 | - /** |
|
14 | - * @return ListenerInterface[][] |
|
15 | - */ |
|
16 | - public function getListeners(): array |
|
17 | - { |
|
18 | - return $this->listeners; |
|
19 | - } |
|
13 | + /** |
|
14 | + * @return ListenerInterface[][] |
|
15 | + */ |
|
16 | + public function getListeners(): array |
|
17 | + { |
|
18 | + return $this->listeners; |
|
19 | + } |
|
20 | 20 | |
21 | - /** |
|
22 | - * @param ListenerInterface $listener |
|
23 | - * @return Mediator |
|
24 | - */ |
|
25 | - public function addListener(ListenerInterface $listener): self |
|
26 | - { |
|
27 | - $this->listeners[$listener->getEventName()][] = $listener; |
|
21 | + /** |
|
22 | + * @param ListenerInterface $listener |
|
23 | + * @return Mediator |
|
24 | + */ |
|
25 | + public function addListener(ListenerInterface $listener): self |
|
26 | + { |
|
27 | + $this->listeners[$listener->getEventName()][] = $listener; |
|
28 | 28 | |
29 | - return $this; |
|
30 | - } |
|
29 | + return $this; |
|
30 | + } |
|
31 | 31 | |
32 | - /** |
|
33 | - * @param ListenerInterface $listener |
|
34 | - * @return Mediator |
|
35 | - */ |
|
36 | - public function removeListener(ListenerInterface $listener): self |
|
37 | - { |
|
38 | - if (!isset($this->listeners[$listener->getEventName()])) { |
|
39 | - return $this; |
|
40 | - } |
|
32 | + /** |
|
33 | + * @param ListenerInterface $listener |
|
34 | + * @return Mediator |
|
35 | + */ |
|
36 | + public function removeListener(ListenerInterface $listener): self |
|
37 | + { |
|
38 | + if (!isset($this->listeners[$listener->getEventName()])) { |
|
39 | + return $this; |
|
40 | + } |
|
41 | 41 | |
42 | - $key = array_search($listener, $this->listeners[$listener->getEventName()]); |
|
42 | + $key = array_search($listener, $this->listeners[$listener->getEventName()]); |
|
43 | 43 | |
44 | - if (false !== $key) { |
|
45 | - unset($this->listeners[$listener->getEventName()][$key]); |
|
46 | - } |
|
44 | + if (false !== $key) { |
|
45 | + unset($this->listeners[$listener->getEventName()][$key]); |
|
46 | + } |
|
47 | 47 | |
48 | - if (empty($this->listeners[$listener->getEventName()])) { |
|
49 | - unset($this->listeners[$listener->getEventName()]); |
|
50 | - } |
|
48 | + if (empty($this->listeners[$listener->getEventName()])) { |
|
49 | + unset($this->listeners[$listener->getEventName()]); |
|
50 | + } |
|
51 | 51 | |
52 | - return $this; |
|
53 | - } |
|
52 | + return $this; |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * @param string $eventName |
|
57 | - * @param EventInterface $event |
|
58 | - */ |
|
59 | - public function notify($eventName, EventInterface $event) |
|
60 | - { |
|
61 | - if (!isset($this->listeners[$eventName])) { |
|
62 | - return; |
|
63 | - } |
|
55 | + /** |
|
56 | + * @param string $eventName |
|
57 | + * @param EventInterface $event |
|
58 | + */ |
|
59 | + public function notify($eventName, EventInterface $event) |
|
60 | + { |
|
61 | + if (!isset($this->listeners[$eventName])) { |
|
62 | + return; |
|
63 | + } |
|
64 | 64 | |
65 | - foreach ($this->listeners[$eventName] as $listener) { |
|
66 | - $listener->notify($event); |
|
67 | - } |
|
68 | - } |
|
65 | + foreach ($this->listeners[$eventName] as $listener) { |
|
66 | + $listener->notify($event); |
|
67 | + } |
|
68 | + } |
|
69 | 69 | |
70 | 70 | } |
@@ -7,21 +7,21 @@ |
||
7 | 7 | interface ListenerInterface |
8 | 8 | { |
9 | 9 | |
10 | - /** |
|
11 | - * @return string |
|
12 | - */ |
|
13 | - public function getEventName(): string; |
|
10 | + /** |
|
11 | + * @return string |
|
12 | + */ |
|
13 | + public function getEventName(): string; |
|
14 | 14 | |
15 | - /** |
|
16 | - * @param callable $callback |
|
17 | - * @return $this |
|
18 | - */ |
|
19 | - public function setCallback(callable $callback): self; |
|
15 | + /** |
|
16 | + * @param callable $callback |
|
17 | + * @return $this |
|
18 | + */ |
|
19 | + public function setCallback(callable $callback): self; |
|
20 | 20 | |
21 | - /** |
|
22 | - * @param EventInterface $event |
|
23 | - * @return void |
|
24 | - */ |
|
25 | - public function notify(EventInterface $event); |
|
21 | + /** |
|
22 | + * @param EventInterface $event |
|
23 | + * @return void |
|
24 | + */ |
|
25 | + public function notify(EventInterface $event); |
|
26 | 26 | |
27 | 27 | } |
@@ -6,57 +6,57 @@ |
||
6 | 6 | |
7 | 7 | class Listener implements ListenerInterface |
8 | 8 | { |
9 | - /** @var string $eventName */ |
|
10 | - private $eventName; |
|
11 | - |
|
12 | - /** @var callable $callback */ |
|
13 | - private $callback; |
|
14 | - |
|
15 | - /** |
|
16 | - * Listener constructor. |
|
17 | - * @param string $eventName |
|
18 | - * @param callable|null $callback |
|
19 | - */ |
|
20 | - public function __construct(string $eventName, callable $callback = null) |
|
21 | - { |
|
22 | - $this->eventName = $eventName; |
|
23 | - $this->callback = $callback; |
|
24 | - } |
|
25 | - |
|
26 | - /** |
|
27 | - * @inheritDoc |
|
28 | - */ |
|
29 | - public function getEventName(): string |
|
30 | - { |
|
31 | - return $this->eventName; |
|
32 | - } |
|
33 | - |
|
34 | - /** |
|
35 | - * @inheritDoc |
|
36 | - */ |
|
37 | - public function setEventName(string $eventName): self |
|
38 | - { |
|
39 | - $this->eventName = $eventName; |
|
40 | - |
|
41 | - return $this; |
|
42 | - } |
|
43 | - |
|
44 | - /** |
|
45 | - * @inheritDoc |
|
46 | - */ |
|
47 | - public function setCallback(callable $callback): ListenerInterface |
|
48 | - { |
|
49 | - $this->callback = $callback; |
|
50 | - |
|
51 | - return $this; |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * @inheritDoc |
|
56 | - */ |
|
57 | - public function notify(EventInterface $event) |
|
58 | - { |
|
59 | - ($this->callback)($event); |
|
60 | - } |
|
9 | + /** @var string $eventName */ |
|
10 | + private $eventName; |
|
11 | + |
|
12 | + /** @var callable $callback */ |
|
13 | + private $callback; |
|
14 | + |
|
15 | + /** |
|
16 | + * Listener constructor. |
|
17 | + * @param string $eventName |
|
18 | + * @param callable|null $callback |
|
19 | + */ |
|
20 | + public function __construct(string $eventName, callable $callback = null) |
|
21 | + { |
|
22 | + $this->eventName = $eventName; |
|
23 | + $this->callback = $callback; |
|
24 | + } |
|
25 | + |
|
26 | + /** |
|
27 | + * @inheritDoc |
|
28 | + */ |
|
29 | + public function getEventName(): string |
|
30 | + { |
|
31 | + return $this->eventName; |
|
32 | + } |
|
33 | + |
|
34 | + /** |
|
35 | + * @inheritDoc |
|
36 | + */ |
|
37 | + public function setEventName(string $eventName): self |
|
38 | + { |
|
39 | + $this->eventName = $eventName; |
|
40 | + |
|
41 | + return $this; |
|
42 | + } |
|
43 | + |
|
44 | + /** |
|
45 | + * @inheritDoc |
|
46 | + */ |
|
47 | + public function setCallback(callable $callback): ListenerInterface |
|
48 | + { |
|
49 | + $this->callback = $callback; |
|
50 | + |
|
51 | + return $this; |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * @inheritDoc |
|
56 | + */ |
|
57 | + public function notify(EventInterface $event) |
|
58 | + { |
|
59 | + ($this->callback)($event); |
|
60 | + } |
|
61 | 61 | |
62 | 62 | } |
@@ -5,9 +5,9 @@ |
||
5 | 5 | interface EventInterface |
6 | 6 | { |
7 | 7 | |
8 | - /** |
|
9 | - * @return string |
|
10 | - */ |
|
11 | - public function getEventName(): string; |
|
8 | + /** |
|
9 | + * @return string |
|
10 | + */ |
|
11 | + public function getEventName(): string; |
|
12 | 12 | |
13 | 13 | } |
@@ -8,54 +8,54 @@ |
||
8 | 8 | |
9 | 9 | abstract class AbstractThreadPoolMediator |
10 | 10 | { |
11 | - /** @var Mediator */ |
|
12 | - private $mediator; |
|
13 | - |
|
14 | - /** |
|
15 | - * @return Mediator |
|
16 | - */ |
|
17 | - public function getMediator() |
|
18 | - { |
|
19 | - return $this->mediator; |
|
20 | - } |
|
21 | - |
|
22 | - /** |
|
23 | - * ThreadPoolMediator constructor. |
|
24 | - */ |
|
25 | - public function __construct() |
|
26 | - { |
|
27 | - $this->mediator = new Mediator(); |
|
28 | - } |
|
29 | - |
|
30 | - /** |
|
31 | - * @param ListenerInterface $listener |
|
32 | - * @return AbstractThreadPoolMediator |
|
33 | - */ |
|
34 | - public function addListener(ListenerInterface $listener): self |
|
35 | - { |
|
36 | - $this->mediator->addListener($listener); |
|
37 | - |
|
38 | - return $this; |
|
39 | - } |
|
40 | - |
|
41 | - /** |
|
42 | - * @param ListenerInterface $listener |
|
43 | - * @return AbstractThreadPoolMediator |
|
44 | - */ |
|
45 | - public function removeListener(ListenerInterface $listener): self |
|
46 | - { |
|
47 | - $this->mediator->removeListener($listener); |
|
48 | - |
|
49 | - return $this; |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * @param string $eventName |
|
54 | - * @param Thread|null $thread |
|
55 | - */ |
|
56 | - public function notify(string $eventName, ?Thread $thread = null) |
|
57 | - { |
|
58 | - $this->getMediator()->notify($eventName, EventFactory::create($eventName, $this, $thread)); |
|
59 | - } |
|
11 | + /** @var Mediator */ |
|
12 | + private $mediator; |
|
13 | + |
|
14 | + /** |
|
15 | + * @return Mediator |
|
16 | + */ |
|
17 | + public function getMediator() |
|
18 | + { |
|
19 | + return $this->mediator; |
|
20 | + } |
|
21 | + |
|
22 | + /** |
|
23 | + * ThreadPoolMediator constructor. |
|
24 | + */ |
|
25 | + public function __construct() |
|
26 | + { |
|
27 | + $this->mediator = new Mediator(); |
|
28 | + } |
|
29 | + |
|
30 | + /** |
|
31 | + * @param ListenerInterface $listener |
|
32 | + * @return AbstractThreadPoolMediator |
|
33 | + */ |
|
34 | + public function addListener(ListenerInterface $listener): self |
|
35 | + { |
|
36 | + $this->mediator->addListener($listener); |
|
37 | + |
|
38 | + return $this; |
|
39 | + } |
|
40 | + |
|
41 | + /** |
|
42 | + * @param ListenerInterface $listener |
|
43 | + * @return AbstractThreadPoolMediator |
|
44 | + */ |
|
45 | + public function removeListener(ListenerInterface $listener): self |
|
46 | + { |
|
47 | + $this->mediator->removeListener($listener); |
|
48 | + |
|
49 | + return $this; |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * @param string $eventName |
|
54 | + * @param Thread|null $thread |
|
55 | + */ |
|
56 | + public function notify(string $eventName, ?Thread $thread = null) |
|
57 | + { |
|
58 | + $this->getMediator()->notify($eventName, EventFactory::create($eventName, $this, $thread)); |
|
59 | + } |
|
60 | 60 | |
61 | 61 | } |
@@ -2,5 +2,5 @@ |
||
2 | 2 | |
3 | 3 | namespace Wonderland\Thread\Exception; |
4 | 4 | |
5 | -class ThreadException extends \Exception{ |
|
5 | +class ThreadException extends \Exception { |
|
6 | 6 | } |
@@ -7,270 +7,270 @@ |
||
7 | 7 | |
8 | 8 | class ThreadPool extends AbstractThreadPoolMediator |
9 | 9 | { |
10 | - // 0.2s |
|
11 | - private const SLEEP_TIME_MS = 50000; |
|
12 | - |
|
13 | - /** @var Thread[] $childs */ |
|
14 | - private $threads; |
|
15 | - |
|
16 | - /** @var Thread[] $toRunThreads */ |
|
17 | - private $toRunThreads; |
|
18 | - |
|
19 | - /** @var Thread[] $runningChilds */ |
|
20 | - private $runningThreads; |
|
21 | - |
|
22 | - /** @var bool $isRunning */ |
|
23 | - private $isRunning; |
|
24 | - |
|
25 | - /** @var int $maxRunningThreadNb */ |
|
26 | - private $maxRunningThreadNb; |
|
27 | - |
|
28 | - /** |
|
29 | - * ThreadPool constructor. |
|
30 | - */ |
|
31 | - public function __construct() |
|
32 | - { |
|
33 | - parent::__construct(); |
|
34 | - $this->threads = []; |
|
35 | - $this->runningThreads = []; |
|
36 | - $this->toRunThreads = []; |
|
37 | - $this->isRunning = false; |
|
38 | - $this->maxRunningThreadNb = 0; |
|
39 | - } |
|
40 | - |
|
41 | - /** |
|
42 | - * |
|
43 | - */ |
|
44 | - public function __destruct() |
|
45 | - { |
|
46 | - pcntl_waitpid(-1, $status, WNOHANG); |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * @return Thread[] |
|
51 | - */ |
|
52 | - public function getThreads(): array |
|
53 | - { |
|
54 | - return $this->threads; |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * @param Thread[] $threads |
|
59 | - * @return ThreadPool |
|
60 | - */ |
|
61 | - public function setThreads(array $threads): self |
|
62 | - { |
|
63 | - $this->threads = $threads; |
|
64 | - |
|
65 | - return $this; |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * @param Thread $thread |
|
70 | - * @return ThreadPool |
|
71 | - */ |
|
72 | - public function addThread(Thread $thread): self |
|
73 | - { |
|
74 | - $this->threads[] = $thread; |
|
75 | - |
|
76 | - return $this; |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * @return int |
|
81 | - */ |
|
82 | - public function getMaxRunningThreadNb(): int |
|
83 | - { |
|
84 | - return $this->maxRunningThreadNb; |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * @param int $maxRunningThreadNb |
|
89 | - * @return ThreadPool |
|
90 | - */ |
|
91 | - public function setMaxRunningThreadNb(int $maxRunningThreadNb): self |
|
92 | - { |
|
93 | - $this->maxRunningThreadNb = $maxRunningThreadNb; |
|
94 | - |
|
95 | - return $this; |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * @return Thread[] |
|
100 | - */ |
|
101 | - public function getToRunThreads(): array |
|
102 | - { |
|
103 | - return $this->toRunThreads; |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * @return Thread[] |
|
108 | - */ |
|
109 | - public function getRunningThreads(): array |
|
110 | - { |
|
111 | - return $this->runningThreads; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * @throws ThreadException |
|
116 | - */ |
|
117 | - public function run() |
|
118 | - { |
|
119 | - $this->checkEnv(); |
|
120 | - $this->initRun(); |
|
121 | - |
|
122 | - while ($this->isRunningThreads()) { |
|
123 | - $this->waitOnThreads(); |
|
124 | - } |
|
125 | - |
|
126 | - $this->resetRun(); |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * @return bool |
|
131 | - * @throws ThreadException |
|
132 | - */ |
|
133 | - private function isRunningThreads(): bool |
|
134 | - { |
|
135 | - if (count($this->toRunThreads) > 0) { |
|
136 | - while (count($this->runningThreads) < $this->maxRunningThreadNb && count($this->toRunThreads) > 0) { |
|
137 | - $this->createThreadProcess(array_shift($this->toRunThreads)); |
|
138 | - } |
|
139 | - } |
|
140 | - |
|
141 | - return count($this->runningThreads) > 0; |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * can't test some part of it this since we can't unit-test in web and we're never in a child |
|
146 | - * process when pid 0 when unit-testing since the coverage is done by the parent thread |
|
147 | - * @param Thread $thread |
|
148 | - * @throws ThreadException |
|
149 | - */ |
|
150 | - private function createThreadProcess(Thread $thread) |
|
151 | - { |
|
152 | - $pid = pcntl_fork(); |
|
153 | - |
|
154 | - switch ($pid) { |
|
155 | - case -1: //error forking |
|
156 | - // @codeCoverageIgnoreStart |
|
157 | - throw new ThreadException('Error while trying to fork. Check your server installation'); |
|
158 | - // @codeCoverageIgnoreEnd |
|
159 | - case 0: // child |
|
160 | - // @codeCoverageIgnoreStart |
|
161 | - $this->processThread($thread); |
|
162 | - break; |
|
163 | - // @codeCoverageIgnoreEnd |
|
164 | - default: //parent |
|
165 | - $thread->setPid($pid); |
|
166 | - $this->runningThreads[] = $thread; |
|
167 | - $this->notify(Event::POOL_NEW_THREAD, $thread); |
|
168 | - $this->startRunStatus(); |
|
169 | - } |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * |
|
174 | - */ |
|
175 | - private function waitOnThreads() |
|
176 | - { |
|
177 | - $this->notify(Event::POOL_PRE_WAIT_TICK); |
|
178 | - foreach ($this->runningThreads as $k => $thread) { |
|
179 | - |
|
180 | - $res = pcntl_waitpid($thread->getPid(), $status, WNOHANG); |
|
181 | - $this->notify(Event::POOL_WAIT_TICK_PID); |
|
182 | - |
|
183 | - if ($res === -1 || $res > 0) { |
|
184 | - $this->notify(Event::POOL_WAIT_TICK_PID_REMOVED, $thread); |
|
185 | - unset($this->runningThreads[$k]); |
|
186 | - } |
|
187 | - |
|
188 | - } |
|
189 | - $this->notify(Event::POOL_POST_WAIT_TICK); |
|
190 | - |
|
191 | - usleep(self::SLEEP_TIME_MS); |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * @codeCoverageIgnore Can't test since this is only run in a child thread.. which doesnt' go throug the |
|
196 | - * unit-test coverage which is only done in the main process |
|
197 | - * @param Thread $thread |
|
198 | - * @throws ThreadException |
|
199 | - */ |
|
200 | - private function processThread(Thread $thread) |
|
201 | - { |
|
202 | - $this->notify(Event::THREAD_PRE_PROCESS, $thread); |
|
203 | - $response = $thread->run($thread->getProcessName()); |
|
204 | - $this->notify(Event::THREAD_POST_PROCESS, $thread); |
|
205 | - |
|
206 | - switch ($response) { |
|
207 | - case Thread::EXIT_STATUS_SUCCESS: |
|
208 | - $this->notify(Event::THREAD_EXIT_SUCCESS, $thread); |
|
209 | - break; |
|
210 | - case Thread::EXIT_STATUS_ERROR: |
|
211 | - $this->notify(Event::THREAD_EXIT_ERROR, $thread); |
|
212 | - break; |
|
213 | - default: |
|
214 | - $this->notify(Event::THREAD_EXIT_UNKNOWN, $thread); |
|
215 | - } |
|
216 | - |
|
217 | - exit($response); |
|
218 | - } |
|
219 | - |
|
220 | - /** |
|
221 | - * Can't test the exception is not in cli since php-unit is only run in cli environment |
|
222 | - * @throws ThreadException |
|
223 | - */ |
|
224 | - private function checkEnv() |
|
225 | - { |
|
226 | - if (false === $this->isCli()) { |
|
227 | - // @codeCoverageIgnoreStart |
|
228 | - throw new ThreadException('Error. It is not safe to use process forking in other way than php-cli'); |
|
229 | - // @codeCoverageIgnoreEnd |
|
230 | - } |
|
231 | - if (0 === count($this->threads)) { |
|
232 | - throw new ThreadException('Error. Can\'t run child threads processes without any added in the Pool'); |
|
233 | - } |
|
234 | - } |
|
235 | - |
|
236 | - /** |
|
237 | - * |
|
238 | - */ |
|
239 | - private function initRun() |
|
240 | - { |
|
241 | - $this->resetRun(); |
|
242 | - } |
|
243 | - |
|
244 | - /** |
|
245 | - * @return bool |
|
246 | - */ |
|
247 | - private function isCli(): bool |
|
248 | - { |
|
249 | - return PHP_SAPI === 'cli'; |
|
250 | - } |
|
251 | - |
|
252 | - /** |
|
253 | - * |
|
254 | - */ |
|
255 | - private function startRunStatus() |
|
256 | - { |
|
257 | - if (false === $this->isRunning) { |
|
258 | - $this->notify(Event::POOL_RUN_START); |
|
259 | - $this->isRunning = true; |
|
260 | - } |
|
261 | - } |
|
262 | - |
|
263 | - /** |
|
264 | - * |
|
265 | - */ |
|
266 | - private function resetRun() |
|
267 | - { |
|
268 | - if (true === $this->isRunning) { |
|
269 | - $this->notify(Event::POOL_RUN_STOP); |
|
270 | - } |
|
271 | - $this->isRunning = false; |
|
272 | - $this->toRunThreads = $this->threads; |
|
273 | - $this->runningThreads = []; |
|
274 | - } |
|
10 | + // 0.2s |
|
11 | + private const SLEEP_TIME_MS = 50000; |
|
12 | + |
|
13 | + /** @var Thread[] $childs */ |
|
14 | + private $threads; |
|
15 | + |
|
16 | + /** @var Thread[] $toRunThreads */ |
|
17 | + private $toRunThreads; |
|
18 | + |
|
19 | + /** @var Thread[] $runningChilds */ |
|
20 | + private $runningThreads; |
|
21 | + |
|
22 | + /** @var bool $isRunning */ |
|
23 | + private $isRunning; |
|
24 | + |
|
25 | + /** @var int $maxRunningThreadNb */ |
|
26 | + private $maxRunningThreadNb; |
|
27 | + |
|
28 | + /** |
|
29 | + * ThreadPool constructor. |
|
30 | + */ |
|
31 | + public function __construct() |
|
32 | + { |
|
33 | + parent::__construct(); |
|
34 | + $this->threads = []; |
|
35 | + $this->runningThreads = []; |
|
36 | + $this->toRunThreads = []; |
|
37 | + $this->isRunning = false; |
|
38 | + $this->maxRunningThreadNb = 0; |
|
39 | + } |
|
40 | + |
|
41 | + /** |
|
42 | + * |
|
43 | + */ |
|
44 | + public function __destruct() |
|
45 | + { |
|
46 | + pcntl_waitpid(-1, $status, WNOHANG); |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * @return Thread[] |
|
51 | + */ |
|
52 | + public function getThreads(): array |
|
53 | + { |
|
54 | + return $this->threads; |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * @param Thread[] $threads |
|
59 | + * @return ThreadPool |
|
60 | + */ |
|
61 | + public function setThreads(array $threads): self |
|
62 | + { |
|
63 | + $this->threads = $threads; |
|
64 | + |
|
65 | + return $this; |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * @param Thread $thread |
|
70 | + * @return ThreadPool |
|
71 | + */ |
|
72 | + public function addThread(Thread $thread): self |
|
73 | + { |
|
74 | + $this->threads[] = $thread; |
|
75 | + |
|
76 | + return $this; |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * @return int |
|
81 | + */ |
|
82 | + public function getMaxRunningThreadNb(): int |
|
83 | + { |
|
84 | + return $this->maxRunningThreadNb; |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * @param int $maxRunningThreadNb |
|
89 | + * @return ThreadPool |
|
90 | + */ |
|
91 | + public function setMaxRunningThreadNb(int $maxRunningThreadNb): self |
|
92 | + { |
|
93 | + $this->maxRunningThreadNb = $maxRunningThreadNb; |
|
94 | + |
|
95 | + return $this; |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * @return Thread[] |
|
100 | + */ |
|
101 | + public function getToRunThreads(): array |
|
102 | + { |
|
103 | + return $this->toRunThreads; |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * @return Thread[] |
|
108 | + */ |
|
109 | + public function getRunningThreads(): array |
|
110 | + { |
|
111 | + return $this->runningThreads; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * @throws ThreadException |
|
116 | + */ |
|
117 | + public function run() |
|
118 | + { |
|
119 | + $this->checkEnv(); |
|
120 | + $this->initRun(); |
|
121 | + |
|
122 | + while ($this->isRunningThreads()) { |
|
123 | + $this->waitOnThreads(); |
|
124 | + } |
|
125 | + |
|
126 | + $this->resetRun(); |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * @return bool |
|
131 | + * @throws ThreadException |
|
132 | + */ |
|
133 | + private function isRunningThreads(): bool |
|
134 | + { |
|
135 | + if (count($this->toRunThreads) > 0) { |
|
136 | + while (count($this->runningThreads) < $this->maxRunningThreadNb && count($this->toRunThreads) > 0) { |
|
137 | + $this->createThreadProcess(array_shift($this->toRunThreads)); |
|
138 | + } |
|
139 | + } |
|
140 | + |
|
141 | + return count($this->runningThreads) > 0; |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * can't test some part of it this since we can't unit-test in web and we're never in a child |
|
146 | + * process when pid 0 when unit-testing since the coverage is done by the parent thread |
|
147 | + * @param Thread $thread |
|
148 | + * @throws ThreadException |
|
149 | + */ |
|
150 | + private function createThreadProcess(Thread $thread) |
|
151 | + { |
|
152 | + $pid = pcntl_fork(); |
|
153 | + |
|
154 | + switch ($pid) { |
|
155 | + case -1: //error forking |
|
156 | + // @codeCoverageIgnoreStart |
|
157 | + throw new ThreadException('Error while trying to fork. Check your server installation'); |
|
158 | + // @codeCoverageIgnoreEnd |
|
159 | + case 0: // child |
|
160 | + // @codeCoverageIgnoreStart |
|
161 | + $this->processThread($thread); |
|
162 | + break; |
|
163 | + // @codeCoverageIgnoreEnd |
|
164 | + default: //parent |
|
165 | + $thread->setPid($pid); |
|
166 | + $this->runningThreads[] = $thread; |
|
167 | + $this->notify(Event::POOL_NEW_THREAD, $thread); |
|
168 | + $this->startRunStatus(); |
|
169 | + } |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * |
|
174 | + */ |
|
175 | + private function waitOnThreads() |
|
176 | + { |
|
177 | + $this->notify(Event::POOL_PRE_WAIT_TICK); |
|
178 | + foreach ($this->runningThreads as $k => $thread) { |
|
179 | + |
|
180 | + $res = pcntl_waitpid($thread->getPid(), $status, WNOHANG); |
|
181 | + $this->notify(Event::POOL_WAIT_TICK_PID); |
|
182 | + |
|
183 | + if ($res === -1 || $res > 0) { |
|
184 | + $this->notify(Event::POOL_WAIT_TICK_PID_REMOVED, $thread); |
|
185 | + unset($this->runningThreads[$k]); |
|
186 | + } |
|
187 | + |
|
188 | + } |
|
189 | + $this->notify(Event::POOL_POST_WAIT_TICK); |
|
190 | + |
|
191 | + usleep(self::SLEEP_TIME_MS); |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * @codeCoverageIgnore Can't test since this is only run in a child thread.. which doesnt' go throug the |
|
196 | + * unit-test coverage which is only done in the main process |
|
197 | + * @param Thread $thread |
|
198 | + * @throws ThreadException |
|
199 | + */ |
|
200 | + private function processThread(Thread $thread) |
|
201 | + { |
|
202 | + $this->notify(Event::THREAD_PRE_PROCESS, $thread); |
|
203 | + $response = $thread->run($thread->getProcessName()); |
|
204 | + $this->notify(Event::THREAD_POST_PROCESS, $thread); |
|
205 | + |
|
206 | + switch ($response) { |
|
207 | + case Thread::EXIT_STATUS_SUCCESS: |
|
208 | + $this->notify(Event::THREAD_EXIT_SUCCESS, $thread); |
|
209 | + break; |
|
210 | + case Thread::EXIT_STATUS_ERROR: |
|
211 | + $this->notify(Event::THREAD_EXIT_ERROR, $thread); |
|
212 | + break; |
|
213 | + default: |
|
214 | + $this->notify(Event::THREAD_EXIT_UNKNOWN, $thread); |
|
215 | + } |
|
216 | + |
|
217 | + exit($response); |
|
218 | + } |
|
219 | + |
|
220 | + /** |
|
221 | + * Can't test the exception is not in cli since php-unit is only run in cli environment |
|
222 | + * @throws ThreadException |
|
223 | + */ |
|
224 | + private function checkEnv() |
|
225 | + { |
|
226 | + if (false === $this->isCli()) { |
|
227 | + // @codeCoverageIgnoreStart |
|
228 | + throw new ThreadException('Error. It is not safe to use process forking in other way than php-cli'); |
|
229 | + // @codeCoverageIgnoreEnd |
|
230 | + } |
|
231 | + if (0 === count($this->threads)) { |
|
232 | + throw new ThreadException('Error. Can\'t run child threads processes without any added in the Pool'); |
|
233 | + } |
|
234 | + } |
|
235 | + |
|
236 | + /** |
|
237 | + * |
|
238 | + */ |
|
239 | + private function initRun() |
|
240 | + { |
|
241 | + $this->resetRun(); |
|
242 | + } |
|
243 | + |
|
244 | + /** |
|
245 | + * @return bool |
|
246 | + */ |
|
247 | + private function isCli(): bool |
|
248 | + { |
|
249 | + return PHP_SAPI === 'cli'; |
|
250 | + } |
|
251 | + |
|
252 | + /** |
|
253 | + * |
|
254 | + */ |
|
255 | + private function startRunStatus() |
|
256 | + { |
|
257 | + if (false === $this->isRunning) { |
|
258 | + $this->notify(Event::POOL_RUN_START); |
|
259 | + $this->isRunning = true; |
|
260 | + } |
|
261 | + } |
|
262 | + |
|
263 | + /** |
|
264 | + * |
|
265 | + */ |
|
266 | + private function resetRun() |
|
267 | + { |
|
268 | + if (true === $this->isRunning) { |
|
269 | + $this->notify(Event::POOL_RUN_STOP); |
|
270 | + } |
|
271 | + $this->isRunning = false; |
|
272 | + $this->toRunThreads = $this->threads; |
|
273 | + $this->runningThreads = []; |
|
274 | + } |
|
275 | 275 | |
276 | 276 | } |
@@ -10,23 +10,23 @@ |
||
10 | 10 | class EventFactory |
11 | 11 | { |
12 | 12 | |
13 | - /** |
|
14 | - * @param string $eventName |
|
15 | - * @param ThreadPool|AbstractThreadPoolMediator $pool |
|
16 | - * @param Thread|null $thread |
|
17 | - * @return Event |
|
18 | - */ |
|
19 | - public static function create(string $eventName, AbstractThreadPoolMediator $pool, ?Thread $thread = null): Event |
|
20 | - { |
|
21 | - $event = new Event($eventName); |
|
22 | - $event->setThreadNb(count($pool->getThreads())); |
|
23 | - $event->setThreadDoneNb(count($pool->getThreads()) - count($pool->getToRunThreads())); |
|
24 | - $event->setMaxRunningThreadNb($pool->getMaxRunningThreadNb()); |
|
25 | - $event->setThreadLeftNb(count($pool->getToRunThreads())); |
|
26 | - $event->setRunningThreadNb(count($pool->getRunningThreads())); |
|
27 | - $event->setThread($thread); |
|
13 | + /** |
|
14 | + * @param string $eventName |
|
15 | + * @param ThreadPool|AbstractThreadPoolMediator $pool |
|
16 | + * @param Thread|null $thread |
|
17 | + * @return Event |
|
18 | + */ |
|
19 | + public static function create(string $eventName, AbstractThreadPoolMediator $pool, ?Thread $thread = null): Event |
|
20 | + { |
|
21 | + $event = new Event($eventName); |
|
22 | + $event->setThreadNb(count($pool->getThreads())); |
|
23 | + $event->setThreadDoneNb(count($pool->getThreads()) - count($pool->getToRunThreads())); |
|
24 | + $event->setMaxRunningThreadNb($pool->getMaxRunningThreadNb()); |
|
25 | + $event->setThreadLeftNb(count($pool->getToRunThreads())); |
|
26 | + $event->setRunningThreadNb(count($pool->getRunningThreads())); |
|
27 | + $event->setThread($thread); |
|
28 | 28 | |
29 | - return $event; |
|
30 | - } |
|
29 | + return $event; |
|
30 | + } |
|
31 | 31 | |
32 | 32 | } |
@@ -7,18 +7,18 @@ |
||
7 | 7 | class ThreadFactory |
8 | 8 | { |
9 | 9 | |
10 | - /** |
|
11 | - * @param $processName |
|
12 | - * @param callable $function |
|
13 | - * @return Thread |
|
14 | - */ |
|
15 | - public static function create(string $processName, callable $function): Thread |
|
16 | - { |
|
17 | - $thread = new Thread(); |
|
18 | - $thread->setCallback($function); |
|
19 | - $thread->setProcessName($processName); |
|
10 | + /** |
|
11 | + * @param $processName |
|
12 | + * @param callable $function |
|
13 | + * @return Thread |
|
14 | + */ |
|
15 | + public static function create(string $processName, callable $function): Thread |
|
16 | + { |
|
17 | + $thread = new Thread(); |
|
18 | + $thread->setCallback($function); |
|
19 | + $thread->setProcessName($processName); |
|
20 | 20 | |
21 | - return $thread; |
|
22 | - } |
|
21 | + return $thread; |
|
22 | + } |
|
23 | 23 | |
24 | 24 | } |