#yyds干货盘点#PHP实现定时任务hellogerard/jobby实例

文章目录
  • <?phpnamespace app\command;class Jobby extends \think\console\Command{ protected function configure() { $this->setName('jobby')->setDescription('jobby任务'); } protected function execute(\think\console\Input $input, \think\console\Output $output) { $jobby = new \Jobby\Jobby(); $config = config('jobby.list'); if (empty($config)) { return false; } foreach ($config as $k => $v) { $jobby->add($k, $v); } $jobby->run(); }}
  • <?php// +----------------------------------------------------------------------// | ThinkPHP [ WE CAN DO IT JUST THINK ]// +----------------------------------------------------------------------// | Copyright (c) 2006~2015 http://thinkphp.cn All rights reserved.// +----------------------------------------------------------------------// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )// +----------------------------------------------------------------------// | Author: yunwuxin <448901948@qq.com>// +----------------------------------------------------------------------namespace think\console;use think\Console;use think\console\input\Argument;use think\console\input\Definition;use think\console\input\Option;class Command{ /** @var Console */ private $console; private $name; private $aliases = []; private $definition; private $help; private $description; private $ignoreValidationErrors = false; private $consoleDefinitionMerged = false; private $consoleDefinitionMergedWithArgs = false; private $code; private $synopsis = []; private $usages = []; /** @var Input */ protected $input; /** @var Output */ protected $output; /** * 构造方法 * @param string|null $name 命令名称,如果没有设置则比如在 configure() 里设置 * @throws \LogicException * @api */ public function __construct($name = null) { $this->definition = new Definition(); if (null !== $name) { $this->setName($name); } $this->configure(); if (!$this->name) { throw new \LogicException(sprintf('The command defined in "%s" cannot have an empty name.', get_class($this))); } } /** * 忽略验证错误 */ public function ignoreValidationErrors() { $this->ignoreValidationErrors = true; } /** * 设置控制台 * @param Console $console */ public function setConsole(Console $console = null) { $this->console = $console; } /** * 获取控制台 * @return Console * @api */ public function getConsole() { return $this->console; } /** * 是否有效 * @return bool */ public function isEnabled() { return true; } /** * 配置指令 */ protected function configure() { } /** * 执行指令 * @param Input $input * @param Output $output * @return null|int * @throws \LogicException * @see setCode() */ protected function execute(Input $input, Output $output) { throw new \LogicException('You must override the execute() method in the concrete command class.'); } /** * 用户验证 * @param Input $input * @param Output $output */ protected function interact(Input $input, Output $output) { } /** * 初始化 * @param Input $input An InputInterface instance * @param Output $output An OutputInterface instance */ protected function initialize(Input $input, Output $output) { } /** * 执行 * @param Input $input * @param Output $output * @return int * @throws \Exception * @see setCode() * @see execute() */ public function run(Input $input, Output $output) { $this->input = $input; $this->output = $output; $this->getSynopsis(true); $this->getSynopsis(false); $this->mergeConsoleDefinition(); try { $input->bind($this->definition); } catch (\Exception $e) { if (!$this->ignoreValidationErrors) { throw $e; } } $this->initialize($input, $output); if ($input->isInteractive()) { $this->interact($input, $output); } $input->validate(); if ($this->code) { $statusCode = call_user_func($this->code, $input, $output); } else { $statusCode = $this->execute($input, $output); } return is_numeric($statusCode) ? (int) $statusCode : 0; } /** * 设置执行代码 * @param callable $code callable(InputInterface $input, OutputInterface $output) * @return Command * @throws \InvalidArgumentException * @see execute() */ public function setCode(callable $code) { if (!is_callable($code)) { throw new \InvalidArgumentException('Invalid callable provided to Command::setCode.'); } if (PHP_VERSION_ID >= 50400 && $code instanceof \Closure) { $r = new \ReflectionFunction($code); if (null === $r->getClosureThis()) { $code = \Closure::bind($code, $this); } } $this->code = $code; return $this; } /** * 合并参数定义 * @param bool $mergeArgs */ public function mergeConsoleDefinition($mergeArgs = true) { if (null === $this->console || (true === $this->consoleDefinitionMerged && ($this->consoleDefinitionMergedWithArgs || !$mergeArgs)) ) { return; } if ($mergeArgs) { $currentArguments = $this->definition->getArguments(); $this->definition->setArguments($this->console->getDefinition()->getArguments()); $this->definition->addArguments($currentArguments); } $this->definition->addOptions($this->console->getDefinition()->getOptions()); $this->consoleDefinitionMerged = true; if ($mergeArgs) { $this->consoleDefinitionMergedWithArgs = true; } } /** * 设置参数定义 * @param array|Definition $definition * @return Command * @api */ public function setDefinition($definition) { if ($definition instanceof Definition) { $this->definition = $definition; } else { $this->definition->setDefinition($definition); } $this->consoleDefinitionMerged = false; return $this; } /** * 获取参数定义 * @return Definition * @api */ public function getDefinition() { return $this->definition; } /** * 获取当前指令的参数定义 * @return Definition */ public function getNativeDefinition() { return $this->getDefinition(); } /** * 添加参数 * @param string $name 名称 * @param int $mode 类型 * @param string $description 描述 * @param mixed $default 默认值 * @return Command */ public function addArgument($name, $mode = null, $description = '', $default = null) { $this->definition->addArgument(new Argument($name, $mode, $description, $default)); return $this; } /** * 添加选项 * @param string $name 选项名称 * @param string $shortcut 别名 * @param int $mode 类型 * @param string $description 描述 * @param mixed $default 默认值 * @return Command */ public function addOption($name, $shortcut = null, $mode = null, $description = '', $default = null) { $this->definition->addOption(new Option($name, $shortcut, $mode, $description, $default)); return $this; } /** * 设置指令名称 * @param string $name * @return Command * @throws \InvalidArgumentException */ public function setName($name) { $this->validateName($name); $this->name = $name; return $this; } /** * 获取指令名称 * @return string */ public function getName() { return $this->name; } /** * 设置描述 * @param string $description * @return Command */ public function setDescription($description) { $this->description = $description; return $this; } /** * 获取描述 * @return string */ public function getDescription() { return $this->description; } /** * 设置帮助信息 * @param string $help * @return Command */ public function setHelp($help) { $this->help = $help; return $this; } /** * 获取帮助信息 * @return string */ public function getHelp() { return $this->help; } /** * 描述信息 * @return string */ public function getProcessedHelp() { $name = $this->name; $placeholders = [ '%command.name%', '%command.full_name%', ]; $replacements = [ $name, $_SERVER['PHP_SELF'] . ' ' . $name, ]; return str_replace($placeholders, $replacements, $this->getHelp()); } /** * 设置别名 * @param string[] $aliases * @return Command * @throws \InvalidArgumentException */ public function setAliases($aliases) { if (!is_array($aliases) && !$aliases instanceof \Traversable) { throw new \InvalidArgumentException('$aliases must be an array or an instance of \Traversable'); } foreach ($aliases as $alias) { $this->validateName($alias); } $this->aliases = $aliases; return $this; } /** * 获取别名 * @return array */ public function getAliases() { return $this->aliases; } /** * 获取简介 * @param bool $short 是否简单的 * @return string */ public function getSynopsis($short = false) { $key = $short ? 'short' : 'long'; if (!isset($this->synopsis[$key])) { $this->synopsis[$key] = trim(sprintf('%s %s', $this->name, $this->definition->getSynopsis($short))); } return $this->synopsis[$key]; } /** * 添加用法介绍 * @param string $usage * @return $this */ public function addUsage($usage) { if (0 !== strpos($usage, $this->name)) { $usage = sprintf('%s %s', $this->name, $usage); } $this->usages[] = $usage; return $this; } /** * 获取用法介绍 * @return array */ public function getUsages() { return $this->usages; } /** * 验证指令名称 * @param string $name * @throws \InvalidArgumentException */ private function validateName($name) { if (!preg_match('/^[^\:]++(\:[^\:]++)*$/', $name)) { throw new \InvalidArgumentException(sprintf('Command name "%s" is invalid.', $name)); } } /** * 输出表格 * @param Table $table * @return string */ protected function table(Table $table) { $content = $table->render(); $this->output->writeln($content); return $content; }}
  • <?php/* * jobby配置 */if (env('APP_ENV') == 'test' && env('APP_SYSTEM') == 'owenapi') { return [ 'list' => [ 'DailyTask' => [ 'command' => 'php /var/lib/owenproject/think DailyTask', 'schedule' => '00 00 * * *', 'enabled' => true, ], //自动推单'00 00 * * *'凌晨 '45 21 * * *'每晚 21:45 //'10 0 * * 0'每周日0点10执行 '* */2 * * *'每两个小时执行一次00 * * * *每整点小时(12点,13点,14点。。)执行一次//00 0/1 * * * * php /home/wwwroot/kaijiang-server-dev/think WormUpdateOneTeamInfoCommand//每分钟执行一次 'autoPushSheet' => [ 'command' => 'php /var/lib/owenproject/think autoPushSheet', 'schedule' => '45 21 * * *', 'enabled' => true, 'output' => 'runtime/log/'.date('Ym').'/'.date('d').'_distribute.log' ], ], ];}
  • <?phpnamespace app\command;use app\lucky\pay\service\UserGoldService;use app\lucky\common\InstanceTrait;class DailyTask extends \think\console\Command{ protected function configure() { $this->setName('DailyTask')->setDescription('每日任务重置'); } protected function execute(\think\console\Input $input, \think\console\Output $output) { \think\facade\Log::record('每日任务重置开始', 'business'); //业务逻辑 //每天凌晨删除key $key = 'sports:everyday:task:tate'; Redis::getInstance()->redisDel($key); \think\facade\Log::record('每日任务重置结束', 'business'); }}
  • Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式:  Seconds Minutes Hours DayofMonth Month DayofWeek Year或 Seconds Minutes Hours DayofMonth Month DayofWeek 每一个域可出现的字符如下: Seconds:可出现", - * /"四个字符,有效范围为0-59的整数 Minutes:可出现", - * /"四个字符,有效范围为0-59的整数 Hours:可出现", - * /"四个字符,有效范围为0-23的整数 DayofMonth:可出现", - * / ? L W C"八个字符,有效范围为0-31的整数 Month:可出现", - * /"四个字符,有效范围为1-12的整数或JAN-DEc DayofWeek:可出现", - * / ? L C #"四个字符,有效范围为1-7的整数或SUN-SAT两个范围。1表示星期天,2表示星期一, 依次类推 Year:可出现", - * /"四个字符,有效范围为1970-2099年 每一个域都使用数字,但还可以出现如下特殊字符,它们的含义是: (1)*:表示匹配该域的任意值,假如在Minutes域使用*, 即表示每分钟都会触发事件。 (2)?:只能用在DayofMonth和DayofWeek两个域。它也匹配域的任意值,但实际不会。因为DayofMonth和DayofWeek会相互影响。例如想在每月的20日触发调度,不管20日到底是星期几,则只能使用如下写法: 13 13 15 20 * ?, 其中最后一位只能用?,而不能使用*,如果使用*表示不管星期几都会触发,实际上并不是这样。 (3)-:表示范围,例如在Minutes域使用5-20,表示从5分到20分钟每分钟触发一次 (4)/:表示起始时间开始触发,然后每隔固定时间触发一次,例如在Minutes域使用5/20,则意味着5分钟触发一次,而25,45等分别触发一次. (5),:表示列出枚举值值。例如:在Minutes域使用5,20,则意味着在5和20分每分钟触发一次。 (6)L:表示最后,只能出现在DayofWeek和DayofMonth域,如果在DayofWeek域使用5L,意味着在最后的一个星期四触发。 (7)W:表示有效工作日(周一到周五),只能出现在DayofMonth域,系统将在离指定日期的最近的有效工作日触发事件。例如:在 DayofMonth使用5W,如果5日是星期六,则将在最近的工作日:星期五,即4日触发。如果5日是星期天,则在6日(周一)触发;如果5日在星期一到星期五中的一天,则就在5日触发。另外一点,W的最近寻找不会跨过月份 (8)LW:这两个字符可以连用,表示在某个月最后一个工作日,即最后一个星期五。 (9)#:用于确定每个月第几个星期几,只能出现在DayofMonth域。例如在4#2,表示某月的第二个星期三。 举几个例子: 0 0 2 1 * ? * 表示在每月的1日的凌晨2点调度任务 0 15 10 ? * MON-FRI 表示周一到周五每天上午10:15执行作业 0 15 10 ? 6L 2002-2006 表示2002-2006年的每个月的最后一个星期五上午10:15执行作 一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素。 按顺序依次为 秒(0~59) 分钟(0~59) 小时(0~23) 天(月)(0~31,但是你需要考虑你月的天数) 月(0~11) 天(星期)(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT) 年份(1970-2099) 其中每个元素可以是一个值(如6),一个连续区间(9-12),一个间隔时间(8-18/4)(/表示每隔4小时),一个列表(1,3,5),通配符。由于"月份中的日期"和"星期中的日期"这两个元素互斥的,必须要对其中一个设置? 0 0 10,14,16 * * ? 每天上午10点,下午2点,4点 0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时 0 0 12 ? * WED 表示每个星期三中午12点 "0 0 12 * * ?" 每天中午12点触发 "0 15 10 ? * *" 每天上午10:15触发 "0 15 10 * * ?" 每天上午10:15触发 "0 15 10 * * ? *" 每天上午10:15触发 "0 15 10 * * ? 2005" 2005年的每天上午10:15触发 "0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发 "0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发 "0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发 "0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发 "0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发 "0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发 "0 15 10 15 * ?" 每月15日上午10:15触发 "0 15 10 L * ?" 每月最后一日的上午10:15触发 "0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发 "0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发 "0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发 有些子表达式能包含一些范围或列表 例如:子表达式(天(星期))可以为 “MON-FRI”,“MON,WED,FRI”,“MON-WED,SAT” “*”字符代表所有可能的值 因此,“*”在子表达式(月)里表示每个月的含义,“*”在子表达式(天(星期))表示星期的每一天 “/”字符用来指定数值的增量 例如:在子表达式(分钟)里的“0/15”表示从第0分钟开始,每15分钟 在子表达式(分钟)里的“3/20”表示从第3分钟开始,每20分钟(它和“3,23,43”)的含义一样 “?”字符仅被用于天(月)和天(星期)两个子表达式,表示不指定值 当2个子表达式其中之一被指定了值以后,为了避免冲突,需要将另一个子表达式的值设为“?” “L” 字符仅被用于天(月)和天(星期)两个子表达式,它是单词“last”的缩写 但是它在两个子表达式里的含义是不同的。 在天(月)子表达式中,“L”表示一个月的最后一天 在天(星期)自表达式中,“L”表示一个星期的最后一天,也就是SAT 如果在“L”前有具体的内容,它就具有其他的含义了 例如:“6L”表示这个月的倒数第6天,“FRIL”表示这个月的最一个星期五 注意:在使用“L”参数时,不要指定列表或范围,因为这会导致问题 字段 允许值 允许的特殊字符 秒 0-59 , - * / 分 0-59 , - * / 小时 0-23 , - * / 日期 1-31 , - * ? / L W C 月份 1-12 或者 JAN-DEC , - * / 星期 1-7 或者 SUN-SAT , - * ? / L C # 年(可选) 留空, 1970-2099 , - * /
  • 觉得对你有帮助,就给我打赏吧,谢谢! ​​Buy me a cup of coffee :)​​ ​​www.owenzhang.com/wechat_rewa…​​
  • * 09 15 21 * * 2 php /home/wwwroot/kaijiang-server-dev/think WormInsertAllHPTeamStats
    * 每周二21点15分09秒

    //自动推单'00 00 * * *'凌晨 '45 21 * * *'每晚 21:45//'10 0 * * 0'每周日0点10执行 '* */2 * * *'每两个小时执行一次00 * * * *每整点小时(12点,13点,14点。。)执行一次//00 0/1 * * * * 每分钟执行一次 php /home/wwwroot/kaijiang-server-dev/think WormUpdateOneTeamInfoCommand

    tp5实现定时任务hellogerard/jobby实例

    每天凌晨删除指定redis,key

    <?php

    namespace app\command;


    class Jobby extends \think\console\Command
    {
    protected function configure()
    {
    $this->setName('jobby')->setDescription('jobby任务');
    }

    protected function execute(\think\console\Input $input, \think\console\Output $output)
    {

    $jobby = new \Jobby\Jobby();
    $config = config('jobby.list');
    if (empty($config)) {
    return false;
    }
    foreach ($config as $k => $v) {
    $jobby->add($k, $v);
    }

    $jobby->run();
    }
    }

    #yyds干货盘点#PHP实现定时任务hellogerard/jobby实例#yyds干货盘点#PHP实现定时任务hellogerard/jobby实例

    <?php
    // +----------------------------------------------------------------------
    // | ThinkPHP [ WE CAN DO IT JUST THINK ]
    // +----------------------------------------------------------------------
    // | Copyright (c) 2006~2015 http://thinkphp.cn All rights reserved.
    // +----------------------------------------------------------------------
    // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
    // +----------------------------------------------------------------------
    // | Author: yunwuxin <448901948@qq.com>
    // +----------------------------------------------------------------------

    namespace think\console;

    use think\Console;
    use think\console\input\Argument;
    use think\console\input\Definition;
    use think\console\input\Option;

    class Command
    {

    /** @var Console */
    private $console;
    private $name;
    private $aliases = [];
    private $definition;
    private $help;
    private $description;
    private $ignoreValidationErrors = false;
    private $consoleDefinitionMerged = false;
    private $consoleDefinitionMergedWithArgs = false;
    private $code;
    private $synopsis = [];
    private $usages = [];

    /** @var Input */
    protected $input;

    /** @var Output */
    protected $output;

    /**
    * 构造方法
    * @param string|null $name 命令名称,如果没有设置则比如在 configure() 里设置
    * @throws \LogicException
    * @api
    */
    public function __construct($name = null)
    {
    $this->definition = new Definition();

    if (null !== $name) {
    $this->setName($name);
    }

    $this->configure();

    if (!$this->name) {
    throw new \LogicException(sprintf('The command defined in "%s" cannot have an empty name.', get_class($this)));
    }
    }

    /**
    * 忽略验证错误
    */
    public function ignoreValidationErrors()
    {
    $this->ignoreValidationErrors = true;
    }

    /**
    * 设置控制台
    * @param Console $console
    */
    public function setConsole(Console $console = null)
    {
    $this->console = $console;
    }

    /**
    * 获取控制台
    * @return Console
    * @api
    */
    public function getConsole()
    {
    return $this->console;
    }

    /**
    * 是否有效
    * @return bool
    */
    public function isEnabled()
    {
    return true;
    }

    /**
    * 配置指令
    */
    protected function configure()
    {
    }

    /**
    * 执行指令
    * @param Input $input
    * @param Output $output
    * @return null|int
    * @throws \LogicException
    * @see setCode()
    */
    protected function execute(Input $input, Output $output)
    {
    throw new \LogicException('You must override the execute() method in the concrete command class.');
    }

    /**
    * 用户验证
    * @param Input $input
    * @param Output $output
    */
    protected function interact(Input $input, Output $output)
    {
    }

    /**
    * 初始化
    * @param Input $input An InputInterface instance
    * @param Output $output An OutputInterface instance
    */
    protected function initialize(Input $input, Output $output)
    {
    }

    /**
    * 执行
    * @param Input $input
    * @param Output $output
    * @return int
    * @throws \Exception
    * @see setCode()
    * @see execute()
    */
    public function run(Input $input, Output $output)
    {
    $this->input = $input;
    $this->output = $output;

    $this->getSynopsis(true);
    $this->getSynopsis(false);

    $this->mergeConsoleDefinition();

    try {
    $input->bind($this->definition);
    } catch (\Exception $e) {
    if (!$this->ignoreValidationErrors) {
    throw $e;
    }
    }

    $this->initialize($input, $output);

    if ($input->isInteractive()) {
    $this->interact($input, $output);
    }

    $input->validate();

    if ($this->code) {
    $statusCode = call_user_func($this->code, $input, $output);
    } else {
    $statusCode = $this->execute($input, $output);
    }

    return is_numeric($statusCode) ? (int) $statusCode : 0;
    }

    /**
    * 设置执行代码
    * @param callable $code callable(InputInterface $input, OutputInterface $output)
    * @return Command
    * @throws \InvalidArgumentException
    * @see execute()
    */
    public function setCode(callable $code)
    {
    if (!is_callable($code)) {
    throw new \InvalidArgumentException('Invalid callable provided to Command::setCode.');
    }

    if (PHP_VERSION_ID >= 50400 && $code instanceof \Closure) {
    $r = new \ReflectionFunction($code);
    if (null === $r->getClosureThis()) {
    $code = \Closure::bind($code, $this);
    }
    }

    $this->code = $code;

    return $this;
    }

    /**
    * 合并参数定义
    * @param bool $mergeArgs
    */
    public function mergeConsoleDefinition($mergeArgs = true)
    {
    if (null === $this->console
    || (true === $this->consoleDefinitionMerged
    && ($this->consoleDefinitionMergedWithArgs || !$mergeArgs))
    ) {
    return;
    }

    if ($mergeArgs) {
    $currentArguments = $this->definition->getArguments();
    $this->definition->setArguments($this->console->getDefinition()->getArguments());
    $this->definition->addArguments($currentArguments);
    }

    $this->definition->addOptions($this->console->getDefinition()->getOptions());

    $this->consoleDefinitionMerged = true;
    if ($mergeArgs) {
    $this->consoleDefinitionMergedWithArgs = true;
    }
    }

    /**
    * 设置参数定义
    * @param array|Definition $definition
    * @return Command
    * @api
    */
    public function setDefinition($definition)
    {
    if ($definition instanceof Definition) {
    $this->definition = $definition;
    } else {
    $this->definition->setDefinition($definition);
    }

    $this->consoleDefinitionMerged = false;

    return $this;
    }

    /**
    * 获取参数定义
    * @return Definition
    * @api
    */
    public function getDefinition()
    {
    return $this->definition;
    }

    /**
    * 获取当前指令的参数定义
    * @return Definition
    */
    public function getNativeDefinition()
    {
    return $this->getDefinition();
    }

    /**
    * 添加参数
    * @param string $name 名称
    * @param int $mode 类型
    * @param string $description 描述
    * @param mixed $default 默认值
    * @return Command
    */
    public function addArgument($name, $mode = null, $description = '', $default = null)
    {
    $this->definition->addArgument(new Argument($name, $mode, $description, $default));

    return $this;
    }

    /**
    * 添加选项
    * @param string $name 选项名称
    * @param string $shortcut 别名
    * @param int $mode 类型
    * @param string $description 描述
    * @param mixed $default 默认值
    * @return Command
    */
    public function addOption($name, $shortcut = null, $mode = null, $description = '', $default = null)
    {
    $this->definition->addOption(new Option($name, $shortcut, $mode, $description, $default));

    return $this;
    }

    /**
    * 设置指令名称
    * @param string $name
    * @return Command
    * @throws \InvalidArgumentException
    */
    public function setName($name)
    {
    $this->validateName($name);

    $this->name = $name;

    return $this;
    }

    /**
    * 获取指令名称
    * @return string
    */
    public function getName()
    {
    return $this->name;
    }

    /**
    * 设置描述
    * @param string $description
    * @return Command
    */
    public function setDescription($description)
    {
    $this->description = $description;

    return $this;
    }

    /**
    * 获取描述
    * @return string
    */
    public function getDescription()
    {
    return $this->description;
    }

    /**
    * 设置帮助信息
    * @param string $help
    * @return Command
    */
    public function setHelp($help)
    {
    $this->help = $help;

    return $this;
    }

    /**
    * 获取帮助信息
    * @return string
    */
    public function getHelp()
    {
    return $this->help;
    }

    /**
    * 描述信息
    * @return string
    */
    public function getProcessedHelp()
    {
    $name = $this->name;

    $placeholders = [
    '%command.name%',
    '%command.full_name%',
    ];
    $replacements = [
    $name,
    $_SERVER['PHP_SELF'] . ' ' . $name,
    ];

    return str_replace($placeholders, $replacements, $this->getHelp());
    }

    /**
    * 设置别名
    * @param string[] $aliases
    * @return Command
    * @throws \InvalidArgumentException
    */
    public function setAliases($aliases)
    {
    if (!is_array($aliases) && !$aliases instanceof \Traversable) {
    throw new \InvalidArgumentException('$aliases must be an array or an instance of \Traversable');
    }

    foreach ($aliases as $alias) {
    $this->validateName($alias);
    }

    $this->aliases = $aliases;

    return $this;
    }

    /**
    * 获取别名
    * @return array
    */
    public function getAliases()
    {
    return $this->aliases;
    }

    /**
    * 获取简介
    * @param bool $short 是否简单的
    * @return string
    */
    public function getSynopsis($short = false)
    {
    $key = $short ? 'short' : 'long';

    if (!isset($this->synopsis[$key])) {
    $this->synopsis[$key] = trim(sprintf('%s %s', $this->name, $this->definition->getSynopsis($short)));
    }

    return $this->synopsis[$key];
    }

    /**
    * 添加用法介绍
    * @param string $usage
    * @return $this
    */
    public function addUsage($usage)
    {
    if (0 !== strpos($usage, $this->name)) {
    $usage = sprintf('%s %s', $this->name, $usage);
    }

    $this->usages[] = $usage;

    return $this;
    }

    /**
    * 获取用法介绍
    * @return array
    */
    public function getUsages()
    {
    return $this->usages;
    }

    /**
    * 验证指令名称
    * @param string $name
    * @throws \InvalidArgumentException
    */
    private function validateName($name)
    {
    if (!preg_match('/^[^\:]++(\:[^\:]++)*$/', $name)) {
    throw new \InvalidArgumentException(sprintf('Command name "%s" is invalid.', $name));
    }
    }

    /**
    * 输出表格
    * @param Table $table
    * @return string
    */
    protected function table(Table $table)
    {
    $content = $table->render();
    $this->output->writeln($content);
    return $content;
    }
    }

    #yyds干货盘点#PHP实现定时任务hellogerard/jobby实例#yyds干货盘点#PHP实现定时任务hellogerard/jobby实例

    <?php

    /*
    * jobby配置
    */

    if (env('APP_ENV') == 'test' && env('APP_SYSTEM') == 'owenapi') {
    return [
    'list' => [
    'DailyTask' => [
    'command' => 'php /var/lib/owenproject/think DailyTask',
    'schedule' => '00 00 * * *',
    'enabled' => true,
    ],
    //自动推单'00 00 * * *'凌晨 '45 21 * * *'每晚 21:45
    //'10 0 * * 0'每周日0点10执行 '* */2 * * *'每两个小时执行一次00 * * * *每整点小时(12点,13点,14点。。)执行一次
    //00 0/1 * * * * php /home/wwwroot/kaijiang-server-dev/think WormUpdateOneTeamInfoCommand
    //每分钟执行一次
    'autoPushSheet' => [
    'command' => 'php /var/lib/owenproject/think autoPushSheet',
    'schedule' => '45 21 * * *',
    'enabled' => true,
    'output' => 'runtime/log/'.date('Ym').'/'.date('d').'_distribute.log'
    ],
    ],
    ];
    }

    #yyds干货盘点#PHP实现定时任务hellogerard/jobby实例#yyds干货盘点#PHP实现定时任务hellogerard/jobby实例

    <?php


    namespace app\command;

    use app\lucky\pay\service\UserGoldService;
    use app\lucky\common\InstanceTrait;

    class DailyTask extends \think\console\Command
    {
    protected function configure()
    {
    $this->setName('DailyTask')->setDescription('每日任务重置');
    }

    protected function execute(\think\console\Input $input, \think\console\Output $output)
    {
    \think\facade\Log::record('每日任务重置开始', 'business');
    //业务逻辑
    //每天凌晨删除key
    $key = 'sports:everyday:task:tate';
    Redis::getInstance()->redisDel($key);
    \think\facade\Log::record('每日任务重置结束', 'business');
    }
    }

    #yyds干货盘点#PHP实现定时任务hellogerard/jobby实例#yyds干货盘点#PHP实现定时任务hellogerard/jobby实例

    Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式: 

    Seconds Minutes Hours DayofMonth Month DayofWeek Year或
    Seconds Minutes Hours DayofMonth Month DayofWeek

    每一个域可出现的字符如下:

    Seconds:可出现", - * /"四个字符,有效范围为0-59的整数

    Minutes:可出现", - * /"四个字符,有效范围为0-59的整数

    Hours:可出现", - * /"四个字符,有效范围为0-23的整数

    DayofMonth:可出现", - * / ? L W C"八个字符,有效范围为0-31的整数

    Month:可出现", - * /"四个字符,有效范围为1-12的整数或JAN-DEc

    DayofWeek:可出现", - * / ? L C #"四个字符,有效范围为1-7的整数或SUN-SAT两个范围。1表示星期天,2表示星期一, 依次类推

    Year:可出现", - * /"四个字符,有效范围为1970-2099年

    每一个域都使用数字,但还可以出现如下特殊字符,它们的含义是:

    (1)*:表示匹配该域的任意值,假如在Minutes域使用*, 即表示每分钟都会触发事件。

    (2)?:只能用在DayofMonth和DayofWeek两个域。它也匹配域的任意值,但实际不会。因为DayofMonth和DayofWeek会相互影响。例如想在每月的20日触发调度,不管20日到底是星期几,则只能使用如下写法: 13 13 15 20 * ?, 其中最后一位只能用?,而不能使用*,如果使用*表示不管星期几都会触发,实际上并不是这样。

    (3)-:表示范围,例如在Minutes域使用5-20,表示从5分到20分钟每分钟触发一次

    (4)/:表示起始时间开始触发,然后每隔固定时间触发一次,例如在Minutes域使用5/20,则意味着5分钟触发一次,而25,45等分别触发一次.

    (5),:表示列出枚举值值。例如:在Minutes域使用5,20,则意味着在5和20分每分钟触发一次。

    (6)L:表示最后,只能出现在DayofWeek和DayofMonth域,如果在DayofWeek域使用5L,意味着在最后的一个星期四触发。

    (7)W:表示有效工作日(周一到周五),只能出现在DayofMonth域,系统将在离指定日期的最近的有效工作日触发事件。例如:在 DayofMonth使用5W,如果5日是星期六,则将在最近的工作日:星期五,即4日触发。如果5日是星期天,则在6日(周一)触发;如果5日在星期一到星期五中的一天,则就在5日触发。另外一点,W的最近寻找不会跨过月份

    (8)LW:这两个字符可以连用,表示在某个月最后一个工作日,即最后一个星期五。

    (9)#:用于确定每个月第几个星期几,只能出现在DayofMonth域。例如在4#2,表示某月的第二个星期三。

    举几个例子:

    0 0 2 1 * ? * 表示在每月的1日的凌晨2点调度任务

    0 15 10 ? * MON-FRI 表示周一到周五每天上午10:15执行作业

    0 15 10 ? 6L 2002-2006 表示2002-2006年的每个月的最后一个星期五上午10:15执行作

    一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素。

    按顺序依次为

    秒(0~59)

    分钟(0~59)

    小时(0~23)

    天(月)(0~31,但是你需要考虑你月的天数)

    月(0~11)

    天(星期)(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT)

    年份(1970-2099)

    其中每个元素可以是一个值(如6),一个连续区间(9-12),一个间隔时间(8-18/4)(/表示每隔4小时),一个列表(1,3,5),通配符。由于"月份中的日期"和"星期中的日期"这两个元素互斥的,必须要对其中一个设置?

    0 0 10,14,16 * * ? 每天上午10点,下午2点,4点

    0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时

    0 0 12 ? * WED 表示每个星期三中午12点

    "0 0 12 * * ?" 每天中午12点触发

    "0 15 10 ? * *" 每天上午10:15触发

    "0 15 10 * * ?" 每天上午10:15触发

    "0 15 10 * * ? *" 每天上午10:15触发

    "0 15 10 * * ? 2005" 2005年的每天上午10:15触发

    "0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发

    "0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发

    "0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发

    "0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发

    "0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发

    "0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发

    "0 15 10 15 * ?" 每月15日上午10:15触发

    "0 15 10 L * ?" 每月最后一日的上午10:15触发

    "0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发

    "0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发

    "0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发

    有些子表达式能包含一些范围或列表

    例如:子表达式(天(星期))可以为 “MON-FRI”,“MON,WED,FRI”,“MON-WED,SAT”

    “*”字符代表所有可能的值

    因此,“*”在子表达式(月)里表示每个月的含义,“*”在子表达式(天(星期))表示星期的每一天

    “/”字符用来指定数值的增量

    例如:在子表达式(分钟)里的“0/15”表示从第0分钟开始,每15分钟

    在子表达式(分钟)里的“3/20”表示从第3分钟开始,每20分钟(它和“3,23,43”)的含义一样

    “?”字符仅被用于天(月)和天(星期)两个子表达式,表示不指定值

    当2个子表达式其中之一被指定了值以后,为了避免冲突,需要将另一个子表达式的值设为“?”

    “L” 字符仅被用于天(月)和天(星期)两个子表达式,它是单词“last”的缩写

    但是它在两个子表达式里的含义是不同的。

    在天(月)子表达式中,“L”表示一个月的最后一天

    在天(星期)自表达式中,“L”表示一个星期的最后一天,也就是SAT

    如果在“L”前有具体的内容,它就具有其他的含义了

    例如:“6L”表示这个月的倒数第6天,“FRIL”表示这个月的最一个星期五

    注意:在使用“L”参数时,不要指定列表或范围,因为这会导致问题

    字段 允许值 允许的特殊字符

    秒 0-59 , - * /

    分 0-59 , - * /

    小时 0-23 , - * /

    日期 1-31 , - * ? / L W C

    月份 1-12 或者 JAN-DEC , - * /

    星期 1-7 或者 SUN-SAT , - * ? / L C #

    年(可选) 留空, 1970-2099 , - * /

    觉得对你有帮助,就给我打赏吧,谢谢!

    ​Buy me a cup of coffee :)​

    ​www.owenzhang.com/wechat_rewa…​

    © 版权声明

    相关文章

    没有相关内容!