From 3f154e5fb5e8ebc8c5fe097b5a7f81be6168eb25 Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Sun, 17 Jan 2021 11:21:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0:=E5=AE=9A=E6=97=B6=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E9=87=8D=E6=96=B0=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- catch/monitor/command/CatchCrontabCommand.php | 2 +- catch/monitor/command/ScheduleCommand.php | 94 +++++++++++++++++++ catch/monitor/command/process/Parser.php | 1 - .../command/process/RegisterSignal.php | 2 +- catch/monitor/command/process/Store.php | 4 +- 5 files changed, 98 insertions(+), 5 deletions(-) delete mode 100644 catch/monitor/command/process/Parser.php diff --git a/catch/monitor/command/CatchCrontabCommand.php b/catch/monitor/command/CatchCrontabCommand.php index 4a6d862..7e330c3 100644 --- a/catch/monitor/command/CatchCrontabCommand.php +++ b/catch/monitor/command/CatchCrontabCommand.php @@ -36,7 +36,7 @@ class CatchCrontabCommand extends Command ->addOption('pid', '-p', Option::VALUE_REQUIRED, 'you can send signal to the process of pid') ->addOption('static', '-s', Option::VALUE_REQUIRED, 'default static process number', 1) ->addOption('dynamic', '-dy', Option::VALUE_REQUIRED, 'default dynamic process number', 10) - ->addOption('interval', '-i', Option::VALUE_REQUIRED, 'interval/seconds', 60) + ->addOption('interval', '-i', Option::VALUE_REQUIRED, 'interval/seconds', 5) ->setDescription('start catch crontab schedule'); } diff --git a/catch/monitor/command/ScheduleCommand.php b/catch/monitor/command/ScheduleCommand.php index b3d9bbc..6ad88c3 100644 --- a/catch/monitor/command/ScheduleCommand.php +++ b/catch/monitor/command/ScheduleCommand.php @@ -1 +1,95 @@ setName('catch:schedule') + ->setDescription('catch schedule'); + } + + protected function execute(Input $input, Output $output) + { + try { + foreach ($this->getExecuteAbleCommands() as $command) { + + $process = new Process(function (Process $process) use ($command) { + $this->getConsole()->call($command); + $process->exit(); + }); + + $process->start(); + } + } catch (\Exception $e) { + Log::error('CatchSchedule Error:' . $e->getMessage()); + } + + } + + /** + * 获取可执行任务 + * + * @time 2021年01月17日 + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + * @return array + */ + protected function getExecuteAbleCommands() + { + $executeAbleCommands = []; + + Crontab::where('status', Crontab::ENABLE) + ->where('tactics', '<>', Crontab::EXECUTE_FORBIDDEN) + ->select() + ->each(function ($command) use (&$executeAbleCommands){ + if ($command->tactics == Crontab::EXECUTE_IMMEDIATELY) { + $executeAbleCommands[] = $command->task; + return true; + } + + $can = date('Y-m-d H:i', CronExpression::factory(trim($command->cron)) + ->getNextRunDate(date('Y-m-d H:i:s'), 0, true) + ->getTimestamp()) == date('Y-m-d H:i', time()); + + if ($can) { + // 如果任务只执行一次 之后禁用该任务 + if ($command->tactics === Crontab::EXECUTE_ONCE) { + Crontab::where('id', $command->id)->update([ + 'status' => Crontab::DISABLE, + ]); + } + + $executeAbleCommands[] = $command->task; + } + return true; + }); + + return $executeAbleCommands; + } +} diff --git a/catch/monitor/command/process/Parser.php b/catch/monitor/command/process/Parser.php deleted file mode 100644 index b3d9bbc..0000000 --- a/catch/monitor/command/process/Parser.php +++ /dev/null @@ -1 +0,0 @@ -exit(); }); - $process->start(); + $process->start(); Process::alarm($this->interval); }); diff --git a/catch/monitor/command/process/Store.php b/catch/monitor/command/process/Store.php index 006262a..8156a61 100644 --- a/catch/monitor/command/process/Store.php +++ b/catch/monitor/command/process/Store.php @@ -121,7 +121,7 @@ trait Store { $pidFile = config('catch.crontab.master_pid_file'); - if (!file_exists($pidFile)) { + if (!FileSystem::exists($pidFile)) { return 0; } @@ -136,7 +136,7 @@ trait Store */ public function renderStatus() { - return file_get_contents(self::statusPath()); + return FileSystem::sharedGet(self::statusPath()); } /**