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 = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param \Swoole\Process $process
|
|
|
|
|
*/
|
|
|
|
|
public function onHandler(\Swoole\Process $process): void
|
|
|
|
|
{
|
2021-03-20 03:15:58 +08:00
|
|
|
$this->channel = new Channel(1000);
|
2021-03-20 03:25:30 +08:00
|
|
|
Coroutine\go(function () {
|
|
|
|
|
$this->waitGroup();
|
2021-03-20 03:15:58 +08:00
|
|
|
});
|
2021-03-20 03:35:44 +08:00
|
|
|
$this->readByWorker($process);
|
2021-03-20 03:15:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-03-20 03:35:44 +08:00
|
|
|
/**
|
|
|
|
|
* @param $process
|
|
|
|
|
*/
|
|
|
|
|
private function readByWorker($process)
|
|
|
|
|
{
|
|
|
|
|
$this->channel->push($process->read());
|
|
|
|
|
|
|
|
|
|
Coroutine::sleep(0.01);
|
|
|
|
|
|
|
|
|
|
$this->readByWorker($process);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @throws \Exception
|
|
|
|
|
*/
|
2021-03-20 03:25:30 +08:00
|
|
|
private function waitGroup()
|
2021-03-20 03:15:58 +08:00
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$content = $this->channel->pop(-1);
|
|
|
|
|
|
|
|
|
|
$_content = json_decode($content, true);
|
|
|
|
|
if (is_null($_content)) {
|
|
|
|
|
$this->jobDelivery($content);
|
|
|
|
|
} else {
|
|
|
|
|
$this->otherAction($_content);
|
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 03:31:04 +08:00
|
|
|
} finally {
|
|
|
|
|
$this->waitGroup();
|
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 () {
|
|
|
|
|
foreach ($this->names as $name => $crontab) {
|
|
|
|
|
$crontab->clearTimer();
|
2021-03-20 02:56:21 +08:00
|
|
|
|
|
|
|
|
unset($this->names[$name], $crontab);
|
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;
|
|
|
|
|
}
|
|
|
|
|
$this->names[$name]->clearTimer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
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:22:27 +08:00
|
|
|
// $runTicker = function () use ($content) {
|
|
|
|
|
// $this->application->warning('execute crontab ' . date('Y-m-d H:i:s'));
|
|
|
|
|
// $content->execute($this);
|
|
|
|
|
// };
|
2021-03-20 03:13:36 +08:00
|
|
|
// $timer = $content->getTickTime() * 10;
|
|
|
|
|
|
2021-03-20 03:20:35 +08:00
|
|
|
|
2021-03-20 03:22:27 +08:00
|
|
|
var_dump(serialize($content));
|
|
|
|
|
|
|
|
|
|
Timer::after(3000, function () use ($content) {
|
2021-03-20 03:37:24 +08:00
|
|
|
$this->application->warning('execute crontab ' . date('Y-m-d H:i:s'));
|
2021-03-20 03:32:11 +08:00
|
|
|
// $content->execute($this);
|
2021-03-20 03:13:36 +08:00
|
|
|
});
|
|
|
|
|
|
2021-03-20 03:29:20 +08:00
|
|
|
var_dump(Timer::stats());
|
|
|
|
|
|
2021-03-20 03:13:36 +08:00
|
|
|
// if ($content->isLoop()) {
|
|
|
|
|
// $content->setTimerId(Timer::tick($timer, function () use ($content) {
|
|
|
|
|
// $this->application->warning('execute crontab ' . date('Y-m-d H:i:s'));
|
|
|
|
|
// $content->execute($this);
|
|
|
|
|
// }));
|
|
|
|
|
// } else {
|
|
|
|
|
// $content->setTimerId(Timer::after($timer, function () use ($content) {
|
|
|
|
|
// $this->application->warning('execute crontab ' . date('Y-m-d H:i:s'));
|
|
|
|
|
// $content->execute($this);
|
|
|
|
|
// }));
|
|
|
|
|
// }
|
|
|
|
|
// $this->names[$content->getName()] = $content;
|
2021-03-20 02:33:50 +08:00
|
|
|
}
|
2021-03-19 17:47:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|