增加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->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();
}
/**

View File

@ -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();

View File

@ -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);
}
}
};
}

View File

@ -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());
}
}