异常日志记录

This commit is contained in:
JaguarJack 2020-07-09 15:41:53 +08:00
parent a5b6b479ce
commit 2a593b179a
3 changed files with 49 additions and 23 deletions

View File

@ -53,6 +53,11 @@ class ManageProcess
*/ */
protected $mater = 'catch-master'; protected $mater = 'catch-master';
/**
* @var int
*/
protected $master_start_at;
/** /**
* process status 存储文件 * process status 存储文件
* *
@ -86,8 +91,11 @@ class ManageProcess
$this->registerSignal(); $this->registerSignal();
// pid // pid
$this->master_pid = getmypid(); $this->master_pid = getmypid();
$this->master_start_at = time();
// 存储 pid // 存储 pid
$this->storeMasterPid($this->master_pid); $this->storeMasterPid($this->master_pid);
// 初始化文件
$this->initFiles();
// 初始化进程 // 初始化进程
$this->initProcesses(); $this->initProcesses();
} }
@ -120,15 +128,15 @@ class ManageProcess
{ {
return function () { return function () {
$schedule = new Schedule(); $schedule = new Schedule();
$schedule->command('catch:cache')->everyThirtySeconds(); $schedule->command('catch:cache')->everyTenSeconds();
foreach ($schedule->getCronTask() as $cron) { foreach ($schedule->getCronTask() as $cron) {
if ($cron->can()) { if ($cron->can()) {
list($waiting, $process) = $this->hasWaitingProcess(); list($waiting, $process) = $this->hasWaitingProcess();
if ($waiting) { if ($waiting) {
// 向 process 投递 cron // 向 process 投递 cron
// var_dump(serialize($cron)); var_dump(serialize($cron));
//$process->push(serialize($cron)); $process->push(serialize($cron));
} else { } else {
// 创建临时 process 处理,处理完自动销毁 // 创建临时 process 处理,处理完自动销毁
$this->createProcess($cron); $this->createProcess($cron);
@ -181,7 +189,7 @@ class ManageProcess
*/ */
protected function initProcesses() protected function initProcesses()
{ {
file_put_contents($this->getProcessStatusPath(), '');
for ($i = 0; $i < $this->staticNum; $i++) { for ($i = 0; $i < $this->staticNum; $i++) {
@ -199,13 +207,14 @@ class ManageProcess
} }
/** /**
* 记录日志 * 初始化文件
* *
* @time 2020年07月07 * @time 2020年07月09
* @return void * @return void
*/ */
protected function log() protected function initFiles()
{ {
fwrite(STDOUT, runtime_path('schedule') . 'error.log'); file_put_contents($this->getProcessStatusPath(), '');
file_put_contents($this->schedulePath() . 'error.log', '');
} }
} }

View File

@ -12,7 +12,6 @@ namespace catcher\library\crontab;
use catcher\CatchAdmin; use catcher\CatchAdmin;
use think\console\Table; use think\console\Table;
use think\facade\Log;
trait Process trait Process
{ {
@ -34,22 +33,22 @@ trait Process
}); });
while (true) { while (true) {
//$data = $worker->pop(); if ($cron = $process->pop()) {
/**if ($cron = $process->pop()) {
if (is_string($cron) && $cron) { if (is_string($cron) && $cron) {
var_dump($cron);
//$cron = unserialize($cron); $cron = unserialize($cron);
$this->beforeTask($process->pid); $this->beforeTask($process->pid);
//$cron->run(); try {
$cron->run();
} catch (\Throwable $e) {
file_put_contents($this->schedulePath() . 'schedule.log', $e . PHP_EOL, FILE_APPEND);
; }
$this->afterTask($process->pid); $this->afterTask($process->pid);
//$process->push('from process' . $process->pid);
} }
}*/ }
pcntl_signal_dispatch(); pcntl_signal_dispatch();
sleep(1); sleep(1);
@ -203,6 +202,10 @@ trait Process
$adminV = CatchAdmin::VERSION; $adminV = CatchAdmin::VERSION;
$phpV = PHP_VERSION; $phpV = PHP_VERSION;
$processNumber = count($this->processes);
$memory = (int)(memory_get_usage()/1024/1024). 'M';
$startAt = date('Y-m-d H:i:s', $this->master_start_at);
$runtime = gmstrftime('%H:%M:%S', time() - $this->master_start_at);
$info = <<<EOT $info = <<<EOT
------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------
| ____ _ _ _ _ _ ____ _ _ _ | | ____ _ _ _ _ _ ____ _ _ _ |
@ -212,6 +215,8 @@ trait Process
| \____\__,_|\__\___|_| |_/_/ \_\__,_|_| |_| |_|_|_| |_| |____/ \___|_| |_|\___|\__,_|\__,_|_|\___| | | \____\__,_|\__\___|_| |_/_/ \_\__,_|_| |_| |_|_|_| |_| |____/ \___|_| |_|\___|\__,_|\__,_|_|\___| |
| ----------------------------------------- CatchAdmin Schedule ---------------------------------------| | ----------------------------------------- CatchAdmin Schedule ---------------------------------------|
| Schedule Version: $scheduleV CatchAdmin Version: $adminV PHP Version: $phpV | | Schedule Version: $scheduleV CatchAdmin Version: $adminV PHP Version: $phpV |
| Process Number: $processNumber Memory: $memory Start at: $startAt |
| Running Time: $runtime |
|------------------------------------------------------------------------------------------------------| |------------------------------------------------------------------------------------------------------|
EOT; EOT;

View File

@ -97,10 +97,22 @@ trait Store
*/ */
protected function writeStatusToFile($status) protected function writeStatusToFile($status)
{ {
$file = new \SplFileObject($this->getProcessStatusPath(), 'rw+'); $this->writeContentToFile($this->getProcessStatusPath(), \json_encode($status));
// 加锁 防止多进程写入混乱 }
/**
* 写入内容
*
* @time 2020年07月09日
* @param $path
* @param $content
* @return void
*/
protected function writeContentToFile($path, $content)
{
$file = new \SplFileObject($path, 'rw+');
$file->flock(LOCK_EX); $file->flock(LOCK_EX);
$file->fwrite(\json_encode($status)); $file->fwrite($content);
$file->flock(LOCK_UN); $file->flock(LOCK_UN);
} }