diff --git a/extend/catcher/library/crontab/ManageProcess.php b/extend/catcher/library/crontab/ManageProcess.php index 2298c40..6374784 100644 --- a/extend/catcher/library/crontab/ManageProcess.php +++ b/extend/catcher/library/crontab/ManageProcess.php @@ -53,6 +53,11 @@ class ManageProcess */ protected $mater = 'catch-master'; + /** + * @var int + */ + protected $master_start_at; + /** * process status 存储文件 * @@ -77,7 +82,7 @@ class ManageProcess public function start() { // 守护进程 - // Process::daemon(true, false); + // Process::daemon(true, false); // alarm 信号 // Process::alarm(1000 * 1000); // 1s 调度一次 @@ -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', ''); } } \ No newline at end of file diff --git a/extend/catcher/library/crontab/Process.php b/extend/catcher/library/crontab/Process.php index 8ab866f..0ea652f 100644 --- a/extend/catcher/library/crontab/Process.php +++ b/extend/catcher/library/crontab/Process.php @@ -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 = <<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); }