Skip to content

Commit

Permalink
Removed Swoole Atomic dependency and useless restart counter in watch…
Browse files Browse the repository at this point in the history
…er.(#6946)


Co-authored-by: Deeka Wong <[email protected]>
  • Loading branch information
albertcht and huangdijia authored Jul 12, 2024
1 parent 3ad2e1d commit 5fd26c9
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace Hyperf\Watcher;

use Hyperf\Watcher\Command\WatchCommand;
use Hyperf\Watcher\Listener\ReloadDotenvAndConfigListener;
use Hyperf\Watcher\Listener\ReloadDotenvListener;

class ConfigProvider
{
Expand All @@ -26,7 +26,7 @@ public function __invoke(): array
WatchCommand::class,
],
'listeners' => [
ReloadDotenvAndConfigListener::class,
ReloadDotenvListener::class,
],
'publish' => [
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5fd26c9

Please sign in to comment.