This commit is contained in:
2021-04-14 11:21:43 +08:00
parent f230c0902f
commit ef86d774ed
+186 -184
View File
@@ -8,7 +8,6 @@ use Closure;
use Exception; use Exception;
use JetBrains\PhpStorm\Pure; use JetBrains\PhpStorm\Pure;
use Snowflake\Abstracts\BaseObject; use Snowflake\Abstracts\BaseObject;
use Snowflake\Event;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use Swoole\Timer; use Swoole\Timer;
@@ -19,236 +18,239 @@ use Swoole\Timer;
class Crontab extends BaseObject class Crontab extends BaseObject
{ {
const WAIT_END = 'crontab:wait:execute'; const WAIT_END = 'crontab:wait:execute';
private array|Closure $handler; private array|Closure $handler;
private string $name = ''; private string $name = '';
private mixed $params = null; private mixed $params = null;
private int $tickTime = 1; private int $tickTime = 1;
private bool $isLoop = false; private bool $isLoop = false;
private int $timerId = -1; private int $timerId = -1;
private int $max_execute_number = -1; private int $max_execute_number = -1;
private int $execute_number = 0; private int $execute_number = 0;
/** /**
* @return $this * @return $this
*/ */
public function increment(): static public function increment(): static
{ {
$this->execute_number += 1; $this->execute_number += 1;
return $this; return $this;
} }
/** /**
* @return array|Closure * @return array|Closure
*/ */
public function getHandler(): array|Closure public function getHandler(): array|Closure
{ {
return $this->handler; return $this->handler;
} }
/** /**
* @return string * @return string
*/ */
#[Pure] public function getName(): string #[Pure] public function getName(): string
{ {
return md5($this->name); return md5($this->name);
} }
/** /**
* @return mixed * @return mixed
*/ */
public function getParams(): mixed public function getParams(): mixed
{ {
return $this->params; return $this->params;
} }
/** /**
* @return int * @return int
*/ */
public function getTickTime(): int public function getTickTime(): int
{ {
return $this->tickTime; return $this->tickTime;
} }
/** /**
* @return bool * @return bool
*/ */
public function isLoop(): bool public function isLoop(): bool
{ {
return $this->isLoop; return $this->isLoop;
} }
/** /**
* @return int * @return int
*/ */
public function getMaxExecuteNumber(): int public function getMaxExecuteNumber(): int
{ {
return $this->max_execute_number; return $this->max_execute_number;
} }
/** /**
* @return int * @return int
*/ */
public function getExecuteNumber(): int public function getExecuteNumber(): int
{ {
return $this->execute_number; return $this->execute_number;
} }
/** /**
* @param array|Closure $handler * @param array|Closure $handler
*/ */
public function setHandler(array|Closure $handler): void public function setHandler(array|Closure $handler): void
{ {
$this->handler = $handler; $this->handler = $handler;
} }
/** /**
* @param string $name * @param string $name
*/ */
public function setName(string $name): void public function setName(string $name): void
{ {
$this->name = $name; $this->name = $name;
} }
/** /**
* @param mixed $params * @param mixed $params
*/ */
public function setParams(mixed $params): void public function setParams(...$params): void
{ {
$this->params = $params; $this->params = $params;
} }
/** /**
* @param int $tickTime * @param int $tickTime
*/ */
public function setTickTime(int $tickTime): void public function setTickTime(int $tickTime): void
{ {
$this->tickTime = $tickTime; $this->tickTime = $tickTime;
} }
/** /**
* @param bool $isLoop * @param bool $isLoop
*/ */
public function setIsLoop(bool $isLoop): void public function setIsLoop(bool $isLoop): void
{ {
$this->isLoop = $isLoop; $this->isLoop = $isLoop;
} }
/** /**
* @param int $max_execute_number * @param int $max_execute_number
*/ */
public function setMaxExecuteNumber(int $max_execute_number): void public function setMaxExecuteNumber(int $max_execute_number): void
{ {
$this->max_execute_number = $max_execute_number; $this->max_execute_number = $max_execute_number;
} }
/** /**
* @param int $execute_number * @param int $execute_number
*/ */
public function setExecuteNumber(int $execute_number): void public function setExecuteNumber(int $execute_number): void
{ {
$this->execute_number = $execute_number; $this->execute_number = $execute_number;
} }
/** /**
* @return int * @return int
*/ */
public function getTimerId(): int public function getTimerId(): int
{ {
return $this->timerId; return $this->timerId;
} }
/** /**
* @param int $timerId * @param int $timerId
*/ */
public function setTimerId(int $timerId): void public function setTimerId(int $timerId): void
{ {
$this->timerId = $timerId; $this->timerId = $timerId;
} }
/** /**
* *
* @throws Exception * @throws Exception
*/ */
public function clearTimer() public function clearTimer()
{ {
$this->warning('crontab timer clear.'); $this->warning('crontab timer clear.');
if (Timer::exists($this->timerId)) { if (Timer::exists($this->timerId)) {
Timer::clear($this->timerId); Timer::clear($this->timerId);
} }
} }
/** /**
* @throws \Exception * @throws \Exception
*/ */
private function recover() private function recover()
{ {
$redis = Snowflake::app()->getRedis(); $redis = Snowflake::app()->getRedis();
$redis->set('crontab:' . ($name = $this->getName()), swoole_serialize($this)); $redis->set('crontab:' . ($name = $this->getName()), swoole_serialize($this));
$tickTime = time() + $this->getTickTime(); $tickTime = time() + $this->getTickTime();
$redis->zAdd(Producer::CRONTAB_KEY, $tickTime, $name); $redis->zAdd(Producer::CRONTAB_KEY, $tickTime, $name);
} }
/** /**
* @throws Exception * @throws Exception
*/ */
public function execute(): void public function execute(): void
{ {
try { try {
$redis = Snowflake::app()->getRedis(); $redis = Snowflake::app()->getRedis();
$name_md5 = $this->getName(); $name_md5 = $this->getName();
$redis->hSet(self::WAIT_END, $name_md5, serialize($this)); $redis->hSet(self::WAIT_END, $name_md5, serialize($this));
$params = call_user_func($this->handler, $this->params, $this->name);
$redis->hDel(self::WAIT_END, $name_md5); array_push($this->params, $this->name);
if ($params === null) {
return; $params = call_user_func($this->handler, ...$this->params);
} $redis->hDel(self::WAIT_END, $name_md5);
$name = date('Y_m_d_H_i_s.' . $this->name . '.log'); if ($params === null) {
write(storage($name, '/log/crontab'), serialize($params)); return;
} catch (\Throwable $throwable) { }
logger()->addError($throwable, 'throwable'); $name = date('Y_m_d_H_i_s.' . $this->name . '.log');
} finally { write(storage($name, '/log/crontab'), serialize($params));
$this->after(); } catch (\Throwable $throwable) {
} logger()->addError($throwable, 'throwable');
} } finally {
$this->after();
}
}
/** /**
* @throws \Exception * @throws \Exception
*/ */
private function after(): void private function after(): void
{ {
if ($this->getExecuteNumber() < $this->getMaxExecuteNumber()) { if ($this->getExecuteNumber() < $this->getMaxExecuteNumber()) {
$this->recover(); $this->recover();
} else if ($this->isLoop()) { } else if ($this->isLoop()) {
$this->recover(); $this->recover();
} }
} }
} }