This commit is contained in:
2021-03-29 10:39:36 +08:00
parent 279da38381
commit 38748d1f8c
+70 -67
View File
@@ -18,90 +18,93 @@ use Swoole\Coroutine;
class Consumer extends Process class Consumer extends Process
{ {
public Coroutine\Channel $channel; public Coroutine\Channel $channel;
/** /**
* @param \Swoole\Process $process * @param \Swoole\Process $process
*/ * @throws Exception
public function onHandler(\Swoole\Process $process): void */
{ public function onHandler(\Swoole\Process $process): void
$process->name('Crontab consumer'); {
if (Snowflake::getPlatform()->isLinux()) {
$process->name('Crontab consumer');
}
$this->channel = new Coroutine\Channel(2000); $this->channel = new Coroutine\Channel(2000);
go(function () { go(function () {
$this->popChannel(); $this->popChannel();
}); });
$this->tick($process); $this->tick($process);
} }
/** /**
* @throws Exception * @throws Exception
*/ */
public function popChannel() public function popChannel()
{ {
/** @var Crontab $crontab */ /** @var Crontab $crontab */
$crontab = $this->channel->pop(-1); $crontab = $this->channel->pop(-1);
go(function () use ($crontab) { go(function () use ($crontab) {
try { try {
$crontab->increment()->execute(); $crontab->increment()->execute();
if ($crontab->getExecuteNumber() < $crontab->getMaxExecuteNumber()) { if ($crontab->getExecuteNumber() < $crontab->getMaxExecuteNumber()) {
Consumer::addTask($crontab); Consumer::addTask($crontab);
} else if ($crontab->isLoop()) { } else if ($crontab->isLoop()) {
Consumer::addTask($crontab); Consumer::addTask($crontab);
} }
} catch (\Throwable $throwable) { } catch (\Throwable $throwable) {
$this->application->addError($throwable->getMessage()); $this->application->addError($throwable->getMessage());
} finally { } finally {
fire(Event::SYSTEM_RESOURCE_RELEASES); fire(Event::SYSTEM_RESOURCE_RELEASES);
} }
}); });
$this->popChannel(); $this->popChannel();
} }
/** /**
* @param \Swoole\Process $process * @param \Swoole\Process $process
* @throws \ReflectionException * @throws \ReflectionException
* @throws \Snowflake\Exception\ComponentException * @throws \Snowflake\Exception\ComponentException
* @throws \Snowflake\Exception\ConfigException * @throws \Snowflake\Exception\ConfigException
* @throws \Snowflake\Exception\NotFindClassException * @throws \Snowflake\Exception\NotFindClassException
*/ */
public function tick(\Swoole\Process $process) public function tick(\Swoole\Process $process)
{ {
$value = $process->read(40); $value = $process->read(40);
$redis = Snowflake::app()->getRedis(); $redis = Snowflake::app()->getRedis();
$crontab = swoole_unserialize($redis->get($value)); $crontab = swoole_unserialize($redis->get($value));
$redis->del($value); $redis->del($value);
if (is_object($crontab)) { if (is_object($crontab)) {
$this->channel->push($crontab); $this->channel->push($crontab);
} }
$redis->release(); $redis->release();
$this->tick($process); $this->tick($process);
} }
/** /**
* @param Crontab $crontab * @param Crontab $crontab
* @throws Exception * @throws Exception
*/ */
private static function addTask(Crontab $crontab) private static function addTask(Crontab $crontab)
{ {
$redis = Snowflake::app()->getRedis(); $redis = Snowflake::app()->getRedis();
$name = md5($crontab->getName()); $name = md5($crontab->getName());
$redis->set('crontab:' . $name, swoole_serialize($crontab)); $redis->set('crontab:' . $name, swoole_serialize($crontab));
$tickTime = time() + $crontab->getTickTime(); $tickTime = time() + $crontab->getTickTime();
$redis->zAdd(Producer::CRONTAB_KEY, $tickTime, $crontab->getName()); $redis->zAdd(Producer::CRONTAB_KEY, $tickTime, $crontab->getName());
} }
} }