From 6397ddca97048afed7e50078d98d537ad599ed9a Mon Sep 17 00:00:00 2001 From: huyanping Date: Fri, 10 Feb 2017 16:48:49 +0800 Subject: [PATCH] add several pool methods --- src/AbstractPool.php | 67 ++++++++++++++++++++++++++++++++++++++++++++ src/ParallelPool.php | 20 ------------- src/Pool.php | 17 ----------- 3 files changed, 67 insertions(+), 37 deletions(-) diff --git a/src/AbstractPool.php b/src/AbstractPool.php index a52ee52..a37a68d 100644 --- a/src/AbstractPool.php +++ b/src/AbstractPool.php @@ -113,4 +113,71 @@ public function aliveCount() return $count; } + + /** + * get process by name + * + * @param string $name process name + * @return Process|null + */ + public function getProcessByName($name) + { + foreach ($this->processes as $process) { + if ($process->name() == $name) { + return $process; + } + } + + return null; + } + + /** + * remove process by name + * + * @param string $name process name + * @throws \RuntimeException + */ + public function removeProcessByName($name) + { + foreach ($this->processes as $key => $process) { + if ($process->name() == $name) { + if ($process->isRunning()) { + throw new \RuntimeException("can not remove a running process"); + } + unset($this->processes[$key]); + } + } + } + + /** + * remove exited process + */ + public function removeExitedProcess() + { + foreach ($this->processes as $key => $process) { + if ($process->isStopped()) { + unset($this->processes[$key]); + } + } + } + + /** + * return process count + * + * @return int + */ + public function count() + { + return count($this->processes); + } + + /** + * get all processes + * + * @return Process[] + */ + public function getProcesses() + { + return $this->processes; + } } \ No newline at end of file diff --git a/src/ParallelPool.php b/src/ParallelPool.php index 0cf8103..85cd142 100644 --- a/src/ParallelPool.php +++ b/src/ParallelPool.php @@ -103,24 +103,4 @@ public function start() } } } - - /** - * return process count - * - * @return int - */ - public function count() - { - return count($this->processes); - } - - /** - * get all processes - * - * @return Process[] - */ - public function getProcesses() - { - return $this->processes; - } } \ No newline at end of file diff --git a/src/Pool.php b/src/Pool.php index 1f841c8..f3bfdb6 100644 --- a/src/Pool.php +++ b/src/Pool.php @@ -35,21 +35,4 @@ public function execute(Process $process, $name = null) return array_push($this->processes, $process); } - - /** - * get process by name - * - * @param string $name process name - * @return Process|null - */ - public function getProcessByName($name) - { - foreach ($this->processes as $process) { - if ($process->name() == $name) { - return $process; - } - } - - return null; - } } \ No newline at end of file