From b8de40a60f41bdcbade003b985c16994bd1e7bb6 Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Thu, 9 Jul 2020 21:56:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=AE=88=E6=8A=A4=E6=8E=A7?= =?UTF-8?q?=E5=88=B6=E9=80=89=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../catcher/command/CatchScheduleCommand.php | 37 ++++++++++++------- extend/catcher/library/crontab/Master.php | 26 ++++++++++++- 2 files changed, 47 insertions(+), 16 deletions(-) diff --git a/extend/catcher/command/CatchScheduleCommand.php b/extend/catcher/command/CatchScheduleCommand.php index 6df9eee..49f9073 100644 --- a/extend/catcher/command/CatchScheduleCommand.php +++ b/extend/catcher/command/CatchScheduleCommand.php @@ -4,10 +4,11 @@ declare (strict_types = 1); namespace catcher\command; -use catcher\library\crontab\ManageProcess; +use catcher\library\crontab\Master; use think\console\Command; use think\console\Input; use think\console\input\Argument; +use think\console\input\Option; use think\console\Output; class CatchScheduleCommand extends Command @@ -17,36 +18,44 @@ class CatchScheduleCommand extends Command // 指令配置 $this->setName('catch:schedule') ->addArgument('option', Argument::OPTIONAL, '[start|reload|stop|restart||status]', 'start') + ->addOption('daemon', '-d', Option::VALUE_NONE, 'daemon mode') ->setDescription('start task schedule'); } protected function execute(Input $input, Output $output) { - $process = new ManageProcess(); - $option = $input->getArgument('option'); - $this->{$option}($process); + if (!extension_loaded('swoole')) { + $output->error('Swoole Extension Not Installed! You can use [pecl] to install swoole'); + } else { + $master = new Master(); + if ($this->input->hasOption('daemon')) { + $master->daemon(); + } + $this->{$input->getArgument('option')}($master); + } } /** * 进程启动 * * @time 2020年07月07日 - * @param ManageProcess $process + * @param Master $process * @return void */ - protected function start(ManageProcess $process) + protected function start(Master $process) { $process->start(); + $this->output->info($process->renderProcessesStatusToString()); } /** * 状态输出 * * @time 2020年07月07日 - * @param ManageProcess $process + * @param Master $process * @return void */ - protected function status(ManageProcess $process) + protected function status(Master $process) { $process->status(); @@ -57,10 +66,10 @@ class CatchScheduleCommand extends Command * 停止任务 * * @time 2020年07月07日 - * @param ManageProcess $process + * @param Master $process * @return void */ - protected function stop(ManageProcess $process) + protected function stop(Master $process) { $process->stop(); } @@ -69,10 +78,10 @@ class CatchScheduleCommand extends Command * 重启任务 * * @time 2020年07月07日 - * @param ManageProcess $process + * @param Master $process * @return void */ - protected function reload(ManageProcess $process) + protected function reload(Master $process) { $process->reload(); } @@ -81,10 +90,10 @@ class CatchScheduleCommand extends Command * 重启 * * @time 2020年07月07日 - * @param ManageProcess $process + * @param Master $process * @return void */ - protected function restart(ManageProcess $process) + protected function restart(Master $process) { $process->stop(); diff --git a/extend/catcher/library/crontab/Master.php b/extend/catcher/library/crontab/Master.php index 0fa4b9d..f44a076 100644 --- a/extend/catcher/library/crontab/Master.php +++ b/extend/catcher/library/crontab/Master.php @@ -65,6 +65,11 @@ class Master */ protected $master_start_at; + /** + * @var bool + */ + protected $daemon = false; + // 版本 const VERSION = '1.0.0'; @@ -82,7 +87,9 @@ class Master public function start() { // 守护进程 - //Process::daemon(true, false); + if ($this->daemon) { + Process::daemon(true, false); + } // alarm 信号 // Process::alarm(1000 * 1000); // 1s 调度一次 @@ -129,7 +136,7 @@ class Master { return function () { $schedule = new Schedule(); - $schedule->command('catch:cache')->everyTenSeconds(); + $schedule->command('catch:cache')->everyThirtySeconds(); foreach ($schedule->getCronTask() as $cron) { if ($cron->can()) { @@ -243,6 +250,8 @@ class Master $this->initLog(); + file_put_contents($this->getSaveProcessStatusFile(), ''); + $this->createTable(); } @@ -268,4 +277,17 @@ class Master 'default' => 'schedule', ], 'log'); } + + /** + * 开启 debug + * + * @time 2020年07月09日 + * @return $this + */ + public function daemon() + { + $this->daemon = true; + + return $this; + } } \ No newline at end of file