diff --git a/composer.json b/composer.json index 160e8c6d..0a1d3861 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,6 @@ "ext-iconv": "*", "ext-mbstring": "*", "ext-xml": "*", - "ext-inotify": "*", "ext-curl": "*", "ext-openssl": "*", "amphp/amp": "v1.2.2", @@ -30,7 +29,13 @@ "ext-rdkafka": "*", "ext-gd": "*", "swiftmailer/swiftmailer": "^6.0", - "ext-spltype": "*" + "psr/container": "^2.0", + "psr/http-server-middleware": "^1.0", + "psr/link": "^2.0", + "psr/cache": "^3.0", + "psr/http-client": "^1.0", + "psr/simple-cache": "^1.0", + "game-worker/kiri-event": "dev-master" }, "autoload": { "psr-4": { diff --git a/core/Crontab/Crontab.php b/core/Crontab/Crontab.php deleted file mode 100644 index 8d459092..00000000 --- a/core/Crontab/Crontab.php +++ /dev/null @@ -1,292 +0,0 @@ -params = $params; - $this->isLoop = $isLoop; - $this->tickTime = $tickTime; - } - - - /** - * @return Application - */ - #[Pure] private function getApplication(): Application - { - return Kiri::app(); - } - - - /** - * @return $this - */ - public function increment(): static - { - $this->execute_number += 1; - return $this; - } - - - /** - * @return string - */ - #[Pure] public function getName(): string - { - return md5($this->name); - } - - /** - * @return mixed - */ - public function getParams(): mixed - { - return $this->params; - } - - /** - * @return int - */ - public function getTickTime(): int - { - return $this->tickTime; - } - - /** - * @return bool - */ - public function isLoop(): bool - { - return $this->isLoop; - } - - /** - * @return int - */ - public function getMaxExecuteNumber(): int - { - return $this->max_execute_number; - } - - /** - * @return int - */ - public function getExecuteNumber(): int - { - return $this->execute_number; - } - - - /** - * @param string $name - */ - public function setName(string $name): void - { - $this->name = $name; - } - - - /** - * @param int $max_execute_number - */ - public function setMaxExecuteNumber(int $max_execute_number): void - { - $this->max_execute_number = $max_execute_number; - } - - /** - * @param int $execute_number - */ - public function setExecuteNumber(int $execute_number): void - { - $this->execute_number = $execute_number; - } - - /** - * @return int - */ - public function getTimerId(): int - { - return $this->timerId; - } - - - /** - * - * @throws Exception - */ - public function clearTimer() - { - $this->application->warning('crontab timer clear.'); - if (Timer::exists($this->timerId)) { - Timer::clear($this->timerId); - } - } - - - /** - * @param $name - * @return mixed - */ - #[Pure] public function __get($name): mixed - { - if ($name === 'application') { - return $this->getApplication(); - } - if (!isset($this->params[$name])) { - return null; - } - return $this->params[$name]; - } - - - /** - * @throws Exception - */ - public function execute(): void - { - try { - defer(fn() => $this->afterExecute()); - $redis = $this->application->getRedis(); - - $name_md5 = $this->getName(); - $redis->hSet(self::WAIT_END, $name_md5, static::getSerialize($this)); - call_user_func([$this, 'process']); - $this->execute_number += 1; - $redis->hDel(self::WAIT_END, $name_md5); - } catch (\Throwable $throwable) { - $this->application->addError($throwable, 'throwable'); - } - } - - - /** - * @throws Exception - */ - public function afterExecute() - { - if ($this->isRecover() !== 999) { - return; - } - $redis = $this->application->getRedis(); - $name = $this->getName(); - if (!$redis->exists('stop:crontab:' . $name)) { - $redis->set('crontab:' . $name, swoole_serialize($this)); - $tickTime = time() + $this->getTickTime(); - $redis->zAdd(Producer::CRONTAB_KEY, $tickTime, $name); - } else { - $redis->del('crontab:' . $name); - $redis->del('stop:crontab:' . $name); - } - } - - - /** - * @return bool|int - * @throws Exception - */ - public function isRecover(): bool|int - { - try { - $redis = $this->application->getRedis(); - $crontab_name = $this->getName(); - if ($redis->exists('stop:crontab:' . $crontab_name)) { - return $redis->del('stop:crontab:' . $crontab_name); - } - if ($this->isExit()) { - return $redis->del('crontab:' . $crontab_name); - } - if (!$this->isMaxExecute()) { - return 999; - } - call_user_func([$this, 'onMaxExecute']); - return $redis->del('crontab:' . $crontab_name); - } catch (\Throwable $throwable) { - return $this->application->addError($throwable, 'throwable'); - } - } - - - /** - * @param $class - * @return string - */ - public static function getSerialize($class): string - { - return serialize($class); - } - - - /** - * @return bool - */ - private function isExit(): bool - { - if ($this->isStop() || !$this->isLoop) { - return true; - } - return false; - } - - - /** - * @return bool - */ - private function isMaxExecute(): bool - { - if ($this->max_execute_number !== -1) { - return $this->execute_number >= $this->max_execute_number; - } - return false; - } - - -} diff --git a/core/Crontab/CrontabInterface.php b/core/Crontab/CrontabInterface.php deleted file mode 100644 index 17d215ec..00000000 --- a/core/Crontab/CrontabInterface.php +++ /dev/null @@ -1,20 +0,0 @@ -getServer(); - $application->set('crontab', ['class' => Producer::class]); - if (Config::get('crontab.enable') !== true) { - return; - } - $server->addProcess(Zookeeper::class); - } - -} diff --git a/core/Crontab/DefaultCrontab.php b/core/Crontab/DefaultCrontab.php deleted file mode 100644 index 0c237e05..00000000 --- a/core/Crontab/DefaultCrontab.php +++ /dev/null @@ -1,40 +0,0 @@ -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.'); - } - - $redis->del('stop:crontab:' . $name); - - $redis->del('crontab:' . $name); - $redis->zRem(static::CRONTAB_KEY, $name); - - $redis->zAdd(self::CRONTAB_KEY, time() + $crontab->getTickTime(), $name); - $redis->set('crontab:' . $name, swoole_serialize($crontab)); - } - - - /** - * @param string $name - * @throws Exception - */ - public function clear(string $name) - { - $redis = Kiri::app()->getRedis(); - - $redis->del('crontab:' . md5($name)); - $redis->zRem(static::CRONTAB_KEY, md5($name)); - - $redis->setex('stop:crontab:' . md5($name), 120, 1); - } - - - /** - * @param string $name - * @return bool - * @throws Exception - */ - public function exists(string $name): bool - { - $redis = Kiri::app()->getRedis(); - if ($redis->exists('crontab:' . md5($name))) { - return true; - } - if ($redis->zRank(static::CRONTAB_KEY, md5($name))) { - return true; - } - if ($redis->hExists(Crontab::WAIT_END, md5($name))) { - return true; - } - return false; - } - - - /** - * @throws Exception - */ - public function clearAll() - { - $redis = Kiri::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/core/Crontab/Zookeeper.php b/core/Crontab/Zookeeper.php deleted file mode 100644 index 5bd8d659..00000000 --- a/core/Crontab/Zookeeper.php +++ /dev/null @@ -1,129 +0,0 @@ -pid . ']'; - if (!empty($prefix)) { - $name .= '.Crontab zookeeper'; - } - return $name; - } - - - /** - * @param Process $process - * @throws Exception - */ - public function onHandler(Process $process): void - { - $this->_timer = Timer::tick(300, [$this, 'loop']); - } - - - /** - * @throws ConfigException - * @throws Exception - */ - public function loop() - { - if ($this->checkProcessIsStop()) { - $this->exit(); - Timer::clear($this->_timer); - return; - } - $redis = Kiri::app()->getRedis(); - defer(fn() => $redis->release()); - $range = $this->loadCarobTask($redis); - foreach ($range as $value) { - $this->dispatch($redis, $value); - } - } - - - /** - * @param Redis|\Redis $redis - * @param $value - * @throws Exception - */ - private function dispatch(Redis|\Redis $redis, $value) - { - try { - if (empty($handler = $redis->get('crontab:' . $value))) { - return; - } - $server = di(ServerManager::class)->getServer(); - $server->sendMessage(swoole_unserialize($handler), $this->getWorker()); - } catch (Throwable $exception) { - logger()->addError($exception); - } - } - - - /** - * @return int - * @throws Exception - */ - private function getWorker(): int - { - if ($this->workerNum == 0) { - $server = di(ServerManager::class)->getServer(); - - $this->workerNum = $server->setting['worker_num'] + ($server->setting['task_worker_num'] ?? 0); - } - return random_int(0, $this->workerNum - 1); - } - - - /** - * @param Redis|\Redis $redis - * @return array - */ - private function loadCarobTask(Redis|\Redis $redis): array - { - $script = <<