From b12edc94394aa0cf02ad5774aa70aab544b3f5b5 Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Mon, 18 Jan 2021 09:29:02 +0800 Subject: [PATCH] =?UTF-8?q?update:=E6=9B=B4=E6=96=B0=E8=B0=83=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- catch/monitor/command/ScheduleCommand.php | 38 +++++++++++++++++++++-- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/catch/monitor/command/ScheduleCommand.php b/catch/monitor/command/ScheduleCommand.php index 6ad88c3..602bbad 100644 --- a/catch/monitor/command/ScheduleCommand.php +++ b/catch/monitor/command/ScheduleCommand.php @@ -16,6 +16,7 @@ namespace catchAdmin\monitor\command; use catchAdmin\monitor\command\process\Process; use catchAdmin\monitor\model\Crontab; +use catchAdmin\monitor\model\CrontabLog; use Cron\CronExpression; use think\console\Command; use think\console\Input; @@ -39,7 +40,7 @@ class ScheduleCommand extends Command foreach ($this->getExecuteAbleCommands() as $command) { $process = new Process(function (Process $process) use ($command) { - $this->getConsole()->call($command); + $this->executeCommand($command); $process->exit(); }); @@ -48,7 +49,38 @@ class ScheduleCommand extends Command } catch (\Exception $e) { Log::error('CatchSchedule Error:' . $e->getMessage()); } + } + /** + * 执行 command + * + * @time 2021年01月18日 + * @param $command + * @return void + */ + protected function executeCommand($command) + { + $start = time(); + + $errorMessage = ''; + + try { + $this->getConsole()->call($command->task); + } catch (\Exception $e) { + $errorMessage = $e->getMessage(); + } + + $end = time(); + + // 插入 crontab 执行日志 + CrontabLog::insert([ + 'crontab_id' => $command->id, + 'used_time' => $end - $start, + 'status' => $errorMessage ? CrontabLog::FAILED : CrontabLog::SUCCESS, + 'error_message' => $errorMessage, + 'created_at' => time(), + 'updated_at' => time(), + ]); } /** @@ -69,7 +101,7 @@ class ScheduleCommand extends Command ->select() ->each(function ($command) use (&$executeAbleCommands){ if ($command->tactics == Crontab::EXECUTE_IMMEDIATELY) { - $executeAbleCommands[] = $command->task; + $executeAbleCommands[] = $command; return true; } @@ -85,7 +117,7 @@ class ScheduleCommand extends Command ]); } - $executeAbleCommands[] = $command->task; + $executeAbleCommands[] = $command; } return true; });