From 2e78fc73c7c79c3b75281bc77f2cad0df80848b5 Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Fri, 16 Apr 2021 00:35:55 +0800 Subject: [PATCH] modify --- System/Crontab/Producer.php | 92 ++++++++++++++++++------------------ System/Crontab/Zookeeper.php | 5 +- 2 files changed, 48 insertions(+), 49 deletions(-) diff --git a/System/Crontab/Producer.php b/System/Crontab/Producer.php index eda21424..5c9b93cf 100644 --- a/System/Crontab/Producer.php +++ b/System/Crontab/Producer.php @@ -16,64 +16,62 @@ use Snowflake\Snowflake; class Producer extends Component { - const CRONTAB_KEY = '_application:system:crontab'; + const CRONTAB_KEY = '_application:system:crontab'; - /** - * @param Crontab $crontab - * @throws Exception - */ - public function dispatch(Crontab $crontab) - { - $redis = Snowflake::app()->getRedis(); + /** + * @param Crontab $crontab + * @throws Exception + */ + public function dispatch(Crontab $crontab) + { + $redis = Snowflake::app()->getRedis(); - $name = $crontab->getName(); - if ( - $redis->exists(self::CRONTAB_KEY) && - $redis->type(self::CRONTAB_KEY) !== \Redis::REDIS_ZSET) { - throw new Exception('Cache key ' . self::CRONTAB_KEY . ' types error.'); - } + $name = $crontab->getName(); + if ( + $redis->exists(self::CRONTAB_KEY) && + $redis->type(self::CRONTAB_KEY) !== \Redis::REDIS_ZSET) { + throw new Exception('Cache key ' . self::CRONTAB_KEY . ' types error.'); + } - $tickTime = time() + $crontab->getTickTime(); - $redis->del('stop:crontab:' . $name, 120); + $tickTime = time() + $crontab->getTickTime(); + $redis->del('stop:crontab:' . $name, 120); - var_dump($tickTime); - - $result = $redis->zAdd(self::CRONTAB_KEY, $tickTime, $name); - if ($result) { - $redis->set('crontab:' . $name, swoole_serialize($crontab)); - } - } + $result = $redis->zAdd(self::CRONTAB_KEY . ':' . time(), $tickTime, $name); + if ($result) { + $redis->set('crontab:' . $name, swoole_serialize($crontab)); + } + } - /** - * @param string $name - * @throws Exception - */ - public function clear(string $name) - { - $redis = Snowflake::app()->getRedis(); + /** + * @param string $name + * @throws Exception + */ + public function clear(string $name) + { + $redis = Snowflake::app()->getRedis(); - $redis->setex('stop:crontab:' . md5($name), 120, 1); + $redis->setex('stop:crontab:' . md5($name), 120, 1); - $redis->zRem(self::CRONTAB_KEY, $name); - $redis->del('crontab:' . md5($name)); - } + $redis->zRem(self::CRONTAB_KEY, $name); + $redis->del('crontab:' . md5($name)); + } - /** - * @throws Exception - */ - public function clearAll() - { - $redis = Snowflake::app()->getRedis(); - $data = $redis->zRange(self::CRONTAB_KEY, 0, -1); - foreach ($data as $datum) { - $redis->setex('stop:crontab:' . $datum, 120, 1); - $redis->del('crontab:' . $datum); - } - $redis->release(); - } + /** + * @throws Exception + */ + public function clearAll() + { + $redis = Snowflake::app()->getRedis(); + $data = $redis->zRange(self::CRONTAB_KEY, 0, -1); + foreach ($data as $datum) { + $redis->setex('stop:crontab:' . $datum, 120, 1); + $redis->del('crontab:' . $datum); + } + $redis->release(); + } } diff --git a/System/Crontab/Zookeeper.php b/System/Crontab/Zookeeper.php index 707f018a..869a97f2 100644 --- a/System/Crontab/Zookeeper.php +++ b/System/Crontab/Zookeeper.php @@ -43,7 +43,7 @@ class Zookeeper extends Process if (Snowflake::getPlatform()->isLinux()) { name($this->pid, 'Crontab zookeeper.'); } - Timer::tick(1000, function () { + while (true) { [$range, $redis] = $this->loadCarobTask(); $server = Snowflake::app()->getSwoole(); @@ -52,7 +52,8 @@ class Zookeeper extends Process $this->dispatch($server, $redis, $setting, $value); } $redis->release(); - }); + sleep(1); + } }