Files
kiri-core/System/Crontab.php
T

179 lines
2.6 KiB
PHP
Raw Normal View History

2021-03-19 17:47:41 +08:00
<?php
namespace Snowflake;
use Closure;
use Exception;
2021-03-19 19:16:12 +08:00
use Snowflake\Abstracts\BaseObject;
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-03-19 19:06:47 +08:00
private array|Closure $handler;
2021-03-19 18:52:30 +08:00
2021-03-19 19:06:47 +08:00
private string $name = '';
2021-03-19 18:52:30 +08:00
2021-03-19 19:06:47 +08:00
private mixed $params;
2021-03-19 18:52:30 +08:00
2021-03-19 19:06:47 +08:00
private int $tickTime = 1;
2021-03-19 18:52:30 +08:00
2021-03-19 19:06:47 +08:00
private bool $isLoop = false;
2021-03-19 18:52:30 +08:00
2021-03-19 19:06:47 +08:00
private int $max_execute_number = -1;
private int $execute_number = 0;
2021-03-19 18:52:30 +08:00
2021-03-19 19:16:12 +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
{
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;
}
2021-03-19 18:52:30 +08:00
2021-03-19 17:47:41 +08:00
/**
* @param array|Closure $handler
2021-03-19 19:06:47 +08:00
*/
2021-03-19 19:07:42 +08:00
public function setHandler(array|Closure $handler): void
2021-03-19 19:06:47 +08:00
{
$this->handler = $handler;
}
/**
* @param string $name
*/
2021-03-19 19:07:42 +08:00
public function setName(string $name): void
2021-03-19 19:06:47 +08:00
{
$this->name = $name;
}
/**
2021-03-19 18:11:42 +08:00
* @param mixed $params
2021-03-19 19:06:47 +08:00
*/
2021-03-19 19:07:42 +08:00
public function setParams(mixed $params): void
2021-03-19 19:06:47 +08:00
{
$this->params = $params;
}
/**
2021-03-19 17:47:41 +08:00
* @param int $tickTime
2021-03-19 19:06:47 +08:00
*/
2021-03-19 19:07:42 +08:00
public function setTickTime(int $tickTime): void
2021-03-19 19:06:47 +08:00
{
$this->tickTime = $tickTime;
}
/**
2021-03-19 17:47:41 +08:00
* @param bool $isLoop
2021-03-19 19:06:47 +08:00
*/
2021-03-19 19:07:42 +08:00
public function setIsLoop(bool $isLoop): void
2021-03-19 19:06:47 +08:00
{
$this->isLoop = $isLoop;
}
/**
2021-03-19 18:52:30 +08:00
* @param int $max_execute_number
2021-03-19 17:47:41 +08:00
*/
2021-03-19 19:07:42 +08:00
public function setMaxExecuteNumber(int $max_execute_number): void
2021-03-19 17:47:41 +08:00
{
2021-03-19 19:06:47 +08:00
$this->max_execute_number = $max_execute_number;
}
/**
* @param int $execute_number
*/
2021-03-19 19:07:42 +08:00
public function setExecuteNumber(int $execute_number): void
2021-03-19 19:06:47 +08:00
{
$this->execute_number = $execute_number;
}
2021-03-19 17:47:41 +08:00
2021-03-19 18:52:30 +08:00
/**
* @throws Exception
*/
public function execute(): void
{
$redis = Snowflake::app()->getRedis();
try {
$this->execute_number += 1;
2021-03-19 19:20:50 +08:00
call_user_func($this->handler, $this->params);
2021-03-19 18:52:30 +08:00
if ($this->isLoop === false) {
return;
}
if ($this->max_execute_number === -1) {
$redis->zAdd('system:crontab', time() + $this->tickTime, serialize($this));
} else if ($this->execute_number < $this->max_execute_number) {
$redis->zAdd('system:crontab', time() + $this->tickTime, serialize($this));
}
} catch (\Throwable $throwable) {
$this->addError($throwable->getMessage());
} finally {
$redis->release();
}
}
2021-03-19 17:47:41 +08:00
}