catchAdmin/extend/catcher/command/CatchScheduleCommand.php

105 lines
2.3 KiB
PHP
Raw Normal View History

2020-07-07 16:58:16 +08:00
<?php
declare (strict_types = 1);
namespace catcher\command;
2020-07-09 21:56:17 +08:00
use catcher\library\crontab\Master;
2020-07-07 16:58:16 +08:00
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
2020-07-09 21:56:17 +08:00
use think\console\input\Option;
2020-07-07 16:58:16 +08:00
use think\console\Output;
class CatchScheduleCommand extends Command
{
protected function configure()
{
// 指令配置
$this->setName('catch:schedule')
->addArgument('option', Argument::OPTIONAL, '[start|reload|stop|restart||status]', 'start')
2020-07-09 21:56:17 +08:00
->addOption('daemon', '-d', Option::VALUE_NONE, 'daemon mode')
2020-07-07 16:58:16 +08:00
->setDescription('start task schedule');
}
protected function execute(Input $input, Output $output)
{
2020-07-09 21:56:17 +08:00
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);
}
2020-07-07 16:58:16 +08:00
}
/**
* 进程启动
*
* @time 2020年07月07日
2020-07-09 21:56:17 +08:00
* @param Master $process
2020-07-07 16:58:16 +08:00
* @return void
*/
2020-07-09 21:56:17 +08:00
protected function start(Master $process)
2020-07-07 16:58:16 +08:00
{
$process->start();
2020-07-09 21:56:17 +08:00
$this->output->info($process->renderProcessesStatusToString());
2020-07-07 16:58:16 +08:00
}
/**
* 状态输出
*
* @time 2020年07月07日
2020-07-09 21:56:17 +08:00
* @param Master $process
2020-07-07 16:58:16 +08:00
* @return void
*/
2020-07-09 21:56:17 +08:00
protected function status(Master $process)
2020-07-07 16:58:16 +08:00
{
$process->status();
$this->output->info($process->output());
}
/**
* 停止任务
*
* @time 2020年07月07日
2020-07-09 21:56:17 +08:00
* @param Master $process
2020-07-07 16:58:16 +08:00
* @return void
*/
2020-07-09 21:56:17 +08:00
protected function stop(Master $process)
2020-07-07 16:58:16 +08:00
{
$process->stop();
2020-08-08 20:47:52 +08:00
$this->output->info('stop catch schedule successfully');
2020-07-07 16:58:16 +08:00
}
/**
* 重启任务
*
* @time 2020年07月07日
2020-07-09 21:56:17 +08:00
* @param Master $process
2020-07-07 16:58:16 +08:00
* @return void
*/
2020-07-09 21:56:17 +08:00
protected function reload(Master $process)
2020-07-07 16:58:16 +08:00
{
$process->reload();
}
/**
* 重启
*
* @time 2020年07月07日
2020-07-09 21:56:17 +08:00
* @param Master $process
2020-07-07 16:58:16 +08:00
* @return void
*/
2020-07-09 21:56:17 +08:00
protected function restart(Master $process)
2020-07-07 16:58:16 +08:00
{
$process->stop();
$process->start();
}
}