@@ -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 | } |
@@ -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 | } |
@@ -20,7 +20,7 @@ discard block |
||
20 | 20 | |
21 | 21 | public function setUp() |
22 | 22 | { |
23 | - $this->callback = function (){ return 0; |
|
23 | + $this->callback = function() { return 0; |
|
24 | 24 | |
25 | 25 | }; |
26 | 26 | |
@@ -46,7 +46,7 @@ discard block |
||
46 | 46 | |
47 | 47 | public function test_callback() |
48 | 48 | { |
49 | - $callback = function(){ return 1; |
|
49 | + $callback = function() { return 1; |
|
50 | 50 | |
51 | 51 | }; |
52 | 52 |
@@ -13,78 +13,78 @@ |
||
13 | 13 | */ |
14 | 14 | class ThreadTest extends TestCase |
15 | 15 | { |
16 | - /** @var AbstractThread */ |
|
17 | - private $thread; |
|
18 | - |
|
19 | - private $callback; |
|
20 | - |
|
21 | - public function setUp() |
|
22 | - { |
|
23 | - $this->callback = function (){ return 0; |
|
24 | - |
|
25 | - }; |
|
26 | - |
|
27 | - $this->thread = new AbstractThread(); |
|
28 | - $this->thread->setPid(1); |
|
29 | - $this->thread->setProcessName('unit-test'); |
|
30 | - $this->thread->setCallback($this->callback); |
|
31 | - } |
|
32 | - |
|
33 | - public function test_pid() |
|
34 | - { |
|
35 | - $this->assertSame(1, $this->thread->getPid()); |
|
36 | - $this->assertSame($this->thread, $this->thread->setPid(100)); |
|
37 | - $this->assertSame(100, $this->thread->getPid()); |
|
38 | - } |
|
39 | - |
|
40 | - public function test_processName() |
|
41 | - { |
|
42 | - $this->assertSame('unit-test', $this->thread->getProcessName()); |
|
43 | - $this->assertSame($this->thread, $this->thread->setProcessName('unit')); |
|
44 | - $this->assertSame('unit', $this->thread->getProcessName()); |
|
45 | - } |
|
46 | - |
|
47 | - public function test_callback() |
|
48 | - { |
|
49 | - $callback = function(){ return 1; |
|
50 | - |
|
51 | - }; |
|
52 | - |
|
53 | - $this->assertSame($this->callback, $this->thread->getCallback()); |
|
54 | - $this->assertSame($this->thread, $this->thread->setCallback($callback)); |
|
55 | - $this->assertSame($callback, $this->thread->getCallback()); |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * @throws ThreadException |
|
60 | - */ |
|
61 | - public function test_run() |
|
62 | - { |
|
63 | - $this->assertSame(0, $this->thread->run('unit-test')); |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * @throws ThreadException |
|
68 | - */ |
|
69 | - public function test_run_exception() |
|
70 | - { |
|
71 | - $this->expectException(ThreadException::class); |
|
72 | - $thread = new AbstractThread(); |
|
73 | - $thread->run('unit-test'); |
|
74 | - |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * @throws ThreadException |
|
79 | - */ |
|
80 | - public function test_run_exception_status() |
|
81 | - { |
|
82 | - $this->expectException(ThreadException::class); |
|
83 | - $thread = new AbstractThread(); |
|
84 | - $thread->setCallback(function() { return null; |
|
85 | - |
|
86 | - }); |
|
87 | - $thread->run('unit-test'); |
|
88 | - } |
|
16 | + /** @var AbstractThread */ |
|
17 | + private $thread; |
|
18 | + |
|
19 | + private $callback; |
|
20 | + |
|
21 | + public function setUp() |
|
22 | + { |
|
23 | + $this->callback = function (){ return 0; |
|
24 | + |
|
25 | + }; |
|
26 | + |
|
27 | + $this->thread = new AbstractThread(); |
|
28 | + $this->thread->setPid(1); |
|
29 | + $this->thread->setProcessName('unit-test'); |
|
30 | + $this->thread->setCallback($this->callback); |
|
31 | + } |
|
32 | + |
|
33 | + public function test_pid() |
|
34 | + { |
|
35 | + $this->assertSame(1, $this->thread->getPid()); |
|
36 | + $this->assertSame($this->thread, $this->thread->setPid(100)); |
|
37 | + $this->assertSame(100, $this->thread->getPid()); |
|
38 | + } |
|
39 | + |
|
40 | + public function test_processName() |
|
41 | + { |
|
42 | + $this->assertSame('unit-test', $this->thread->getProcessName()); |
|
43 | + $this->assertSame($this->thread, $this->thread->setProcessName('unit')); |
|
44 | + $this->assertSame('unit', $this->thread->getProcessName()); |
|
45 | + } |
|
46 | + |
|
47 | + public function test_callback() |
|
48 | + { |
|
49 | + $callback = function(){ return 1; |
|
50 | + |
|
51 | + }; |
|
52 | + |
|
53 | + $this->assertSame($this->callback, $this->thread->getCallback()); |
|
54 | + $this->assertSame($this->thread, $this->thread->setCallback($callback)); |
|
55 | + $this->assertSame($callback, $this->thread->getCallback()); |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * @throws ThreadException |
|
60 | + */ |
|
61 | + public function test_run() |
|
62 | + { |
|
63 | + $this->assertSame(0, $this->thread->run('unit-test')); |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * @throws ThreadException |
|
68 | + */ |
|
69 | + public function test_run_exception() |
|
70 | + { |
|
71 | + $this->expectException(ThreadException::class); |
|
72 | + $thread = new AbstractThread(); |
|
73 | + $thread->run('unit-test'); |
|
74 | + |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * @throws ThreadException |
|
79 | + */ |
|
80 | + public function test_run_exception_status() |
|
81 | + { |
|
82 | + $this->expectException(ThreadException::class); |
|
83 | + $thread = new AbstractThread(); |
|
84 | + $thread->setCallback(function() { return null; |
|
85 | + |
|
86 | + }); |
|
87 | + $thread->run('unit-test'); |
|
88 | + } |
|
89 | 89 | |
90 | 90 | } |
@@ -2,7 +2,7 @@ discard block |
||
2 | 2 | declare( strict_types = 1 ); |
3 | 3 | |
4 | 4 | if ( PHP_SAPI !== 'cli' ) { |
5 | - die( 'Not an entry point' ); |
|
5 | + die( 'Not an entry point' ); |
|
6 | 6 | } |
7 | 7 | |
8 | 8 | error_reporting( -1 ); |
@@ -10,7 +10,7 @@ discard block |
||
10 | 10 | ini_set( 'display_errors', '1' ); |
11 | 11 | |
12 | 12 | if ( !is_readable( __DIR__ . '/../vendor/autoload.php' ) ) { |
13 | - die( 'You need to install this package with Composer before you can run the tests' ); |
|
13 | + die( 'You need to install this package with Composer before you can run the tests' ); |
|
14 | 14 | } |
15 | 15 | |
16 | 16 | require_once __DIR__ . '/../vendor/autoload.php'; |
@@ -1,16 +1,16 @@ |
||
1 | 1 | <?php |
2 | -declare( strict_types = 1 ); |
|
2 | +declare(strict_types=1); |
|
3 | 3 | |
4 | -if ( PHP_SAPI !== 'cli' ) { |
|
5 | - die( 'Not an entry point' ); |
|
4 | +if (PHP_SAPI !== 'cli') { |
|
5 | + die('Not an entry point'); |
|
6 | 6 | } |
7 | 7 | |
8 | 8 | error_reporting( -1 ); |
9 | 9 | |
10 | -ini_set( 'display_errors', '1' ); |
|
10 | +ini_set('display_errors', '1'); |
|
11 | 11 | |
12 | -if ( !is_readable( __DIR__ . '/../vendor/autoload.php' ) ) { |
|
13 | - die( 'You need to install this package with Composer before you can run the tests' ); |
|
12 | +if (!is_readable(__DIR__ . '/../vendor/autoload.php')) { |
|
13 | + die('You need to install this package with Composer before you can run the tests'); |
|
14 | 14 | } |
15 | 15 | |
16 | 16 | require_once __DIR__ . '/../vendor/autoload.php'; |
@@ -7,49 +7,49 @@ |
||
7 | 7 | |
8 | 8 | class Mediator |
9 | 9 | { |
10 | - /** @var ListenerInterface[] */ |
|
11 | - private $listeners = []; |
|
12 | - |
|
13 | - /** |
|
14 | - * @return ListenerInterface[] |
|
15 | - */ |
|
16 | - public function getListeners(): array |
|
17 | - { |
|
18 | - return $this->listeners; |
|
19 | - } |
|
20 | - |
|
21 | - /** |
|
22 | - * @param ListenerInterface $listener |
|
23 | - * @return Mediator |
|
24 | - */ |
|
25 | - public function addListener(ListenerInterface $listener): self |
|
26 | - { |
|
27 | - $this->listeners[] = $listener; |
|
28 | - |
|
29 | - return $this; |
|
30 | - } |
|
31 | - |
|
32 | - /** |
|
33 | - * @param ListenerInterface $listener |
|
34 | - * @return Mediator |
|
35 | - */ |
|
36 | - public function removeListener(ListenerInterface $listener): self |
|
37 | - { |
|
10 | + /** @var ListenerInterface[] */ |
|
11 | + private $listeners = []; |
|
12 | + |
|
13 | + /** |
|
14 | + * @return ListenerInterface[] |
|
15 | + */ |
|
16 | + public function getListeners(): array |
|
17 | + { |
|
18 | + return $this->listeners; |
|
19 | + } |
|
20 | + |
|
21 | + /** |
|
22 | + * @param ListenerInterface $listener |
|
23 | + * @return Mediator |
|
24 | + */ |
|
25 | + public function addListener(ListenerInterface $listener): self |
|
26 | + { |
|
27 | + $this->listeners[] = $listener; |
|
28 | + |
|
29 | + return $this; |
|
30 | + } |
|
31 | + |
|
32 | + /** |
|
33 | + * @param ListenerInterface $listener |
|
34 | + * @return Mediator |
|
35 | + */ |
|
36 | + public function removeListener(ListenerInterface $listener): self |
|
37 | + { |
|
38 | 38 | if (false !== ($key = array_search($listener, $this->listeners, true))) { |
39 | 39 | unset($this->listeners[$key]); |
40 | 40 | } |
41 | 41 | |
42 | - return $this; |
|
43 | - } |
|
44 | - |
|
45 | - /** |
|
46 | - * @param EventInterface $event |
|
47 | - */ |
|
48 | - public function notify(EventInterface $event) |
|
49 | - { |
|
50 | - foreach ($this->listeners as $listener) { |
|
51 | - $listener->notify($event); |
|
52 | - } |
|
53 | - } |
|
42 | + return $this; |
|
43 | + } |
|
44 | + |
|
45 | + /** |
|
46 | + * @param EventInterface $event |
|
47 | + */ |
|
48 | + public function notify(EventInterface $event) |
|
49 | + { |
|
50 | + foreach ($this->listeners as $listener) { |
|
51 | + $listener->notify($event); |
|
52 | + } |
|
53 | + } |
|
54 | 54 | |
55 | 55 | } |
@@ -9,16 +9,16 @@ |
||
9 | 9 | /** |
10 | 10 | * @param EventInterface $event |
11 | 11 | */ |
12 | - public function notify(EventInterface $event) |
|
13 | - { |
|
14 | - $mapping = $this->getEventMapping(); |
|
15 | - if (false === isset($mapping[$event->getEventName()])) { |
|
16 | - return; |
|
12 | + public function notify(EventInterface $event) |
|
13 | + { |
|
14 | + $mapping = $this->getEventMapping(); |
|
15 | + if (false === isset($mapping[$event->getEventName()])) { |
|
16 | + return; |
|
17 | 17 | } |
18 | 18 | |
19 | 19 | call_user_func( |
20 | 20 | [$this, $mapping[$event->getEventName()]], |
21 | 21 | $event |
22 | 22 | ); |
23 | - } |
|
23 | + } |
|
24 | 24 | } |
@@ -6,15 +6,15 @@ |
||
6 | 6 | |
7 | 7 | interface ListenerInterface |
8 | 8 | { |
9 | - /** |
|
10 | - * @return array |
|
11 | - */ |
|
12 | - public function getEventMapping(): array; |
|
9 | + /** |
|
10 | + * @return array |
|
11 | + */ |
|
12 | + public function getEventMapping(): array; |
|
13 | 13 | |
14 | - /** |
|
15 | - * @param EventInterface $event |
|
16 | - * @return void |
|
17 | - */ |
|
18 | - public function notify(EventInterface $event); |
|
14 | + /** |
|
15 | + * @param EventInterface $event |
|
16 | + * @return void |
|
17 | + */ |
|
18 | + public function notify(EventInterface $event); |
|
19 | 19 | |
20 | 20 | } |
@@ -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 | - protected 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 AbstractThread|null $thread |
|
55 | - */ |
|
56 | - protected function notify(string $eventName, ?AbstractThread $thread = null) |
|
57 | - { |
|
58 | - $this->getMediator()->notify(EventPoolFactory::create($eventName, $this, $thread)); |
|
59 | - } |
|
11 | + /** @var Mediator */ |
|
12 | + private $mediator; |
|
13 | + |
|
14 | + /** |
|
15 | + * @return Mediator |
|
16 | + */ |
|
17 | + protected 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 AbstractThread|null $thread |
|
55 | + */ |
|
56 | + protected function notify(string $eventName, ?AbstractThread $thread = null) |
|
57 | + { |
|
58 | + $this->getMediator()->notify(EventPoolFactory::create($eventName, $this, $thread)); |
|
59 | + } |
|
60 | 60 | |
61 | 61 | } |
@@ -7,272 +7,272 @@ |
||
7 | 7 | |
8 | 8 | class ThreadPool extends AbstractThreadPoolMediator |
9 | 9 | { |
10 | - // 0.2s |
|
11 | - private const SLEEP_TIME_MS = 50000; |
|
12 | - |
|
13 | - /** @var AbstractThread[] $childs */ |
|
14 | - private $threads; |
|
15 | - |
|
16 | - /** @var AbstractThread[] $toRunThreads */ |
|
17 | - private $toRunThreads; |
|
18 | - |
|
19 | - /** @var AbstractThread[] $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 AbstractThread[] |
|
51 | - */ |
|
52 | - public function getThreads(): array |
|
53 | - { |
|
54 | - return $this->threads; |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * @param AbstractThread[] $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 AbstractThread $thread |
|
70 | - * @return ThreadPool |
|
71 | - */ |
|
72 | - public function addThread(AbstractThread $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 AbstractThread[] |
|
100 | - */ |
|
101 | - public function getToRunThreads(): array |
|
102 | - { |
|
103 | - return $this->toRunThreads; |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * @return AbstractThread[] |
|
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 | - * |
|
148 | - * @param AbstractThread $thread |
|
149 | - * @throws ThreadException |
|
150 | - */ |
|
151 | - private function createThreadProcess(AbstractThread $thread) |
|
152 | - { |
|
153 | - $pid = pcntl_fork(); |
|
154 | - |
|
155 | - switch ($pid) { |
|
156 | - case -1: //error forking |
|
157 | - // @codeCoverageIgnoreStart |
|
158 | - throw new ThreadException('Error while trying to fork. Check your server installation'); |
|
159 | - // @codeCoverageIgnoreEnd |
|
160 | - case 0: // child |
|
161 | - // @codeCoverageIgnoreStart |
|
10 | + // 0.2s |
|
11 | + private const SLEEP_TIME_MS = 50000; |
|
12 | + |
|
13 | + /** @var AbstractThread[] $childs */ |
|
14 | + private $threads; |
|
15 | + |
|
16 | + /** @var AbstractThread[] $toRunThreads */ |
|
17 | + private $toRunThreads; |
|
18 | + |
|
19 | + /** @var AbstractThread[] $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 AbstractThread[] |
|
51 | + */ |
|
52 | + public function getThreads(): array |
|
53 | + { |
|
54 | + return $this->threads; |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * @param AbstractThread[] $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 AbstractThread $thread |
|
70 | + * @return ThreadPool |
|
71 | + */ |
|
72 | + public function addThread(AbstractThread $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 AbstractThread[] |
|
100 | + */ |
|
101 | + public function getToRunThreads(): array |
|
102 | + { |
|
103 | + return $this->toRunThreads; |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * @return AbstractThread[] |
|
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 | + * |
|
148 | + * @param AbstractThread $thread |
|
149 | + * @throws ThreadException |
|
150 | + */ |
|
151 | + private function createThreadProcess(AbstractThread $thread) |
|
152 | + { |
|
153 | + $pid = pcntl_fork(); |
|
154 | + |
|
155 | + switch ($pid) { |
|
156 | + case -1: //error forking |
|
157 | + // @codeCoverageIgnoreStart |
|
158 | + throw new ThreadException('Error while trying to fork. Check your server installation'); |
|
159 | + // @codeCoverageIgnoreEnd |
|
160 | + case 0: // child |
|
161 | + // @codeCoverageIgnoreStart |
|
162 | 162 | $thread->setMediator($this->getMediator()); |
163 | - $this->processThread($thread); |
|
164 | - break; |
|
165 | - // @codeCoverageIgnoreEnd |
|
166 | - default: //parent |
|
167 | - $thread->setPid($pid); |
|
168 | - $this->runningThreads[] = $thread; |
|
169 | - $this->notify(PoolEvent::POOL_NEW_THREAD, $thread); |
|
170 | - $this->startRunStatus(); |
|
171 | - } |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * |
|
176 | - */ |
|
177 | - private function waitOnThreads() |
|
178 | - { |
|
179 | - $this->notify(PoolEvent::POOL_PRE_WAIT_TICK); |
|
180 | - foreach ($this->runningThreads as $k => $thread) { |
|
181 | - |
|
182 | - $res = pcntl_waitpid($thread->getPid(), $status, WNOHANG); |
|
183 | - $this->notify(PoolEvent::POOL_WAIT_TICK_PID); |
|
184 | - |
|
185 | - if ($res === -1 || $res > 0) { |
|
186 | - $this->notify(PoolEvent::POOL_WAIT_TICK_PID_REMOVED, $thread); |
|
187 | - unset($this->runningThreads[$k]); |
|
188 | - } |
|
189 | - |
|
190 | - } |
|
191 | - $this->notify(PoolEvent::POOL_POST_WAIT_TICK); |
|
192 | - |
|
193 | - usleep(self::SLEEP_TIME_MS); |
|
194 | - } |
|
195 | - |
|
196 | - /** |
|
197 | - * @codeCoverageIgnore Can't test since this is only run in a child thread.. which doesnt' go throug the |
|
198 | - * unit-test coverage which is only done in the main process |
|
199 | - * @param AbstractThread $thread |
|
200 | - * @throws ThreadException |
|
201 | - */ |
|
202 | - private function processThread(AbstractThread $thread) |
|
203 | - { |
|
204 | - $this->notify(PoolEvent::THREAD_PRE_PROCESS, $thread); |
|
205 | - $response = $thread->run(); |
|
206 | - $this->notify(PoolEvent::THREAD_POST_PROCESS, $thread); |
|
207 | - |
|
208 | - switch ($response) { |
|
209 | - case AbstractThread::EXIT_STATUS_SUCCESS: |
|
210 | - $this->notify(PoolEvent::THREAD_EXIT_SUCCESS, $thread); |
|
211 | - break; |
|
212 | - case AbstractThread::EXIT_STATUS_ERROR: |
|
213 | - $this->notify(PoolEvent::THREAD_EXIT_ERROR, $thread); |
|
214 | - break; |
|
215 | - default: |
|
216 | - $this->notify(PoolEvent::THREAD_EXIT_UNKNOWN, $thread); |
|
217 | - } |
|
218 | - |
|
219 | - exit($response); |
|
220 | - } |
|
221 | - |
|
222 | - /** |
|
223 | - * Can't test the exception is not in cli since php-unit is only run in cli environment |
|
224 | - * @throws ThreadException |
|
225 | - */ |
|
226 | - private function checkEnv() |
|
227 | - { |
|
228 | - if (false === $this->isCli()) { |
|
229 | - // @codeCoverageIgnoreStart |
|
230 | - throw new ThreadException('Error. It is not safe to use process forking in other way than php-cli'); |
|
231 | - // @codeCoverageIgnoreEnd |
|
232 | - } |
|
233 | - if (0 === count($this->threads)) { |
|
234 | - throw new ThreadException('Error. Can\'t run child threads processes without any added in the Pool'); |
|
235 | - } |
|
236 | - } |
|
237 | - |
|
238 | - /** |
|
239 | - * |
|
240 | - */ |
|
241 | - private function initRun() |
|
242 | - { |
|
243 | - $this->resetRun(); |
|
244 | - } |
|
245 | - |
|
246 | - /** |
|
247 | - * @return bool |
|
248 | - */ |
|
249 | - private function isCli(): bool |
|
250 | - { |
|
251 | - return PHP_SAPI === 'cli'; |
|
252 | - } |
|
253 | - |
|
254 | - /** |
|
255 | - * |
|
256 | - */ |
|
257 | - private function startRunStatus() |
|
258 | - { |
|
259 | - if (false === $this->isRunning) { |
|
260 | - $this->notify(PoolEvent::POOL_RUN_START); |
|
261 | - $this->isRunning = true; |
|
262 | - } |
|
263 | - } |
|
264 | - |
|
265 | - /** |
|
266 | - * |
|
267 | - */ |
|
268 | - private function resetRun() |
|
269 | - { |
|
270 | - if (true === $this->isRunning) { |
|
271 | - $this->notify(PoolEvent::POOL_RUN_STOP); |
|
272 | - } |
|
273 | - $this->isRunning = false; |
|
274 | - $this->toRunThreads = $this->threads; |
|
275 | - $this->runningThreads = []; |
|
276 | - } |
|
163 | + $this->processThread($thread); |
|
164 | + break; |
|
165 | + // @codeCoverageIgnoreEnd |
|
166 | + default: //parent |
|
167 | + $thread->setPid($pid); |
|
168 | + $this->runningThreads[] = $thread; |
|
169 | + $this->notify(PoolEvent::POOL_NEW_THREAD, $thread); |
|
170 | + $this->startRunStatus(); |
|
171 | + } |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * |
|
176 | + */ |
|
177 | + private function waitOnThreads() |
|
178 | + { |
|
179 | + $this->notify(PoolEvent::POOL_PRE_WAIT_TICK); |
|
180 | + foreach ($this->runningThreads as $k => $thread) { |
|
181 | + |
|
182 | + $res = pcntl_waitpid($thread->getPid(), $status, WNOHANG); |
|
183 | + $this->notify(PoolEvent::POOL_WAIT_TICK_PID); |
|
184 | + |
|
185 | + if ($res === -1 || $res > 0) { |
|
186 | + $this->notify(PoolEvent::POOL_WAIT_TICK_PID_REMOVED, $thread); |
|
187 | + unset($this->runningThreads[$k]); |
|
188 | + } |
|
189 | + |
|
190 | + } |
|
191 | + $this->notify(PoolEvent::POOL_POST_WAIT_TICK); |
|
192 | + |
|
193 | + usleep(self::SLEEP_TIME_MS); |
|
194 | + } |
|
195 | + |
|
196 | + /** |
|
197 | + * @codeCoverageIgnore Can't test since this is only run in a child thread.. which doesnt' go throug the |
|
198 | + * unit-test coverage which is only done in the main process |
|
199 | + * @param AbstractThread $thread |
|
200 | + * @throws ThreadException |
|
201 | + */ |
|
202 | + private function processThread(AbstractThread $thread) |
|
203 | + { |
|
204 | + $this->notify(PoolEvent::THREAD_PRE_PROCESS, $thread); |
|
205 | + $response = $thread->run(); |
|
206 | + $this->notify(PoolEvent::THREAD_POST_PROCESS, $thread); |
|
207 | + |
|
208 | + switch ($response) { |
|
209 | + case AbstractThread::EXIT_STATUS_SUCCESS: |
|
210 | + $this->notify(PoolEvent::THREAD_EXIT_SUCCESS, $thread); |
|
211 | + break; |
|
212 | + case AbstractThread::EXIT_STATUS_ERROR: |
|
213 | + $this->notify(PoolEvent::THREAD_EXIT_ERROR, $thread); |
|
214 | + break; |
|
215 | + default: |
|
216 | + $this->notify(PoolEvent::THREAD_EXIT_UNKNOWN, $thread); |
|
217 | + } |
|
218 | + |
|
219 | + exit($response); |
|
220 | + } |
|
221 | + |
|
222 | + /** |
|
223 | + * Can't test the exception is not in cli since php-unit is only run in cli environment |
|
224 | + * @throws ThreadException |
|
225 | + */ |
|
226 | + private function checkEnv() |
|
227 | + { |
|
228 | + if (false === $this->isCli()) { |
|
229 | + // @codeCoverageIgnoreStart |
|
230 | + throw new ThreadException('Error. It is not safe to use process forking in other way than php-cli'); |
|
231 | + // @codeCoverageIgnoreEnd |
|
232 | + } |
|
233 | + if (0 === count($this->threads)) { |
|
234 | + throw new ThreadException('Error. Can\'t run child threads processes without any added in the Pool'); |
|
235 | + } |
|
236 | + } |
|
237 | + |
|
238 | + /** |
|
239 | + * |
|
240 | + */ |
|
241 | + private function initRun() |
|
242 | + { |
|
243 | + $this->resetRun(); |
|
244 | + } |
|
245 | + |
|
246 | + /** |
|
247 | + * @return bool |
|
248 | + */ |
|
249 | + private function isCli(): bool |
|
250 | + { |
|
251 | + return PHP_SAPI === 'cli'; |
|
252 | + } |
|
253 | + |
|
254 | + /** |
|
255 | + * |
|
256 | + */ |
|
257 | + private function startRunStatus() |
|
258 | + { |
|
259 | + if (false === $this->isRunning) { |
|
260 | + $this->notify(PoolEvent::POOL_RUN_START); |
|
261 | + $this->isRunning = true; |
|
262 | + } |
|
263 | + } |
|
264 | + |
|
265 | + /** |
|
266 | + * |
|
267 | + */ |
|
268 | + private function resetRun() |
|
269 | + { |
|
270 | + if (true === $this->isRunning) { |
|
271 | + $this->notify(PoolEvent::POOL_RUN_STOP); |
|
272 | + } |
|
273 | + $this->isRunning = false; |
|
274 | + $this->toRunThreads = $this->threads; |
|
275 | + $this->runningThreads = []; |
|
276 | + } |
|
277 | 277 | |
278 | 278 | } |