2020-07-04 10:43:09 +08:00
|
|
|
|
<?php
|
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
|
// | CatchAdmin [Just Like ~ ]
|
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
|
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
|
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
|
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
|
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
|
// | Author: JaguarJack [ njphper@gmail.com ]
|
|
|
|
|
// +----------------------------------------------------------------------
|
2020-07-07 16:58:34 +08:00
|
|
|
|
namespace catcher\library\crontab;
|
|
|
|
|
|
|
|
|
|
use Cron\CronExpression;
|
|
|
|
|
use think\facade\Console;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class Cron
|
|
|
|
|
* @package catcher\library\crontab
|
|
|
|
|
*
|
|
|
|
|
* // cron 表达式
|
|
|
|
|
* * * * *
|
|
|
|
|
* - - - - -
|
|
|
|
|
* | | | | |
|
|
|
|
|
* | | | | |
|
|
|
|
|
* | | | | +----- day of week (0 - 6) (Sunday=0)
|
|
|
|
|
* | | | +---------- month (1 - 12)
|
|
|
|
|
* | | +--------------- day of month (1 - 31)
|
|
|
|
|
* | +-------------------- hour (0 - 23)
|
|
|
|
|
* +------------------------- min (0 - 59)
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
class Cron
|
|
|
|
|
{
|
|
|
|
|
use Frequencies;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* crontab 表达式
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
protected $expression = '* * * * *';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* task 任务
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
protected $task;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* console 命令
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
protected $console;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* console 参数
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected $arguments;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 秒级支持
|
|
|
|
|
*
|
|
|
|
|
* @var int
|
|
|
|
|
*/
|
|
|
|
|
protected $second;
|
|
|
|
|
|
|
|
|
|
public function __construct($name, $arguments = [])
|
|
|
|
|
{
|
|
|
|
|
if (is_string($name)) {
|
|
|
|
|
$this->console = $name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (is_object($name)) {
|
|
|
|
|
$this->task = $name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->arguments = $arguments;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 运行 cron 任务
|
|
|
|
|
*
|
|
|
|
|
* @time 2020年07月04日
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function run()
|
|
|
|
|
{
|
|
|
|
|
if ($this->console) {
|
|
|
|
|
Console::call($this->console, $this->arguments);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($this->task && method_exists($this->task, 'run')) {
|
|
|
|
|
$this->task->run();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 是否可运行
|
|
|
|
|
*
|
|
|
|
|
* @time 2020年07月04日
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2020-07-08 17:48:45 +08:00
|
|
|
|
public function can()
|
2020-07-07 16:58:34 +08:00
|
|
|
|
{
|
|
|
|
|
if ($this->second) {
|
|
|
|
|
$now = date('s', time());
|
|
|
|
|
|
2020-07-08 17:48:45 +08:00
|
|
|
|
return ($now % 5) == 0;
|
2020-07-07 16:58:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($this->expression) {
|
|
|
|
|
$cron = CronExpression::factory($this->expression);
|
2020-07-08 17:48:45 +08:00
|
|
|
|
return $cron->getNextRunDate(date('Y-m-d H:i:s'), 0 , true)->getTimestamp() == time();
|
2020-07-07 16:58:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|