This commit is contained in:
as2252258@163.com
2021-04-11 15:38:23 +08:00
parent d4b64c3af2
commit 60fa2cb896
3 changed files with 136 additions and 120 deletions
+89 -75
View File
@@ -22,94 +22,108 @@ 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 * @throws Exception
*/ */
public function onHandler(\Swoole\Process $process): void public function onHandler(\Swoole\Process $process): void
{ {
if (Snowflake::getPlatform()->isLinux()) { if (Snowflake::getPlatform()->isLinux()) {
name($this->pid, 'Crontab consumer'); name($this->pid, '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->recovery();
$this->tick($process);
}
/** private function recovery()
* @throws Exception {
*/ $redis = redis()->hGetAll('crontab:wait:execute');
public function popChannel() foreach ($redis as $redi) {
{ $this->channel->push($redi);
/** @var Crontab $crontab */ }
$crontab = $this->channel->pop(-1); fire(Event::SYSTEM_RESOURCE_RELEASES);
go(function () use ($crontab) { }
try {
$crontab->increment()->execute();
if ($crontab->getExecuteNumber() < $crontab->getMaxExecuteNumber()) {
Consumer::addTask($crontab);
} else if ($crontab->isLoop()) {
Consumer::addTask($crontab);
}
} catch (\Throwable $throwable) {
logger()->addError($throwable,'throwable');
} finally {
fire(Event::SYSTEM_RESOURCE_RELEASES);
}
});
$this->popChannel();
}
/** /**
* @param \Swoole\Process $process * @throws Exception
* @throws ReflectionException */
* @throws ComponentException public function popChannel()
* @throws ConfigException {
* @throws NotFindClassException /** @var Crontab $crontab */
* @throws Exception $crontab = $this->channel->pop(-1);
*/ go(function () use ($crontab) {
public function tick(\Swoole\Process $process) try {
{ $crontab->increment()->execute();
$value = $process->read(40); if ($crontab->getExecuteNumber() < $crontab->getMaxExecuteNumber()) {
Consumer::addTask($crontab);
$redis = Snowflake::app()->getRedis(); } else if ($crontab->isLoop()) {
Consumer::addTask($crontab);
$crontab = swoole_unserialize($redis->get($value)); }
$redis->del($value); } catch (\Throwable $throwable) {
if (is_object($crontab)) { logger()->addError($throwable, 'throwable');
$this->channel->push($crontab); } finally {
} fire(Event::SYSTEM_RESOURCE_RELEASES);
}
$redis->release(); });
$this->popChannel();
$this->tick($process); }
}
/** /**
* @param Crontab $crontab * @param \Swoole\Process $process
* @throws Exception * @throws ReflectionException
*/ * @throws ComponentException
private static function addTask(Crontab $crontab) * @throws ConfigException
{ * @throws NotFindClassException
$redis = Snowflake::app()->getRedis(); * @throws Exception
*/
public function tick(\Swoole\Process $process)
{
$value = $process->read(40);
$name = md5($crontab->getName()); $redis = Snowflake::app()->getRedis();
$redis->set('crontab:' . $name, swoole_serialize($crontab)); /** @var Crontab $crontab */
$crontab = swoole_unserialize($redis->get($value));
$redis->hSet('crontab:wait:execute', $crontab->getName(), $redis->get($value));
$redis->del($value);
if (is_object($crontab)) {
$this->channel->push($crontab);
}
$tickTime = time() + $crontab->getTickTime(); $redis->release();
$redis->zAdd(Producer::CRONTAB_KEY, $tickTime, $crontab->getName()); $this->tick($process);
} }
/**
* @param Crontab $crontab
* @throws Exception
*/
private static function addTask(Crontab $crontab)
{
$redis = Snowflake::app()->getRedis();
$name = md5($crontab->getName());
$redis->set('crontab:' . $name, swoole_serialize($crontab));
$tickTime = time() + $crontab->getTickTime();
$redis->zAdd(Producer::CRONTAB_KEY, $tickTime, $crontab->getName());
}
} }
+1
View File
@@ -200,6 +200,7 @@ class Crontab extends BaseObject
public function execute(): void public function execute(): void
{ {
$params = call_user_func($this->handler, $this->params, $this->name); $params = call_user_func($this->handler, $this->params, $this->name);
\redis()->hDel('crontab:wait:execute', $this->getName());
if ($params !== null) { if ($params !== null) {
$name = date('Y_m_d_H_i_s.' . $this->name . '.log'); $name = date('Y_m_d_H_i_s.' . $this->name . '.log');
write(storage($name, '/log/crontab'), serialize($params)); write(storage($name, '/log/crontab'), serialize($params));
+46 -45
View File
@@ -19,68 +19,69 @@ class ZookeeperProcess extends Process
{ {
private Channel $channel; private Channel $channel;
private WaitGroup $waitGroup; private WaitGroup $waitGroup;
/** @var Crontab[] $names */ /** @var Crontab[] $names */
public array $names = []; public array $names = [];
public array $scores = []; public array $scores = [];
public array $timers = []; public array $timers = [];
/** /**
* @param \Swoole\Process $process * @param \Swoole\Process $process
* @throws Exception * @throws Exception
*/ */
public function onHandler(\Swoole\Process $process): void public function onHandler(\Swoole\Process $process): void
{ {
$crontab = Snowflake::app()->get('crontab'); $crontab = Snowflake::app()->get('crontab');
$crontab->clearAll(); $crontab->clearAll();
if (Snowflake::getPlatform()->isLinux()) { if (Snowflake::getPlatform()->isLinux()) {
name($this->pid, 'Crontab zookeeper.'); name($this->pid, 'Crontab zookeeper.');
} }
Timer::tick(1000, function () { Timer::tick(1000, function () {
$startTime = time(); $startTime = time();
$redis = Snowflake::app()->getRedis(); $redis = Snowflake::app()->getRedis();
$redis->multi(); $redis->multi();
$range = $redis->zRangeByScore(Producer::CRONTAB_KEY, '0', (string)$startTime); $range = $redis->zRangeByScore(Producer::CRONTAB_KEY, '0', (string)$startTime);
$redis->zRemRangeByScore(Producer::CRONTAB_KEY, '0', (string)$startTime); $redis->zRemRangeByScore(Producer::CRONTAB_KEY, '0', (string)$startTime);
$redis->exec(); $redis->exec();
$redis->release(); $redis->release();
/** @var Consumer $consumer */ /** @var Consumer $consumer */
$consumer = Snowflake::app()->get(Consumer::class); $consumer = Snowflake::app()->get(Consumer::class);
foreach ($range as $value) { foreach ($range as $value) {
$consumer->write('crontab:' . md5($value)); $consumer->write('crontab:' . md5($value));
} }
}); });
} }
/**
* @param string $name
*/
public function clear(string $name)
{
if (!isset($this->names[$name])) {
return;
}
$timers = $this->timers[$name];
$search = array_search($name, $this->scores[$timers]); /**
if ($search !== false) { * @param string $name
unset($this->scores[$timers][$search]); */
} public function clear(string $name)
unset($this->timers[$name], $this->names[$name]); {
} if (!isset($this->names[$name])) {
return;
}
$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]);
}
} }