This commit is contained in:
as2252258@163.com
2021-03-20 02:33:50 +08:00
parent 25a09a379a
commit 06422d5073
3 changed files with 253 additions and 231 deletions
+12 -21
View File
@@ -4,6 +4,8 @@
namespace Snowflake\Abstracts; namespace Snowflake\Abstracts;
use Snowflake\Core\Json;
use Snowflake\Process\CrontabProcess;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use Exception; use Exception;
@@ -21,11 +23,11 @@ class Crontab extends Component
* @param $executeTime * @param $executeTime
* @throws Exception * @throws Exception
*/ */
public function dispatch(\Snowflake\Crontab $crontab, int $executeTime) public function dispatch(\Snowflake\Crontab $crontab)
{ {
$redis = Snowflake::app()->getRedis(); /** @var CrontabProcess $redis */
$redis = Snowflake::app()->get(CrontabProcess::class);
$redis->zAdd('system:crontab', (string)$executeTime, serialize($crontab)); $redis->write(serialize($crontab));
} }
@@ -35,20 +37,9 @@ class Crontab extends Component
*/ */
public function clear(string $name) public function clear(string $name)
{ {
$redis = Snowflake::app()->getRedis(); /** @var CrontabProcess $redis */
$redis = Snowflake::app()->get(CrontabProcess::class);
$data = $redis->zRevRange('system:crontab', 0, -1); $redis->write(Json::encode(['action' => 'clear', 'name' => $name]));
if (empty($data)) {
return;
}
foreach ($data as $datum) {
/** @var \Snowflake\Crontab $crontab */
$crontab = unserialize($datum);
if ($crontab->getName() == $name) {
$redis->zRem('system:crontab', $datum);
}
}
} }
@@ -57,9 +48,9 @@ class Crontab extends Component
*/ */
public function clearAll() public function clearAll()
{ {
$redis = Snowflake::app()->getRedis(); /** @var CrontabProcess $redis */
$redis = Snowflake::app()->get(CrontabProcess::class);
$redis->del('system:crontab'); $redis->write(Json::encode(['action' => 'clearAll']));
} }
+37 -10
View File
@@ -7,6 +7,7 @@ namespace Snowflake;
use Closure; use Closure;
use Exception; use Exception;
use Snowflake\Abstracts\BaseObject; use Snowflake\Abstracts\BaseObject;
use Swoole\Timer;
/** /**
* Class Async * Class Async
@@ -31,6 +32,9 @@ class Crontab extends BaseObject
private bool $isLoop = false; private bool $isLoop = false;
private int $timerId = -1;
private int $max_execute_number = -1; private int $max_execute_number = -1;
@@ -149,28 +153,51 @@ class Crontab extends BaseObject
$this->execute_number = $execute_number; $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;
}
/**
*
*/
public function clearTimer()
{
$this->warning('crontab timer clear.');
if (Timer::exists($this->timerId)) {
Timer::clear($this->timerId);
}
}
/** /**
* @throws Exception * @throws Exception
*/ */
public function execute(): void public function execute(): void
{ {
$redis = Snowflake::app()->getRedis();
try { try {
$this->execute_number += 1;
call_user_func($this->handler, $this->params); call_user_func($this->handler, $this->params);
if ($this->isLoop === false) { $this->execute_number += 1;
return;
} if ($this->execute_number < $this->max_execute_number) {
if ($this->max_execute_number === -1) { $this->clearTimer();
$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) { } catch (\Throwable $throwable) {
$this->addError($throwable->getMessage()); $this->addError($throwable->getMessage());
} finally { } finally {
$redis->release(); fire(Event::SYSTEM_RESOURCE_RELEASES);
} }
} }
+46 -42
View File
@@ -5,6 +5,7 @@ namespace Snowflake\Process;
use ReflectionException; use ReflectionException;
use Snowflake\Core\Json;
use Snowflake\Crontab; use Snowflake\Crontab;
use Snowflake\Exception\ComponentException; use Snowflake\Exception\ComponentException;
use Snowflake\Exception\ConfigException; use Snowflake\Exception\ConfigException;
@@ -23,67 +24,70 @@ use Swoole\Timer;
class CrontabProcess extends Process class CrontabProcess extends Process
{ {
/** @var Crontab[] $names */
public Channel $channel; public array $names = [];
/** /**
* @param \Swoole\Process $process * @param \Swoole\Process $process
*/ */
public function onHandler(\Swoole\Process $process): void public function onHandler(\Swoole\Process $process): void
{
$this->channel = new Channel(5000);
Coroutine::create([$this, 'execute']);
Timer::tick(1000, [$this, 'systemLoop']);
}
/**
* @throws \Exception
*/
public function execute()
{ {
while (true) { while (true) {
/** @var Crontab $list */ $content = $process->read();
$list = $this->channel->pop(-1); $_content = json_decode($content, true);
$list->execute(); if (is_null($_content)) {
$this->jobDelivery($content);
} else {
$this->otherAction($content);
}
} }
} }
/** /**
* @throws ReflectionException * @param $content
* @throws ComponentException
* @throws ConfigException
* @throws NotFindClassException
* @throws Exception
* @throws \Exception
*/ */
public function systemLoop() private function otherAction($content)
{ {
$score = time(); call_user_func(match ($content['action']) {
$redis = Snowflake::app()->getRedis(); 'clear' => function ($content) {
if (!isset($this->names[$content['name']])) {
$lists = $redis->zRangeByScore('system:crontab', '0', (string)$score);
$redis->zRemRangeByScore('system:crontab', '0', (string)$score);
if (empty($lists)) {
$redis->release();
return; return;
} }
$this->names[$content['name']]->clearTimer();
$barrier = Barrier::make(); },
foreach ($lists as $list) { 'clearAll' => function () {
$list = unserialize($list); foreach ($this->names as $name => $crontab) {
if (!($list instanceof Crontab)) { $crontab->clearTimer();
continue;
} }
$this->channel->push($list); },
default => function () {
$this->application->error('unknown action');
} }
Barrier::wait($barrier); });
$redis->release();
} }
/**
* @param $content
*/
private function jobDelivery($content)
{
$content = unserialize($content);
$this->names[$content->getName()] = $content;
if (!($content instanceof Crontab)) {
return;
}
$runTicker = [$content, 'execute'];
$timer = $content->getTickTime() * 1000;
if ($content->isLoop()) {
$timerId = Timer::tick($timer, $runTicker);
} else {
$timerId = Timer::after($timer, $runTicker);
}
$content->setTimerId($timerId);
}
} }