异常日志记录

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';
/**
* @var int
*/
protected $master_start_at;
/**
* process status 存储文件
*
@ -86,8 +91,11 @@ class ManageProcess
$this->registerSignal();
// pid
$this->master_pid = getmypid();
$this->master_start_at = time();
// 存储 pid
$this->storeMasterPid($this->master_pid);
// 初始化文件
$this->initFiles();
// 初始化进程
$this->initProcesses();
}
@ -120,15 +128,15 @@ class ManageProcess
{
return function () {
$schedule = new Schedule();
$schedule->command('catch:cache')->everyThirtySeconds();
$schedule->command('catch:cache')->everyTenSeconds();
foreach ($schedule->getCronTask() as $cron) {
if ($cron->can()) {
list($waiting, $process) = $this->hasWaitingProcess();
if ($waiting) {
// 向 process 投递 cron
// var_dump(serialize($cron));
//$process->push(serialize($cron));
var_dump(serialize($cron));
$process->push(serialize($cron));
} else {
// 创建临时 process 处理,处理完自动销毁
$this->createProcess($cron);
@ -181,7 +189,7 @@ class ManageProcess
*/
protected function initProcesses()
{
file_put_contents($this->getProcessStatusPath(), '');
for ($i = 0; $i < $this->staticNum; $i++) {
@ -199,13 +207,14 @@ class ManageProcess
}
/**
* 记录日志
* 初始化文件
*
* @time 2020年07月07
* @time 2020年07月09
* @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 think\console\Table;
use think\facade\Log;
trait Process
{
@ -34,22 +33,22 @@ trait Process
});
while (true) {
//$data = $worker->pop();
/**if ($cron = $process->pop()) {
if ($cron = $process->pop()) {
if (is_string($cron) && $cron) {
var_dump($cron);
//$cron = unserialize($cron);
$cron = unserialize($cron);
$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);
//$process->push('from process' . $process->pid);
}
}*/
}
pcntl_signal_dispatch();
sleep(1);
@ -203,6 +202,10 @@ trait Process
$adminV = CatchAdmin::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
-------------------------------------------------------------------------------------------------------
| ____ _ _ _ _ _ ____ _ _ _ |
@ -212,6 +215,8 @@ trait Process
| \____\__,_|\__\___|_| |_/_/ \_\__,_|_| |_| |_|_|_| |_| |____/ \___|_| |_|\___|\__,_|\__,_|_|\___| |
| ----------------------------------------- CatchAdmin Schedule ---------------------------------------|
| Schedule Version: $scheduleV CatchAdmin Version: $adminV PHP Version: $phpV |
| Process Number: $processNumber Memory: $memory Start at: $startAt |
| Running Time: $runtime |
|------------------------------------------------------------------------------------------------------|
EOT;

View File

@ -97,10 +97,22 @@ trait Store
*/
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->fwrite(\json_encode($status));
$file->fwrite($content);
$file->flock(LOCK_UN);
}