Files
kiri-core/System/Crontab/Crontab.php
T

300 lines
5.8 KiB
PHP
Raw Normal View History

2021-03-19 17:47:41 +08:00
<?php
2021-03-26 01:01:21 +08:00
namespace Snowflake\Crontab;
2021-03-19 17:47:41 +08:00
use Exception;
2021-04-12 11:05:57 +08:00
use JetBrains\PhpStorm\Pure;
2021-03-19 19:16:12 +08:00
use Snowflake\Abstracts\BaseObject;
2021-04-14 14:46:55 +08:00
use Snowflake\Core\Json;
2021-04-12 02:55:12 +08:00
use Snowflake\Snowflake;
2021-03-20 02:33:50 +08:00
use Swoole\Timer;
2021-03-19 17:47:41 +08:00
/**
* Class Async
* @package Snowflake
*/
2021-04-14 14:14:12 +08:00
abstract class Crontab extends BaseObject
2021-03-19 17:47:41 +08:00
{
2021-04-16 00:22:26 +08:00
const WAIT_END = 'crontab:wait:execute';
2021-03-19 17:47:41 +08:00
2021-03-19 18:52:30 +08:00
2021-04-16 00:22:26 +08:00
private string $name = '';
2021-03-19 18:52:30 +08:00
2021-04-16 00:22:26 +08:00
private mixed $params = [];
2021-03-20 02:33:50 +08:00
2021-04-16 00:22:26 +08:00
private int $tickTime = 1;
2021-03-20 02:33:50 +08:00
2021-04-16 00:22:26 +08:00
private bool $isLoop = false;
2021-03-20 02:33:50 +08:00
2021-04-16 00:22:26 +08:00
private int $timerId = -1;
2021-03-20 02:33:50 +08:00
2021-04-16 00:22:26 +08:00
private int $max_execute_number = -1;
2021-03-20 02:33:50 +08:00
2021-04-14 11:21:43 +08:00
2021-04-16 00:22:26 +08:00
private int $execute_number = 0;
2021-03-20 02:33:50 +08:00
2021-03-21 23:53:48 +08:00
2021-04-16 00:22:26 +08:00
/**
* @return $this
*/
public function increment(): static
{
$this->execute_number += 1;
return $this;
}
/**
* @return string
*/
#[Pure] public function getName(): string
{
return md5($this->name);
}
/**
* @return mixed
*/
public function getParams(): mixed
{
return $this->params;
}
/**
* @return int
*/
public function getTickTime(): int
{
return $this->tickTime;
}
/**
* @return bool
*/
public function isLoop(): bool
{
return $this->isLoop;
}
/**
* @return int
*/
public function getMaxExecuteNumber(): int
{
return $this->max_execute_number;
}
/**
* @return int
*/
public function getExecuteNumber(): int
{
return $this->execute_number;
}
/**
* @param string $name
*/
public function setName(string $name): void
{
$this->name = $name;
}
public function setParams(): void
{
$this->params = func_get_args();
}
/**
* @param int $tickTime
*/
public function setTickTime(int $tickTime): void
{
$this->tickTime = $tickTime;
}
/**
* @param bool $isLoop
*/
public function setIsLoop(bool $isLoop): void
{
$this->isLoop = $isLoop;
}
/**
* @param int $max_execute_number
*/
public function setMaxExecuteNumber(int $max_execute_number): void
{
$this->max_execute_number = $max_execute_number;
}
/**
* @param int $execute_number
*/
public function setExecuteNumber(int $execute_number): void
{
$this->execute_number = $execute_number;
}
/**
* @return int
*/
public function getTimerId(): int
{
return $this->timerId;
}
/**
* @param int $timerId
*/
public function setTimerId(int $timerId): void
{
$this->timerId = $timerId;
}
/**
*
* @throws Exception
*/
public function clearTimer()
{
$this->warning('crontab timer clear.');
if (Timer::exists($this->timerId)) {
Timer::clear($this->timerId);
}
}
/**
* @throws Exception
*/
2021-04-16 10:18:02 +08:00
private function recover(): bool
2021-04-16 00:22:26 +08:00
{
$redis = Snowflake::app()->getRedis();
2021-04-16 00:39:23 +08:00
if ($redis->exists('stop:crontab:' . $this->getName())) {
2021-04-16 00:22:26 +08:00
$redis->del('crontab:' . $this->getName());
$redis->del('stop:crontab:' . $this->getName());
} else {
$redis->set('crontab:' . ($name = $this->getName()), swoole_serialize($this));
$tickTime = time() + $this->getTickTime();
$redis->zAdd(Producer::CRONTAB_KEY, $tickTime, $name);
}
2021-04-16 10:18:02 +08:00
return true;
2021-04-16 00:22:26 +08:00
}
abstract public function process(): mixed;
abstract public function max_execute(): mixed;
abstract public function isStop(): bool;
/**
* @throws Exception
*/
public function execute(): void
{
2021-04-16 15:43:50 +08:00
defer(function () {
$this->isRecover();
});
2021-04-16 00:22:26 +08:00
$this->run();
}
2021-04-17 15:45:02 +08:00
/**
* @return bool|int
* @throws Exception
*/
2021-04-16 10:18:02 +08:00
public function isRecover(): bool|int
2021-04-16 00:22:26 +08:00
{
try {
$redis = Snowflake::app()->getRedis();
2021-04-17 15:45:02 +08:00
if ($redis->exists('stop:crontab:' . $this->getName())) {
$redis->del('stop:crontab:' . $this->getName());
return true;
}
2021-04-16 10:18:02 +08:00
if ($this->isExit()) {
2021-04-16 00:22:26 +08:00
return $redis->del('crontab:' . $this->getName());
}
2021-04-16 10:18:02 +08:00
if ($this->isMaxExecute()) {
call_user_func([$this, 'max_execute'], ...$this->getParams());
2021-04-16 00:22:26 +08:00
return $redis->del('crontab:' . $this->getName());
} else {
2021-04-16 10:18:02 +08:00
return $this->recover();
2021-04-16 00:22:26 +08:00
}
} catch (\Throwable $throwable) {
return logger()->addError($throwable, 'throwable');
}
}
/**
* @throws Exception
*/
private function run()
{
try {
$redis = Snowflake::app()->getRedis();
$name_md5 = $this->getName();
$redis->hSet(self::WAIT_END, $name_md5, serialize($this));
$params = call_user_func([$this, 'process'], ...$this->params);
$redis->hDel(self::WAIT_END, $name_md5);
if ($params === null) {
return;
}
$name = date('Y-m-d.log');
write(storage($name, '/log/crontab'), Json::encode([
'name' => $this->name,
'response' => serialize($params)
]));
} catch (\Throwable $throwable) {
logger()->addError($throwable, 'throwable');
}
}
/**
* @return bool
*/
private function isExit(): bool
{
if ($this->isStop()) {
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;
}
2021-04-14 14:14:12 +08:00
2021-03-19 17:47:41 +08:00
}