From ebcb9735c7fcf672f594e5cd72a0f4eedbd03b1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Wed, 14 Apr 2021 14:14:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- System/Crontab/Crontab.php | 52 +++++++++++++++++++++++-------- System/Crontab/DefaultCrontab.php | 38 ++++++++++++++++++++++ 2 files changed, 77 insertions(+), 13 deletions(-) create mode 100644 System/Crontab/DefaultCrontab.php diff --git a/System/Crontab/Crontab.php b/System/Crontab/Crontab.php index 599c2827..0b3d28a4 100644 --- a/System/Crontab/Crontab.php +++ b/System/Crontab/Crontab.php @@ -15,7 +15,7 @@ use Swoole\Timer; * Class Async * @package Snowflake */ -class Crontab extends BaseObject +abstract class Crontab extends BaseObject { const WAIT_END = 'crontab:wait:execute'; @@ -236,6 +236,12 @@ class Crontab extends BaseObject } + abstract public function process(): void; + + abstract public function max_execute(): void; + abstract public function isStop(): bool; + + /** * @throws Exception */ @@ -250,7 +256,7 @@ class Crontab extends BaseObject array_push($this->params, $this->name); - $params = call_user_func($this->handler, ...$this->params); + $params = call_user_func([$this, 'process'], ...$this->params); $redis->hDel(self::WAIT_END, $name_md5); if ($params === null) { return; @@ -265,25 +271,45 @@ class Crontab extends BaseObject } + /** + * @return bool + */ + private function isExit(): bool + { + if ($this->_stop === true) { + return true; + } + if (!$this->isLoop) { + return true; + } + return false; + } + + + /** + * @return bool + */ + private function isMaxExecute(): bool + { + if ($this->max_execute_number !== -1) { + return $this->execute_number >= $this->max_execute_number; + } + return false; + } + + /** * @throws Exception */ private function after(): void { - if ($this->_stop === true) { + if ($this->isExit()) { return; } - if ($this->isLoop()) { - $this->recover(); - return; - } - if ($this->max_execute_number == -1) { - return; - } - if ($this->getExecuteNumber() < $this->getMaxExecuteNumber()) { - $this->recover(); + if ($this->isMaxExecute()) { + call_user_func([$this, 'max_execute'], ...$this->params); } else { - call_user_func($this->_max_execute_handler, ...$this->params); + $this->recover(); } } diff --git a/System/Crontab/DefaultCrontab.php b/System/Crontab/DefaultCrontab.php new file mode 100644 index 00000000..67a196e1 --- /dev/null +++ b/System/Crontab/DefaultCrontab.php @@ -0,0 +1,38 @@ +