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

256 lines
4.5 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 Closure;
use Exception;
2021-03-19 19:16:12 +08:00
use Snowflake\Abstracts\BaseObject;
2021-03-26 01:01:21 +08:00
use Snowflake\Event;
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-03-19 19:16:12 +08:00
class Crontab extends BaseObject
2021-03-19 17:47:41 +08:00
{
2021-04-12 02:55:12 +08:00
const WAIT_END = 'crontab:wait:execute';
2021-03-19 17:47:41 +08:00
2021-03-20 02:33:50 +08:00
private array|Closure $handler;
2021-03-19 18:52:30 +08:00
2021-03-20 02:33:50 +08:00
private string $name = '';
2021-03-19 18:52:30 +08:00
2021-03-26 01:53:26 +08:00
private mixed $params = null;
2021-03-20 02:33:50 +08:00
private int $tickTime = 1;
private bool $isLoop = false;
private int $timerId = -1;
private int $max_execute_number = -1;
private int $execute_number = 0;
2021-03-21 23:53:48 +08:00
/**
* @return $this
*/
public function increment(): static
{
$this->execute_number += 1;
return $this;
}
2021-03-20 02:33:50 +08:00
/**
* @return array|Closure
*/
public function getHandler(): array|Closure
{
return $this->handler;
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @return mixed
*/
public function getParams(): mixed
{
return $this->params;
}
/**
* @return int
*/
public function getTickTime(): int
{
2021-03-22 00:28:27 +08:00
return $this->tickTime;
2021-03-20 02:33:50 +08:00
}
/**
* @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 array|Closure $handler
*/
public function setHandler(array|Closure $handler): void
{
$this->handler = $handler;
}
/**
* @param string $name
*/
public function setName(string $name): void
{
$this->name = $name;
}
/**
* @param mixed $params
*/
public function setParams(mixed $params): void
{
$this->params = $params;
}
/**
* @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;
}
2021-03-26 01:47:24 +08:00
/**
*
* @throws Exception
*/
2021-03-20 02:33:50 +08:00
public function clearTimer()
{
$this->warning('crontab timer clear.');
if (Timer::exists($this->timerId)) {
Timer::clear($this->timerId);
}
}
2021-04-12 02:55:12 +08:00
/**
* @throws \Exception
*/
private function recover()
{
$redis = Snowflake::app()->getRedis();
2021-04-12 03:04:42 +08:00
$redis->set('crontab:' . ($name = md5($this->getName())), swoole_serialize($this));
2021-04-12 02:55:12 +08:00
$tickTime = time() + $this->getTickTime();
2021-04-12 03:04:42 +08:00
$redis->zAdd(Producer::CRONTAB_KEY, $tickTime, $name);
2021-04-12 02:55:12 +08:00
}
2021-03-20 02:33:50 +08:00
/**
* @throws Exception
*/
2021-03-21 23:53:48 +08:00
public function execute(): void
2021-03-20 02:33:50 +08:00
{
2021-04-12 02:55:12 +08:00
try {
$redis = Snowflake::app()->getRedis();
$name_md5 = md5($this->getName());
2021-04-12 03:06:42 +08:00
var_dump($name_md5);
2021-04-12 02:55:12 +08:00
$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);
if ($params === null) {
return;
}
2021-03-26 02:20:17 +08:00
$name = date('Y_m_d_H_i_s.' . $this->name . '.log');
write(storage($name, '/log/crontab'), serialize($params));
2021-04-12 02:55:12 +08:00
} catch (\Throwable $throwable) {
logger()->addError($throwable, 'throwable');
} finally {
$this->after();
}
}
/**
* @throws \Exception
*/
private function after(): void
{
if ($this->getExecuteNumber() < $this->getMaxExecuteNumber()) {
$this->recover();
} else if ($this->isLoop()) {
$this->recover();
2021-03-26 02:20:17 +08:00
}
2021-03-20 02:33:50 +08:00
}
2021-03-19 18:52:30 +08:00
2021-03-19 17:47:41 +08:00
}