增加USR1信号,实时获取 worker 信息

This commit is contained in:
JaguarJack 2020-07-09 10:04:31 +08:00
parent 104ad3f744
commit a5b6b479ce
4 changed files with 70 additions and 26 deletions

View File

@ -84,10 +84,10 @@ class ManageProcess
$this->timeTick(1000, $this->schedule()); $this->timeTick(1000, $this->schedule());
// 注册信号 // 注册信号
$this->registerSignal(); $this->registerSignal();
// 存储 pid
$this->storeMasterPid(getmypid());
// pid // pid
$this->master_pid = getmypid(); $this->master_pid = getmypid();
// 存储 pid
$this->storeMasterPid($this->master_pid);
// 初始化进程 // 初始化进程
$this->initProcesses(); $this->initProcesses();
} }
@ -148,8 +148,7 @@ class ManageProcess
protected function createProcess(Cron $cron) protected function createProcess(Cron $cron)
{ {
$process = new Process(function (Process $process) use($cron) { $process = new Process(function (Process $process) use($cron) {
echo 'hello world'; $cron->run();
//$cron->run();
$process->exit(); $process->exit();
}); });
@ -195,6 +194,8 @@ class ManageProcess
$this->storeStatus($this->processInfo($process)); $this->storeStatus($this->processInfo($process));
} }
$this->saveProcessStatus();
} }
/** /**

View File

@ -28,8 +28,9 @@ trait Process
$quit = true; $quit = true;
}); });
pcntl_signal(SIGUSR1, function (){ pcntl_signal(SIGUSR1, function () use ($process){
// todo // todo
$this->afterTask($process->pid);
}); });
while (true) { while (true) {
@ -94,6 +95,7 @@ trait Process
// 获取等待状态的 worker // 获取等待状态的 worker
$processes = $this->getProcessesStatus(); $processes = $this->getProcessesStatus();
// $processIds // $processIds
foreach ($processes as $process) { foreach ($processes as $process) {
if ($process['status'] == self::WAITING) { if ($process['status'] == self::WAITING) {
@ -195,7 +197,7 @@ trait Process
* @time 2020年07月05日 * @time 2020年07月05日
* @return string * @return string
*/ */
public function getWorkerStatus() public function renderProcessesStatusToString()
{ {
$scheduleV = self::VERSION; $scheduleV = self::VERSION;
$adminV = CatchAdmin::VERSION; $adminV = CatchAdmin::VERSION;
@ -215,22 +217,24 @@ EOT;
$table = new Table(); $table = new Table();
$table->setHeader([ $table->setHeader([
'Pid', 'StartAt', 'Status', 'DealTaskNumber', 'Errors' 'pid', 'memory', 'start_at', 'running_time', 'status', 'deal_tasks','errors'
], 3); ], 2);
$processes = []; $processes = [];
foreach ($this->process as $process) { foreach ($this->getProcessesStatus() as $process) {
$processes[] = [ $processes[] = [
'pid' => $process['pid'], $process['pid'],
'start_at' => date('Y-m-d H:i', $process['start_at']), (int)($process['memory']/1024/1024) . 'M',
'status' => $process['status'], date('Y-m-d H:i', $process['start_at']),
'deal_num' => $process['deal_num'], gmstrftime('%H:%M:%S', $process['running_time']),
'error' => $process['error'], $process['status'],
$process['deal_tasks'],
$process['errors'],
]; ];
} }
$table->setRows($processes, 3); $table->setRows($processes, 2);
$table->render(); $table->render();

View File

@ -97,10 +97,9 @@ trait RegisterSignal
protected function workerStatus() protected function workerStatus()
{ {
return function () { return function () {
// $this->storeStatus(); foreach ($this->processes as $pid => $process) {
foreach ($this->processes as $pid => $process) {
Process::kill($pid, SIGUSR1); Process::kill($pid, SIGUSR1);
} }
}; };
} }

View File

@ -35,8 +35,8 @@ trait Store
*/ */
public function storeStatus(array $status) public function storeStatus(array $status)
{ {
$workersStatus = $this->getProcessesStatus(); $workersStatus = $this->getProcessesStatus();
if (empty($workersStatus)) { if (empty($workersStatus)) {
$this->writeStatusToFile([$status]); $this->writeStatusToFile([$status]);
} else { } else {
@ -115,7 +115,7 @@ trait Store
// 等待信号输出 // 等待信号输出
sleep(1); sleep(1);
return file_exists($this->getProcessStatusPath()) ? file_get_contents($this->getProcessStatusPath()) : ''; return $this->getProcessStatusInfo();
} }
/** /**
@ -138,6 +138,17 @@ trait Store
* @return string * @return string
*/ */
protected function getMasterPidPath() protected function getMasterPidPath()
{
return $this->schedulePath() . 'master.pid';
}
/**
* 创建任务调度文件夹
*
* @time 2020年07月09日
* @return string
*/
protected function schedulePath()
{ {
$path = runtime_path('schedule' . DIRECTORY_SEPARATOR); $path = runtime_path('schedule' . DIRECTORY_SEPARATOR);
@ -145,7 +156,7 @@ trait Store
mkdir($path, 0777, true); mkdir($path, 0777, true);
} }
return $path . 'master.pid'; return $path;
} }
/** /**
@ -156,12 +167,41 @@ trait Store
*/ */
protected function getProcessStatusPath() 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());
} }
} }