Files
kiri-core/System/Process/CrontabProcess.php
T
as2252258@163.com 8a56598d73 modify
2021-03-20 02:46:29 +08:00

97 lines
2.3 KiB
PHP

<?php
namespace Snowflake\Process;
use ReflectionException;
use Snowflake\Core\Json;
use Snowflake\Crontab;
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
{
/** @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);
var_dump($content, $_content);
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');
}
}, $content);
}
/**
* @param $content
*/
private function jobDelivery($content)
{
$content = unserialize($content);
$this->names[$content->getName()] = $content;
if (!($content instanceof Crontab)) {
return;
}
$runTicker = function (Crontab $crontab) {
$crontab->execute();
};
$timer = $content->getTickTime() * 1000;
if ($content->isLoop()) {
$timerId = Timer::tick($timer, $runTicker, $content);
} else {
$timerId = Timer::after($timer, $runTicker, $content);
}
$content->setTimerId($timerId);
}
}