From c84d5a2a1703e1eaf77a04d10953b251928a5daa Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Thu, 9 Jul 2020 21:17:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=88=9B=E5=BB=BA=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E8=BF=9B=E7=A8=8B=E6=95=B0=E7=9B=AE=E4=B9=8B=E5=90=8E?= =?UTF-8?q?=E5=8F=AF=E7=BB=A7=E7=BB=AD=E5=88=9B=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extend/catcher/library/crontab/Master.php | 47 ++++++++++++------- extend/catcher/library/crontab/Process.php | 2 +- .../library/crontab/RegisterSignal.php | 2 + 3 files changed, 33 insertions(+), 18 deletions(-) diff --git a/extend/catcher/library/crontab/Master.php b/extend/catcher/library/crontab/Master.php index f0e5ba9..0fa4b9d 100644 --- a/extend/catcher/library/crontab/Master.php +++ b/extend/catcher/library/crontab/Master.php @@ -23,14 +23,21 @@ class Master * * @var int */ - protected $maxNum = 10; + protected $maxNum; /** * 常驻 process * * @var int */ - protected $staticNum = 1; + protected $staticNum; + + /** + * 临时进程数量 + * + * @var int + */ + protected $temporaryNum = 0; /** * 存储 process 信息 @@ -58,15 +65,6 @@ class Master */ protected $master_start_at; - - protected $table; - /** - * 日志 - * - * @var - */ - protected $logHandle; - // 版本 const VERSION = '1.0.0'; @@ -157,14 +155,29 @@ class Master */ protected function createProcess(Cron $cron) { - $process = new Process(function (Process $process) use($cron) { - $cron->run(); - $process->exit(); - }); + if ($this->isCanCreateTemporaryProcess()) { + $process = new Process(function (Process $process) use ($cron) { + $cron->run(); + $process->exit(); + }); - // $process->name(sprintf('worker: ')); + // $process->name(sprintf('worker: ')); - $process->start(); + $process->start(); + + $this->temporaryNum += 1; + } + } + + /** + * 是否可以创建临时进程 + * + * @time 2020年07月09日 + * @return bool + */ + protected function isCanCreateTemporaryProcess() + { + return ($this->table->count() + $this->temporaryNum) < $this->maxNum; } /** diff --git a/extend/catcher/library/crontab/Process.php b/extend/catcher/library/crontab/Process.php index 3fd9e0c..3cd18d7 100644 --- a/extend/catcher/library/crontab/Process.php +++ b/extend/catcher/library/crontab/Process.php @@ -98,7 +98,7 @@ trait Process } } - // 获取相应的状态 + // 获取相应的进程投递任务 if (isset($this->processes[$pid])) { return [true, $this->processes[$pid]]; } diff --git a/extend/catcher/library/crontab/RegisterSignal.php b/extend/catcher/library/crontab/RegisterSignal.php index 28ccd27..19cb105 100644 --- a/extend/catcher/library/crontab/RegisterSignal.php +++ b/extend/catcher/library/crontab/RegisterSignal.php @@ -66,6 +66,8 @@ trait RegisterSignal $this->unsetWorkerStatus($res['pid']); unset($this->processes[$res['pid']]); } + // 临时进程数目减少一次 + $this->temporaryNum -= 1; } }; }