From a5b6b479ceee60ac7e55e3ccf3b085ceebb1264f Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Thu, 9 Jul 2020 10:04:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0USR1=E4=BF=A1=E5=8F=B7?= =?UTF-8?q?=EF=BC=8C=E5=AE=9E=E6=97=B6=E8=8E=B7=E5=8F=96=20worker=20?= =?UTF-8?q?=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../catcher/library/crontab/ManageProcess.php | 9 +-- extend/catcher/library/crontab/Process.php | 26 +++++---- .../library/crontab/RegisterSignal.php | 5 +- extend/catcher/library/crontab/Store.php | 56 ++++++++++++++++--- 4 files changed, 70 insertions(+), 26 deletions(-) diff --git a/extend/catcher/library/crontab/ManageProcess.php b/extend/catcher/library/crontab/ManageProcess.php index bc3bb50..2298c40 100644 --- a/extend/catcher/library/crontab/ManageProcess.php +++ b/extend/catcher/library/crontab/ManageProcess.php @@ -84,10 +84,10 @@ class ManageProcess $this->timeTick(1000, $this->schedule()); // 注册信号 $this->registerSignal(); - // 存储 pid - $this->storeMasterPid(getmypid()); // pid $this->master_pid = getmypid(); + // 存储 pid + $this->storeMasterPid($this->master_pid); // 初始化进程 $this->initProcesses(); } @@ -148,8 +148,7 @@ class ManageProcess protected function createProcess(Cron $cron) { $process = new Process(function (Process $process) use($cron) { - echo 'hello world'; - //$cron->run(); + $cron->run(); $process->exit(); }); @@ -195,6 +194,8 @@ class ManageProcess $this->storeStatus($this->processInfo($process)); } + + $this->saveProcessStatus(); } /** diff --git a/extend/catcher/library/crontab/Process.php b/extend/catcher/library/crontab/Process.php index c7bd751..8ab866f 100644 --- a/extend/catcher/library/crontab/Process.php +++ b/extend/catcher/library/crontab/Process.php @@ -28,8 +28,9 @@ trait Process $quit = true; }); - pcntl_signal(SIGUSR1, function (){ + pcntl_signal(SIGUSR1, function () use ($process){ // todo + $this->afterTask($process->pid); }); while (true) { @@ -94,6 +95,7 @@ trait Process // 获取等待状态的 worker $processes = $this->getProcessesStatus(); + // $processIds foreach ($processes as $process) { if ($process['status'] == self::WAITING) { @@ -195,7 +197,7 @@ trait Process * @time 2020年07月05日 * @return string */ - public function getWorkerStatus() + public function renderProcessesStatusToString() { $scheduleV = self::VERSION; $adminV = CatchAdmin::VERSION; @@ -215,22 +217,24 @@ EOT; $table = new Table(); $table->setHeader([ - 'Pid', 'StartAt', 'Status', 'DealTaskNumber', 'Errors' - ], 3); + 'pid', 'memory', 'start_at', 'running_time', 'status', 'deal_tasks','errors' + ], 2); $processes = []; - foreach ($this->process as $process) { + foreach ($this->getProcessesStatus() as $process) { $processes[] = [ - 'pid' => $process['pid'], - 'start_at' => date('Y-m-d H:i', $process['start_at']), - 'status' => $process['status'], - 'deal_num' => $process['deal_num'], - 'error' => $process['error'], + $process['pid'], + (int)($process['memory']/1024/1024) . 'M', + date('Y-m-d H:i', $process['start_at']), + gmstrftime('%H:%M:%S', $process['running_time']), + $process['status'], + $process['deal_tasks'], + $process['errors'], ]; } - $table->setRows($processes, 3); + $table->setRows($processes, 2); $table->render(); diff --git a/extend/catcher/library/crontab/RegisterSignal.php b/extend/catcher/library/crontab/RegisterSignal.php index 40c5650..d61ee16 100644 --- a/extend/catcher/library/crontab/RegisterSignal.php +++ b/extend/catcher/library/crontab/RegisterSignal.php @@ -97,10 +97,9 @@ trait RegisterSignal protected function workerStatus() { return function () { - // $this->storeStatus(); - foreach ($this->processes as $pid => $process) { + foreach ($this->processes as $pid => $process) { Process::kill($pid, SIGUSR1); - } + } }; } diff --git a/extend/catcher/library/crontab/Store.php b/extend/catcher/library/crontab/Store.php index 60a86d8..c0cad87 100644 --- a/extend/catcher/library/crontab/Store.php +++ b/extend/catcher/library/crontab/Store.php @@ -35,8 +35,8 @@ trait Store */ public function storeStatus(array $status) { - $workersStatus = $this->getProcessesStatus(); + if (empty($workersStatus)) { $this->writeStatusToFile([$status]); } else { @@ -115,7 +115,7 @@ trait Store // 等待信号输出 sleep(1); - return file_exists($this->getProcessStatusPath()) ? file_get_contents($this->getProcessStatusPath()) : ''; + return $this->getProcessStatusInfo(); } /** @@ -138,6 +138,17 @@ trait Store * @return string */ protected function getMasterPidPath() + { + return $this->schedulePath() . 'master.pid'; + } + + /** + * 创建任务调度文件夹 + * + * @time 2020年07月09日 + * @return string + */ + protected function schedulePath() { $path = runtime_path('schedule' . DIRECTORY_SEPARATOR); @@ -145,7 +156,7 @@ trait Store mkdir($path, 0777, true); } - return $path . 'master.pid'; + return $path; } /** @@ -156,12 +167,41 @@ trait Store */ protected function getProcessStatusPath() { - $path = runtime_path('schedule' . DIRECTORY_SEPARATOR); + return $this->schedulePath() . 'worker-status.json'; + } - if (!is_dir($path)) { - mkdir($path, 0777, true); - } + /** + * 进程状态文件 + * + * @time 2020年07月09日 + * @return string + */ + protected function getSaveProcessStatusFile() + { + return $this->schedulePath() . '.worker-status'; + } - return $path . 'worker-status.json'; + /** + * 保存进程状态 + * + * @time 2020年07月09日 + * @return void + */ + protected function saveProcessStatus() + { + file_put_contents($this->getSaveProcessStatusFile(), $this->renderProcessesStatusToString()); + } + + /** + * 获取进程状态 + * + * @time 2020年07月09日 + * @return false|string + */ + protected function getProcessStatusInfo() + { + $this->saveProcessStatus(); + + return file_get_contents($this->getSaveProcessStatusFile()); } } \ No newline at end of file