From 5fd26c9a75a6690c1468e5f3765ce312e872cd21 Mon Sep 17 00:00:00 2001 From: Albert Chen Date: Fri, 12 Jul 2024 14:10:34 +0800 Subject: [PATCH] Removed Swoole Atomic dependency and useless restart counter in watcher.(#6946) Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com> --- src/ConfigProvider.php | 4 +-- ...gListener.php => ReloadDotenvListener.php} | 29 +------------------ 2 files changed, 3 insertions(+), 30 deletions(-) rename src/Listener/{ReloadDotenvAndConfigListener.php => ReloadDotenvListener.php} (51%) diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php index 4206be1..fb24cf6 100644 --- a/src/ConfigProvider.php +++ b/src/ConfigProvider.php @@ -13,7 +13,7 @@ namespace Hyperf\Watcher; use Hyperf\Watcher\Command\WatchCommand; -use Hyperf\Watcher\Listener\ReloadDotenvAndConfigListener; +use Hyperf\Watcher\Listener\ReloadDotenvListener; class ConfigProvider { @@ -26,7 +26,7 @@ public function __invoke(): array WatchCommand::class, ], 'listeners' => [ - ReloadDotenvAndConfigListener::class, + ReloadDotenvListener::class, ], 'publish' => [ [ diff --git a/src/Listener/ReloadDotenvAndConfigListener.php b/src/Listener/ReloadDotenvListener.php similarity index 51% rename from src/Listener/ReloadDotenvAndConfigListener.php rename to src/Listener/ReloadDotenvListener.php index f27e50e..b9acd5c 100644 --- a/src/Listener/ReloadDotenvAndConfigListener.php +++ b/src/Listener/ReloadDotenvListener.php @@ -12,54 +12,27 @@ namespace Hyperf\Watcher\Listener; -use Hyperf\Contract\ConfigInterface; use Hyperf\Event\Contract\ListenerInterface; -use Hyperf\Framework\Event\BeforeWorkerStart; use Hyperf\Support\DotenvManager; use Hyperf\Watcher\Event\BeforeServerRestart; use Psr\Container\ContainerInterface; -use Swoole\Atomic; -class ReloadDotenvAndConfigListener implements ListenerInterface +final class ReloadDotenvListener implements ListenerInterface { - protected static Atomic $restartCounter; - public function __construct(protected ContainerInterface $container) { - static::$restartCounter = new Atomic(0); } public function listen(): array { return [ - BeforeWorkerStart::class, BeforeServerRestart::class, ]; } public function process(object $event): void { - if ($event instanceof BeforeWorkerStart - && $event->workerId === 0 - && static::$restartCounter->get() === 0 - ) { - static::$restartCounter->add(); - return; - } - - static::$restartCounter->add(); - $this->reloadDotenv(); - $this->reloadConfig(); - } - - protected function reloadConfig(): void - { - if (! method_exists($this->container, 'unbind')) { - return; - } - - $this->container->unbind(ConfigInterface::class); } protected function reloadDotenv(): void