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