Files
kiri-core/System/Crontab.php
T

42 lines
794 B
PHP
Raw Normal View History

2021-03-19 17:47:41 +08:00
<?php
namespace Snowflake;
use Closure;
use Exception;
use Snowflake\Abstracts\Component;
/**
* Class Async
* @package Snowflake
*/
class Crontab extends Component
{
/**
* @param array|Closure $handler
2021-03-19 18:11:42 +08:00
* @param mixed $params
2021-03-19 17:47:41 +08:00
* @param int $tickTime
* @param bool $isLoop
* @throws Exception
*/
2021-03-19 18:11:42 +08:00
public function dispatch(array|Closure $handler, mixed $params = null, $tickTime = 1, $isLoop = false)
2021-03-19 17:47:41 +08:00
{
$redis = Snowflake::app()->getRedis();
if (is_array($handler) && is_string($handler[0])) {
$handler[0] = Snowflake::createObject($handler[0]);
}
$executeTime = time() + $tickTime;
2021-03-19 18:11:42 +08:00
$crontab = ['isLoop' => $isLoop, 'handler' => $handler, 'tick' => $tickTime, 'params' => $params];
2021-03-19 17:47:41 +08:00
2021-03-19 18:11:42 +08:00
$redis->zAdd('system:crontab', $executeTime, serialize($crontab));
2021-03-19 17:47:41 +08:00
}
}