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

94 lines
2.2 KiB
PHP
Raw Normal View History

2021-03-19 17:47:41 +08:00
<?php
namespace Snowflake\Process;
use ReflectionException;
2021-03-20 02:33:50 +08:00
use Snowflake\Core\Json;
2021-03-19 18:52:30 +08:00
use Snowflake\Crontab;
2021-03-19 17:47:41 +08:00
use Snowflake\Exception\ComponentException;
use Snowflake\Exception\ConfigException;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
use Swoole\Coroutine;
use Swoole\Coroutine\Barrier;
use Swoole\Coroutine\Channel;
use Swoole\Exception;
use Swoole\Timer;
/**
* Class CrontabProcess
* @package Snowflake\Process
*/
class CrontabProcess extends Process
{
2021-03-20 02:33:50 +08:00
/** @var Crontab[] $names */
public array $names = [];
/**
* @param \Swoole\Process $process
*/
public function onHandler(\Swoole\Process $process): void
{
while (true) {
$content = $process->read();
$_content = json_decode($content, true);
if (is_null($_content)) {
$this->jobDelivery($content);
} else {
$this->otherAction($content);
}
}
}
/**
* @param $content
*/
private function otherAction($content)
{
call_user_func(match ($content['action']) {
'clear' => function ($content) {
if (!isset($this->names[$content['name']])) {
return;
}
$this->names[$content['name']]->clearTimer();
},
'clearAll' => function () {
foreach ($this->names as $name => $crontab) {
$crontab->clearTimer();
}
},
default => function () {
$this->application->error('unknown action');
}
});
}
/**
* @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);
}
2021-03-19 17:47:41 +08:00
}