1 Star 0 Fork 0

向林 / kiri-crontab

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Crontab.php 3.79 KB
一键复制 编辑 原始数据 按行查看 历史
向林 提交于 2022-01-12 14:10 . Revert "改名"
<?php
namespace Kiri\Crontab;
use Exception;
use JetBrains\PhpStorm\Pure;
use Kiri\Application;
use Kiri\Cache\Redis;
use Kiri\Context;
use Kiri;
use Psr\Log\LoggerInterface;
use ReflectionException;
/**
* Class Async
* @package Kiri
* @property Application $application
*/
abstract class Crontab implements CrontabInterface
{
const WAIT_END = 'crontab:wait:execute';
public string $name = '';
public mixed $params;
public int $tickTime;
public bool $isLoop;
public int $retry = 1;
/**
* Crontab constructor.
* @param mixed $params
* @param false $isLoop
* @param int $tickTime
* @param int $retry
*/
public function __construct(mixed $params, bool $isLoop = false, int $tickTime = 1, int $retry = 1)
{
$this->params = $params;
$this->isLoop = $isLoop;
$this->tickTime = $tickTime;
$this->retry = $retry;
}
/**
* @return Application
*/
#[Pure] private function getApplication(): Application
{
return Kiri::app();
}
/**
* @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;
}
/**
* @param string $name
*/
public function setName(string $name): void
{
$this->name = $name;
}
/**
* @return array
*/
public function __serialize(): array
{
return [
'name' => $this->name,
'params' => $this->params,
'tickTime' => $this->tickTime,
'isLoop' => $this->isLoop,
'retry' => $this->retry
];
}
/**
* @param array $data
* @return void
*/
public function __unserialize(array $data): void
{
$this->name = $data['name'];
$this->params = $data['params'];
$this->tickTime = $data['tickTime'];
$this->isLoop = $data['isLoop'];
$this->retry = $data['retry'];
}
/**
* @param $name
* @return mixed
*/
#[Pure] public function __get($name): mixed
{
if ($name === 'application') {
return $this->getApplication();
}
if (!isset($this->params[$name])) {
return null;
}
return $this->params[$name];
}
/**
* @throws Exception
*/
public function process(): void
{
try {
$redis = Kiri::getDi()->get(Redis::class);
$name_md5 = $this->getName();
$redis->hSet(Crontab::WAIT_END, $name_md5, serialize($this));
call_user_func([$this, 'execute']);
$redis->hDel(self::WAIT_END, $name_md5);
} catch (\Throwable $throwable) {
$logger = Kiri::getDi()->get(LoggerInterface::class);
$logger->error('crontab execute fail.[' . $throwable->getMessage() . ']', [error_trigger_format($throwable)]);
if (Context::increment('retry.number') >= $this->retry) {
return;
}
$this->process();
} finally {
$this->onRecover($this->getName());
}
}
/**
* @param $name_md5
* @return bool|int
* @throws ReflectionException
* @throws Exception
*/
protected function onRecover($name_md5): bool|int
{
$redis = Kiri::getDi()->get(Redis::class);
$crontab = Kiri::getDi()->get(Producer::class);
if ($redis->sIsMember(Producer::CRONTAB_STOP_KEY, $name_md5)) {
return $redis->sRem(Producer::CRONTAB_STOP_KEY, $name_md5);
}
if ($this->isPropagationStopped()) {
return true;
}
return $crontab->task($this);
}
/**
* @return int
*/
private function next(): int
{
return time() + $this->getTickTime();
}
/**
* @param $class
* @return string
*/
public static function serialize($class): string
{
return serialize($class);
}
}
PHP
1
https://gitee.com/dreamwithouttrace/kiri-crontab.git
git@gitee.com:dreamwithouttrace/kiri-crontab.git
dreamwithouttrace
kiri-crontab
kiri-crontab
master

搜索帮助