Files
kiri-core/System/Process/CrontabProcess.php
T

126 lines
3.0 KiB
PHP
Raw Normal View History

2021-03-19 17:47:41 +08:00
<?php
namespace Snowflake\Process;
2021-03-22 00:48:35 +08:00
use Exception;
2021-03-23 10:41:18 +08:00
use ReflectionException;
2021-03-19 18:52:30 +08:00
use Snowflake\Crontab;
2021-03-22 00:16:40 +08:00
use Snowflake\Abstracts\Crontab as ACrontab;
2021-03-22 00:19:06 +08:00
use Snowflake\Event;
2021-03-23 10:41:18 +08:00
use Snowflake\Exception\ComponentException;
use Snowflake\Exception\NotFindClassException;
2021-03-22 00:16:40 +08:00
use Snowflake\Snowflake;
2021-03-19 17:47:41 +08:00
use Swoole\Coroutine;
2021-03-20 03:25:30 +08:00
use Swoole\Coroutine\WaitGroup;
2021-03-19 17:47:41 +08:00
use Swoole\Coroutine\Channel;
use Swoole\Timer;
/**
* Class CrontabProcess
* @package Snowflake\Process
*/
class CrontabProcess extends Process
{
2021-03-20 03:15:58 +08:00
private Channel $channel;
2021-03-20 03:25:30 +08:00
private WaitGroup $waitGroup;
2021-03-20 03:15:58 +08:00
2021-03-20 02:33:50 +08:00
/** @var Crontab[] $names */
public array $names = [];
2021-03-21 23:53:48 +08:00
public array $scores = [];
public array $timers = [];
2021-03-23 10:41:18 +08:00
/**
* @param \Swoole\Process $process
* @throws ReflectionException
* @throws ComponentException
* @throws NotFindClassException
*/
2021-03-20 02:33:50 +08:00
public function onHandler(\Swoole\Process $process): void
2021-03-20 03:15:58 +08:00
{
2021-03-22 00:48:35 +08:00
$crontab = Snowflake::app()->get('crontab');
$crontab->clearAll();
2021-03-22 00:25:35 +08:00
2021-03-21 23:53:48 +08:00
Timer::tick(1000, function () {
$startTime = time();
2021-03-22 00:16:40 +08:00
$redis = Snowflake::app()->getRedis();
2021-03-22 00:01:12 +08:00
2021-03-22 00:17:27 +08:00
$range = $redis->zRangeByScore(ACrontab::CRONTAB_KEY, '0', (string)$startTime);
2021-03-22 00:18:11 +08:00
$redis->zRemRangeByScore(ACrontab::CRONTAB_KEY, '0', (string)$startTime);
2021-03-22 00:16:40 +08:00
foreach ($range as $value) {
$crontab = $redis->get('crontab:' . md5($value));
2021-03-22 00:48:35 +08:00
$redis->del('crontab:' . md5($value));
2021-03-24 17:57:08 +08:00
if (empty($crontab) || !($crontab = swoole_unserialize($crontab))) {
2021-03-22 00:16:40 +08:00
continue;
}
2021-03-21 23:53:48 +08:00
Coroutine::create(function (Crontab $value, int $startTime) {
2021-03-22 00:16:40 +08:00
$this->dispatch($value);
}, $crontab, $startTime);
2021-03-21 23:53:48 +08:00
}
2021-03-22 00:19:06 +08:00
$redis->release();
2021-03-21 23:53:48 +08:00
});
}
2021-03-20 03:15:58 +08:00
2021-03-23 10:41:18 +08:00
/**
* @param Crontab $value
* @throws Exception
*/
2021-03-22 00:16:40 +08:00
private function dispatch(Crontab $value)
2021-03-21 23:53:48 +08:00
{
2021-03-22 00:55:19 +08:00
$value->increment()->execute();
if ($value->getExecuteNumber() < $value->getMaxExecuteNumber()) {
$this->addTask($value);
} else if ($value->isLoop()) {
$this->addTask($value);
2021-03-20 02:33:50 +08:00
}
2021-03-22 00:55:19 +08:00
fire(Event::SYSTEM_RESOURCE_RELEASES);
2021-03-20 02:33:50 +08:00
}
2021-03-20 02:56:21 +08:00
/**
* @param string $name
*/
public function clear(string $name)
{
if (!isset($this->names[$name])) {
return;
}
2021-03-21 23:53:48 +08:00
$timers = $this->timers[$name];
$search = array_search($name, $this->scores[$timers]);
if ($search !== false) {
unset($this->scores[$timers][$search]);
}
unset($this->timers[$name], $this->names[$name]);
2021-03-20 02:56:21 +08:00
}
2021-03-23 10:41:18 +08:00
/**
* @param Crontab $crontab
* @throws Exception
*/
2021-03-22 00:16:40 +08:00
private function addTask(Crontab $crontab)
2021-03-21 23:53:48 +08:00
{
2021-03-22 00:16:40 +08:00
$redis = Snowflake::app()->getRedis();
2021-03-21 23:53:48 +08:00
2021-03-22 00:16:40 +08:00
$name = md5($crontab->getName());
2021-03-21 23:53:48 +08:00
2021-03-24 17:57:08 +08:00
$redis->set('crontab:' . $name, swoole_serialize($crontab));
2021-03-21 23:53:48 +08:00
2021-03-22 00:16:40 +08:00
$tickTime = time() + $crontab->getTickTime();
2021-03-21 23:53:48 +08:00
2021-03-22 00:16:40 +08:00
$redis->zAdd(ACrontab::CRONTAB_KEY, $tickTime, $crontab->getName());
2021-03-20 02:33:50 +08:00
}
2021-03-19 17:47:41 +08:00
}