@@ -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 | } |
@@ -9,22 +9,22 @@ |
||
| 9 | 9 | |
| 10 | 10 | class EventPoolFactory |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * @param string $eventName |
|
| 14 | - * @param ThreadPool|AbstractThreadPoolMediator $pool |
|
| 15 | - * @param AbstractThread|null $thread |
|
| 16 | - * @return PoolEvent |
|
| 17 | - */ |
|
| 18 | - public static function create(string $eventName, AbstractThreadPoolMediator $pool, ?AbstractThread $thread = null): PoolEvent |
|
| 19 | - { |
|
| 20 | - $event = new PoolEvent($eventName); |
|
| 21 | - $event->setThreadNb(count($pool->getThreads())); |
|
| 22 | - $event->setThreadDoneNb(count($pool->getThreads()) - count($pool->getToRunThreads())); |
|
| 23 | - $event->setMaxRunningThreadNb($pool->getMaxRunningThreadNb()); |
|
| 24 | - $event->setThreadLeftNb(count($pool->getToRunThreads())); |
|
| 25 | - $event->setRunningThreadNb(count($pool->getRunningThreads())); |
|
| 26 | - $event->setThread($thread); |
|
| 12 | + /** |
|
| 13 | + * @param string $eventName |
|
| 14 | + * @param ThreadPool|AbstractThreadPoolMediator $pool |
|
| 15 | + * @param AbstractThread|null $thread |
|
| 16 | + * @return PoolEvent |
|
| 17 | + */ |
|
| 18 | + public static function create(string $eventName, AbstractThreadPoolMediator $pool, ?AbstractThread $thread = null): PoolEvent |
|
| 19 | + { |
|
| 20 | + $event = new PoolEvent($eventName); |
|
| 21 | + $event->setThreadNb(count($pool->getThreads())); |
|
| 22 | + $event->setThreadDoneNb(count($pool->getThreads()) - count($pool->getToRunThreads())); |
|
| 23 | + $event->setMaxRunningThreadNb($pool->getMaxRunningThreadNb()); |
|
| 24 | + $event->setThreadLeftNb(count($pool->getToRunThreads())); |
|
| 25 | + $event->setRunningThreadNb(count($pool->getRunningThreads())); |
|
| 26 | + $event->setThread($thread); |
|
| 27 | 27 | |
| 28 | - return $event; |
|
| 29 | - } |
|
| 28 | + return $event; |
|
| 29 | + } |
|
| 30 | 30 | } |
@@ -7,186 +7,186 @@ |
||
| 7 | 7 | |
| 8 | 8 | class PoolEvent implements EventInterface |
| 9 | 9 | { |
| 10 | - const POOL_RUN_START = 'run_start'; |
|
| 11 | - const POOL_RUN_STOP = 'run_stop'; |
|
| 12 | - const POOL_NEW_THREAD = 'new_thread'; |
|
| 13 | - const POOL_PRE_WAIT_TICK = 'pre_wait_tick'; |
|
| 14 | - const POOL_POST_WAIT_TICK = 'post_wait_tick'; |
|
| 15 | - const POOL_WAIT_TICK_PID = 'wait_tick_pid'; |
|
| 16 | - const POOL_WAIT_TICK_PID_REMOVED = 'wait_tick_pid_removed'; |
|
| 17 | - |
|
| 18 | - const THREAD_PRE_PROCESS = 'pre_process'; |
|
| 19 | - const THREAD_POST_PROCESS = 'post_process'; |
|
| 20 | - const THREAD_EXIT_SUCCESS = 'exist_success'; |
|
| 21 | - const THREAD_EXIT_ERROR = 'exit_error'; |
|
| 22 | - const THREAD_EXIT_UNKNOWN = 'exit_unknown'; |
|
| 23 | - |
|
| 24 | - /** @var string $eventName */ |
|
| 25 | - private $eventName; |
|
| 26 | - |
|
| 27 | - /** @var int $threadNb */ |
|
| 28 | - private $threadNb; |
|
| 29 | - |
|
| 30 | - /** @var int $runningThreadNb */ |
|
| 31 | - private $runningThreadNb; |
|
| 32 | - |
|
| 33 | - /** @var int $threadDoneNb */ |
|
| 34 | - private $threadDoneNb; |
|
| 35 | - |
|
| 36 | - /** @var int $threadLeftNb */ |
|
| 37 | - private $threadLeftNb; |
|
| 38 | - |
|
| 39 | - /** @var int $maxRunningThreadNb */ |
|
| 40 | - private $maxRunningThreadNb; |
|
| 41 | - |
|
| 42 | - /** @var AbstractThread $thread */ |
|
| 43 | - private $thread; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * ThreadPoolEvent constructor. |
|
| 47 | - * @param string $eventName |
|
| 48 | - */ |
|
| 49 | - public function __construct($eventName) |
|
| 50 | - { |
|
| 51 | - $this->eventName = $eventName; |
|
| 52 | - $this->threadNb = 0; |
|
| 53 | - $this->runningThreadNb = 0; |
|
| 54 | - $this->threadDoneNb = 0; |
|
| 55 | - $this->threadLeftNb = 0; |
|
| 56 | - $this->maxRunningThreadNb = 0; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * @return string |
|
| 61 | - */ |
|
| 62 | - public function getEventName(): string |
|
| 63 | - { |
|
| 64 | - return $this->eventName; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * @param string $eventName |
|
| 69 | - * @return PoolEvent |
|
| 70 | - */ |
|
| 71 | - public function setEventName(string $eventName): self |
|
| 72 | - { |
|
| 73 | - $this->eventName = $eventName; |
|
| 74 | - |
|
| 75 | - return $this; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * @return int |
|
| 80 | - */ |
|
| 81 | - public function getThreadNb(): int |
|
| 82 | - { |
|
| 83 | - return $this->threadNb; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * @param int $threadNb |
|
| 88 | - * @return PoolEvent |
|
| 89 | - */ |
|
| 90 | - public function setThreadNb(int $threadNb): self |
|
| 91 | - { |
|
| 92 | - $this->threadNb = $threadNb; |
|
| 93 | - |
|
| 94 | - return $this; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * @return int |
|
| 99 | - */ |
|
| 100 | - public function getThreadDoneNb(): int |
|
| 101 | - { |
|
| 102 | - return $this->threadDoneNb; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * @param int $threadDoneNb |
|
| 107 | - * @return PoolEvent |
|
| 108 | - */ |
|
| 109 | - public function setThreadDoneNb(int $threadDoneNb): self |
|
| 110 | - { |
|
| 111 | - $this->threadDoneNb = $threadDoneNb; |
|
| 112 | - |
|
| 113 | - return $this; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * @return int |
|
| 118 | - */ |
|
| 119 | - public function getThreadLeftNb(): int |
|
| 120 | - { |
|
| 121 | - return $this->threadLeftNb; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * @param int $threadLeftNb |
|
| 126 | - * @return PoolEvent |
|
| 127 | - */ |
|
| 128 | - public function setThreadLeftNb(int $threadLeftNb): self |
|
| 129 | - { |
|
| 130 | - $this->threadLeftNb = $threadLeftNb; |
|
| 131 | - |
|
| 132 | - return $this; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * @return int |
|
| 137 | - */ |
|
| 138 | - public function getMaxRunningThreadNb(): int |
|
| 139 | - { |
|
| 140 | - return $this->maxRunningThreadNb; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * @param int $maxRunningThreadNb |
|
| 145 | - * @return PoolEvent |
|
| 146 | - */ |
|
| 147 | - public function setMaxRunningThreadNb(int $maxRunningThreadNb): self |
|
| 148 | - { |
|
| 149 | - $this->maxRunningThreadNb = $maxRunningThreadNb; |
|
| 150 | - |
|
| 151 | - return $this; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * @return int |
|
| 156 | - */ |
|
| 157 | - public function getRunningThreadNb(): int |
|
| 158 | - { |
|
| 159 | - return $this->runningThreadNb; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * @param int $runningThreadNb |
|
| 164 | - * @return PoolEvent |
|
| 165 | - */ |
|
| 166 | - public function setRunningThreadNb(int $runningThreadNb): self |
|
| 167 | - { |
|
| 168 | - $this->runningThreadNb = $runningThreadNb; |
|
| 169 | - |
|
| 170 | - return $this; |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * @return AbstractThread |
|
| 175 | - */ |
|
| 176 | - public function getThread(): ?AbstractThread |
|
| 177 | - { |
|
| 178 | - return $this->thread; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * @param AbstractThread|null $thread |
|
| 183 | - * @return $this |
|
| 184 | - */ |
|
| 185 | - public function setThread(AbstractThread $thread = null): self |
|
| 186 | - { |
|
| 187 | - $this->thread = $thread; |
|
| 188 | - |
|
| 189 | - return $this; |
|
| 190 | - } |
|
| 10 | + const POOL_RUN_START = 'run_start'; |
|
| 11 | + const POOL_RUN_STOP = 'run_stop'; |
|
| 12 | + const POOL_NEW_THREAD = 'new_thread'; |
|
| 13 | + const POOL_PRE_WAIT_TICK = 'pre_wait_tick'; |
|
| 14 | + const POOL_POST_WAIT_TICK = 'post_wait_tick'; |
|
| 15 | + const POOL_WAIT_TICK_PID = 'wait_tick_pid'; |
|
| 16 | + const POOL_WAIT_TICK_PID_REMOVED = 'wait_tick_pid_removed'; |
|
| 17 | + |
|
| 18 | + const THREAD_PRE_PROCESS = 'pre_process'; |
|
| 19 | + const THREAD_POST_PROCESS = 'post_process'; |
|
| 20 | + const THREAD_EXIT_SUCCESS = 'exist_success'; |
|
| 21 | + const THREAD_EXIT_ERROR = 'exit_error'; |
|
| 22 | + const THREAD_EXIT_UNKNOWN = 'exit_unknown'; |
|
| 23 | + |
|
| 24 | + /** @var string $eventName */ |
|
| 25 | + private $eventName; |
|
| 26 | + |
|
| 27 | + /** @var int $threadNb */ |
|
| 28 | + private $threadNb; |
|
| 29 | + |
|
| 30 | + /** @var int $runningThreadNb */ |
|
| 31 | + private $runningThreadNb; |
|
| 32 | + |
|
| 33 | + /** @var int $threadDoneNb */ |
|
| 34 | + private $threadDoneNb; |
|
| 35 | + |
|
| 36 | + /** @var int $threadLeftNb */ |
|
| 37 | + private $threadLeftNb; |
|
| 38 | + |
|
| 39 | + /** @var int $maxRunningThreadNb */ |
|
| 40 | + private $maxRunningThreadNb; |
|
| 41 | + |
|
| 42 | + /** @var AbstractThread $thread */ |
|
| 43 | + private $thread; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * ThreadPoolEvent constructor. |
|
| 47 | + * @param string $eventName |
|
| 48 | + */ |
|
| 49 | + public function __construct($eventName) |
|
| 50 | + { |
|
| 51 | + $this->eventName = $eventName; |
|
| 52 | + $this->threadNb = 0; |
|
| 53 | + $this->runningThreadNb = 0; |
|
| 54 | + $this->threadDoneNb = 0; |
|
| 55 | + $this->threadLeftNb = 0; |
|
| 56 | + $this->maxRunningThreadNb = 0; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * @return string |
|
| 61 | + */ |
|
| 62 | + public function getEventName(): string |
|
| 63 | + { |
|
| 64 | + return $this->eventName; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * @param string $eventName |
|
| 69 | + * @return PoolEvent |
|
| 70 | + */ |
|
| 71 | + public function setEventName(string $eventName): self |
|
| 72 | + { |
|
| 73 | + $this->eventName = $eventName; |
|
| 74 | + |
|
| 75 | + return $this; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * @return int |
|
| 80 | + */ |
|
| 81 | + public function getThreadNb(): int |
|
| 82 | + { |
|
| 83 | + return $this->threadNb; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * @param int $threadNb |
|
| 88 | + * @return PoolEvent |
|
| 89 | + */ |
|
| 90 | + public function setThreadNb(int $threadNb): self |
|
| 91 | + { |
|
| 92 | + $this->threadNb = $threadNb; |
|
| 93 | + |
|
| 94 | + return $this; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * @return int |
|
| 99 | + */ |
|
| 100 | + public function getThreadDoneNb(): int |
|
| 101 | + { |
|
| 102 | + return $this->threadDoneNb; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * @param int $threadDoneNb |
|
| 107 | + * @return PoolEvent |
|
| 108 | + */ |
|
| 109 | + public function setThreadDoneNb(int $threadDoneNb): self |
|
| 110 | + { |
|
| 111 | + $this->threadDoneNb = $threadDoneNb; |
|
| 112 | + |
|
| 113 | + return $this; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * @return int |
|
| 118 | + */ |
|
| 119 | + public function getThreadLeftNb(): int |
|
| 120 | + { |
|
| 121 | + return $this->threadLeftNb; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * @param int $threadLeftNb |
|
| 126 | + * @return PoolEvent |
|
| 127 | + */ |
|
| 128 | + public function setThreadLeftNb(int $threadLeftNb): self |
|
| 129 | + { |
|
| 130 | + $this->threadLeftNb = $threadLeftNb; |
|
| 131 | + |
|
| 132 | + return $this; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * @return int |
|
| 137 | + */ |
|
| 138 | + public function getMaxRunningThreadNb(): int |
|
| 139 | + { |
|
| 140 | + return $this->maxRunningThreadNb; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * @param int $maxRunningThreadNb |
|
| 145 | + * @return PoolEvent |
|
| 146 | + */ |
|
| 147 | + public function setMaxRunningThreadNb(int $maxRunningThreadNb): self |
|
| 148 | + { |
|
| 149 | + $this->maxRunningThreadNb = $maxRunningThreadNb; |
|
| 150 | + |
|
| 151 | + return $this; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * @return int |
|
| 156 | + */ |
|
| 157 | + public function getRunningThreadNb(): int |
|
| 158 | + { |
|
| 159 | + return $this->runningThreadNb; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * @param int $runningThreadNb |
|
| 164 | + * @return PoolEvent |
|
| 165 | + */ |
|
| 166 | + public function setRunningThreadNb(int $runningThreadNb): self |
|
| 167 | + { |
|
| 168 | + $this->runningThreadNb = $runningThreadNb; |
|
| 169 | + |
|
| 170 | + return $this; |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * @return AbstractThread |
|
| 175 | + */ |
|
| 176 | + public function getThread(): ?AbstractThread |
|
| 177 | + { |
|
| 178 | + return $this->thread; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * @param AbstractThread|null $thread |
|
| 183 | + * @return $this |
|
| 184 | + */ |
|
| 185 | + public function setThread(AbstractThread $thread = null): self |
|
| 186 | + { |
|
| 187 | + $this->thread = $thread; |
|
| 188 | + |
|
| 189 | + return $this; |
|
| 190 | + } |
|
| 191 | 191 | |
| 192 | 192 | } |
@@ -6,21 +6,21 @@ discard block |
||
| 6 | 6 | |
| 7 | 7 | abstract class AbstractThread extends AbstractThreadMediator |
| 8 | 8 | { |
| 9 | - const EXIT_STATUS_SUCCESS = 0; |
|
| 10 | - const EXIT_STATUS_ERROR = 1; |
|
| 9 | + const EXIT_STATUS_SUCCESS = 0; |
|
| 10 | + const EXIT_STATUS_ERROR = 1; |
|
| 11 | 11 | |
| 12 | - /** @var int $pid */ |
|
| 13 | - private $pid; |
|
| 12 | + /** @var int $pid */ |
|
| 13 | + private $pid; |
|
| 14 | 14 | |
| 15 | - /** @var string $processName */ |
|
| 16 | - private $processName; |
|
| 15 | + /** @var string $processName */ |
|
| 16 | + private $processName; |
|
| 17 | 17 | |
| 18 | 18 | /** |
| 19 | 19 | * AbstractThread constructor. |
| 20 | 20 | * |
| 21 | 21 | * @param string $processName |
| 22 | 22 | */ |
| 23 | - public function __construct(string $processName) |
|
| 23 | + public function __construct(string $processName) |
|
| 24 | 24 | { |
| 25 | 25 | $this->processName = $processName; |
| 26 | 26 | } |
@@ -29,61 +29,61 @@ discard block |
||
| 29 | 29 | * Return the name of the method to process during the thread |
| 30 | 30 | * @return string |
| 31 | 31 | */ |
| 32 | - abstract protected function getMethodName(): string; |
|
| 32 | + abstract protected function getMethodName(): string; |
|
| 33 | 33 | |
| 34 | 34 | /** |
| 35 | 35 | * Return the list of dependencies that will be passed as parameters of the method referenced by getMethodName |
| 36 | 36 | * @return array |
| 37 | 37 | */ |
| 38 | - abstract protected function getDependencies(): array; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @return int|null |
|
| 42 | - */ |
|
| 43 | - public function getPid(): ?int |
|
| 44 | - { |
|
| 45 | - return $this->pid; |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * @param int $pid |
|
| 50 | - * @return AbstractThread |
|
| 51 | - */ |
|
| 52 | - public function setPid(int $pid): self |
|
| 53 | - { |
|
| 54 | - $this->pid = $pid; |
|
| 55 | - |
|
| 56 | - return $this; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * @return string |
|
| 61 | - */ |
|
| 62 | - public function getProcessName(): ?string |
|
| 63 | - { |
|
| 64 | - return $this->processName; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * @return int |
|
| 69 | - * @throws ThreadException |
|
| 70 | - */ |
|
| 71 | - public function run(): int |
|
| 72 | - { |
|
| 73 | - if (false === method_exists($this, $this->getMethodName())) { |
|
| 74 | - throw new ThreadException('No proper method defined for the thread'); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - $status = call_user_func_array( |
|
| 38 | + abstract protected function getDependencies(): array; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @return int|null |
|
| 42 | + */ |
|
| 43 | + public function getPid(): ?int |
|
| 44 | + { |
|
| 45 | + return $this->pid; |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * @param int $pid |
|
| 50 | + * @return AbstractThread |
|
| 51 | + */ |
|
| 52 | + public function setPid(int $pid): self |
|
| 53 | + { |
|
| 54 | + $this->pid = $pid; |
|
| 55 | + |
|
| 56 | + return $this; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * @return string |
|
| 61 | + */ |
|
| 62 | + public function getProcessName(): ?string |
|
| 63 | + { |
|
| 64 | + return $this->processName; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * @return int |
|
| 69 | + * @throws ThreadException |
|
| 70 | + */ |
|
| 71 | + public function run(): int |
|
| 72 | + { |
|
| 73 | + if (false === method_exists($this, $this->getMethodName())) { |
|
| 74 | + throw new ThreadException('No proper method defined for the thread'); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + $status = call_user_func_array( |
|
| 78 | 78 | [$this, $this->getMethodName()], |
| 79 | 79 | array_merge([$this->getProcessName()], $this->getDependencies()) |
| 80 | 80 | ); |
| 81 | 81 | |
| 82 | - if (null === $status) { |
|
| 83 | - throw new ThreadException('Error. You must return a process status in your callback function'); |
|
| 84 | - } |
|
| 82 | + if (null === $status) { |
|
| 83 | + throw new ThreadException('Error. You must return a process status in your callback function'); |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - return $status; |
|
| 87 | - } |
|
| 86 | + return $status; |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | 89 | } |
@@ -4,7 +4,7 @@ |
||
| 4 | 4 | use Wonderland\Thread\Example\Simple\TestThread; |
| 5 | 5 | use Wonderland\Thread\ThreadPool; |
| 6 | 6 | |
| 7 | -require __DIR__.'/../../vendor/autoload.php'; |
|
| 7 | +require __DIR__ . '/../../vendor/autoload.php'; |
|
| 8 | 8 | |
| 9 | 9 | /** |
| 10 | 10 | * @param $increment |