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

179 lines
4.3 KiB
PHP
Raw Normal View History

2021-03-19 17:47:41 +08:00
<?php
namespace Snowflake\Process;
2021-03-19 18:52:30 +08:00
use Snowflake\Crontab;
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-20 02:33:50 +08:00
/**
* @param \Swoole\Process $process
*/
public function onHandler(\Swoole\Process $process): void
2021-03-20 03:15:58 +08:00
{
2021-03-21 23:53:48 +08:00
$this->readBySocket($process);
Timer::tick(1000, function () {
$startTime = time();
2021-03-22 00:01:12 +08:00
var_dump($startTime);
2021-03-21 23:53:48 +08:00
$time = $this->scores[$startTime];
unset($this->scores[$startTime]);
foreach ($time as $value) {
Coroutine::create(function (Crontab $value, int $startTime) {
$this->dispatch($value, $startTime);
}, $value, $startTime);
}
});
}
2021-03-20 03:15:58 +08:00
2021-03-21 23:53:48 +08:00
/**
* @param Crontab $value
* @param int $startTime
* @throws \Exception
*/
private function dispatch(Crontab $value, int $startTime)
{
try {
$value->increment()->execute();
if ($value->getExecuteNumber() < $value->getMaxExecuteNumber()) {
$this->addTask($value, $startTime + $value->getTickTime());
} else if ($value->isLoop()) {
$this->addTask($value, $startTime + $value->getTickTime());
2021-03-20 02:33:50 +08:00
}
2021-03-20 03:15:58 +08:00
} catch (\Throwable $exception) {
$this->application->error($exception->getMessage());
2021-03-20 02:33:50 +08:00
}
}
2021-03-21 23:53:48 +08:00
/**
* @param \Swoole\Process $process
* @throws \Exception
*/
public function readBySocket(\Swoole\Process $process)
{
Coroutine::create(function (\Swoole\Process $process) {
try {
$content = $process->read();
$_content = json_decode($content, true);
if (is_null($_content)) {
$this->jobDelivery($content);
} else {
$this->otherAction($_content);
}
} catch (\Throwable $exception) {
$this->application->error($exception->getMessage());
} finally {
$this->onHandler($process);
}
}, $process);
}
2021-03-20 02:33:50 +08:00
/**
* @param $content
*/
private function otherAction($content)
{
call_user_func(match ($content['action']) {
'clear' => function ($content) {
2021-03-20 02:56:21 +08:00
$this->clear($content['name']);
2021-03-20 02:33:50 +08:00
},
'clearAll' => function () {
2021-03-20 03:51:07 +08:00
$this->names = [];
Timer::clearAll();
2021-03-20 02:33:50 +08:00
},
default => function () {
$this->application->error('unknown action');
}
2021-03-20 02:44:30 +08:00
}, $content);
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-20 02:33:50 +08:00
/**
* @param $content
*/
private function jobDelivery($content)
{
2021-03-20 02:51:37 +08:00
/** @var Crontab $content */
2021-03-20 02:33:50 +08:00
$content = unserialize($content);
2021-03-20 03:54:21 +08:00
2021-03-21 23:53:48 +08:00
$ticker = intval($content->getTickTime() * 1000) + time();
$this->addTask($content, $ticker);
}
/**
* @param Crontab $content
* @param $ticker
*/
private function addTask(Crontab $content, $ticker)
{
2021-03-20 03:54:21 +08:00
$name = $content->getName();
2021-03-20 03:57:24 +08:00
if (isset($this->names[$name])) {
2021-03-21 23:53:48 +08:00
unset($this->names[$content->getName()]);
$search = array_search($content->getName(), $this->scores);
unset($this->scores[$search]);
2021-03-20 03:56:51 +08:00
}
2021-03-21 23:53:48 +08:00
if (!isset($this->scores[$ticker])) {
$this->scores[$ticker] = [];
2021-03-20 03:39:11 +08:00
}
2021-03-21 23:53:48 +08:00
$this->timers[$content->getName()] = $ticker;
$this->scores[$ticker][] = $content->getName();
$this->names[$content->getName()] = $content;
ksort($this->scores, SORT_NUMERIC);
2021-03-20 02:33:50 +08:00
}
2021-03-19 17:47:41 +08:00
}