改名
This commit is contained in:
+52
-2
@@ -16,14 +16,33 @@ class Crontab extends Component
|
|||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
|
public array|Closure $handler;
|
||||||
|
|
||||||
|
|
||||||
|
public mixed $params;
|
||||||
|
|
||||||
|
|
||||||
|
public int $tickTime = 1;
|
||||||
|
|
||||||
|
|
||||||
|
public bool $isLoop = false;
|
||||||
|
|
||||||
|
|
||||||
|
public int $max_execute_number = -1;
|
||||||
|
|
||||||
|
|
||||||
|
public int $execute_number = 0;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array|Closure $handler
|
* @param array|Closure $handler
|
||||||
* @param mixed $params
|
* @param mixed $params
|
||||||
* @param int $tickTime
|
* @param int $tickTime
|
||||||
* @param bool $isLoop
|
* @param bool $isLoop
|
||||||
|
* @param int $max_execute_number
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function dispatch(array|Closure $handler, mixed $params = null, $tickTime = 1, $isLoop = false)
|
public function dispatch(array|Closure $handler, mixed $params = null, $tickTime = 1, bool $isLoop = false, $max_execute_number = -1)
|
||||||
{
|
{
|
||||||
$redis = Snowflake::app()->getRedis();
|
$redis = Snowflake::app()->getRedis();
|
||||||
|
|
||||||
@@ -33,9 +52,40 @@ class Crontab extends Component
|
|||||||
|
|
||||||
$executeTime = time() + $tickTime;
|
$executeTime = time() + $tickTime;
|
||||||
|
|
||||||
$crontab = ['isLoop' => $isLoop, 'handler' => $handler, 'tick' => $tickTime, 'params' => $params];
|
$crontab = new Crontab();
|
||||||
|
$crontab->max_execute_number = $max_execute_number;
|
||||||
|
$crontab->handler = $handler;
|
||||||
|
$crontab->params = $params;
|
||||||
|
$crontab->tickTime = $tickTime;
|
||||||
|
$crontab->isLoop = $isLoop;
|
||||||
|
|
||||||
$redis->zAdd('system:crontab', (string)$executeTime, serialize($crontab));
|
$redis->zAdd('system:crontab', (string)$executeTime, serialize($crontab));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function execute(): void
|
||||||
|
{
|
||||||
|
$redis = Snowflake::app()->getRedis();
|
||||||
|
try {
|
||||||
|
$this->execute_number += 1;
|
||||||
|
call_user_func($this->handler, $list['params'] ?? null);
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ namespace Snowflake\Process;
|
|||||||
|
|
||||||
|
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
|
use Snowflake\Crontab;
|
||||||
use Snowflake\Exception\ComponentException;
|
use Snowflake\Exception\ComponentException;
|
||||||
use Snowflake\Exception\ConfigException;
|
use Snowflake\Exception\ConfigException;
|
||||||
use Snowflake\Exception\NotFindClassException;
|
use Snowflake\Exception\NotFindClassException;
|
||||||
@@ -44,18 +45,10 @@ class CrontabProcess extends Process
|
|||||||
*/
|
*/
|
||||||
public function execute()
|
public function execute()
|
||||||
{
|
{
|
||||||
$redis = Snowflake::app()->getRedis();
|
|
||||||
while (true) {
|
while (true) {
|
||||||
|
/** @var Crontab $list */
|
||||||
$list = $this->channel->pop(-1);
|
$list = $this->channel->pop(-1);
|
||||||
if (isset($list['isLoop']) && isset($list['tick']) && $list['isLoop'] == 1) {
|
$list->execute();
|
||||||
$redis->zAdd('system:crontab', time() + $list['tick'], serialize($list));
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
call_user_func($list['handler'], $list['params'] ?? null);
|
|
||||||
} catch (\Throwable $throwable) {
|
|
||||||
$this->application->addError($throwable->getMessage());
|
|
||||||
}
|
|
||||||
$redis->release();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,7 +72,7 @@ class CrontabProcess extends Process
|
|||||||
$barrier = Barrier::make();
|
$barrier = Barrier::make();
|
||||||
foreach ($lists as $list) {
|
foreach ($lists as $list) {
|
||||||
$list = unserialize($list);
|
$list = unserialize($list);
|
||||||
if (!isset($list['handler']) || !is_callable($list['handler'], true)) {
|
if (!($list instanceof Crontab)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$this->channel->push($list);
|
$this->channel->push($list);
|
||||||
|
|||||||
Reference in New Issue
Block a user