修复创建最大进程数目之后可继续创建

This commit is contained in:
JaguarJack 2020-07-09 21:17:34 +08:00
parent 9335556197
commit c84d5a2a17
3 changed files with 33 additions and 18 deletions

View File

@ -23,14 +23,21 @@ class Master
* *
* @var int * @var int
*/ */
protected $maxNum = 10; protected $maxNum;
/** /**
* 常驻 process * 常驻 process
* *
* @var int * @var int
*/ */
protected $staticNum = 1; protected $staticNum;
/**
* 临时进程数量
*
* @var int
*/
protected $temporaryNum = 0;
/** /**
* 存储 process 信息 * 存储 process 信息
@ -58,15 +65,6 @@ class Master
*/ */
protected $master_start_at; protected $master_start_at;
protected $table;
/**
* 日志
*
* @var
*/
protected $logHandle;
// 版本 // 版本
const VERSION = '1.0.0'; const VERSION = '1.0.0';
@ -157,6 +155,7 @@ class Master
*/ */
protected function createProcess(Cron $cron) protected function createProcess(Cron $cron)
{ {
if ($this->isCanCreateTemporaryProcess()) {
$process = new Process(function (Process $process) use ($cron) { $process = new Process(function (Process $process) use ($cron) {
$cron->run(); $cron->run();
$process->exit(); $process->exit();
@ -165,6 +164,20 @@ class Master
// $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;
} }
/** /**

View File

@ -98,7 +98,7 @@ trait Process
} }
} }
// 获取相应的状态 // 获取相应的进程投递任务
if (isset($this->processes[$pid])) { if (isset($this->processes[$pid])) {
return [true, $this->processes[$pid]]; return [true, $this->processes[$pid]];
} }

View File

@ -66,6 +66,8 @@ trait RegisterSignal
$this->unsetWorkerStatus($res['pid']); $this->unsetWorkerStatus($res['pid']);
unset($this->processes[$res['pid']]); unset($this->processes[$res['pid']]);
} }
// 临时进程数目减少一次
$this->temporaryNum -= 1;
} }
}; };
} }