Skip to content

Commit

Permalink
Support to reload .env when using hyperf/watcher. (#6936)
Browse files Browse the repository at this point in the history

Co-authored-by: 李铭昕 <715557344@qq.com>
  • Loading branch information
albertcht and limingxinleo committed Jul 8, 2024
1 parent 804dc0a commit 3ad2e1d
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace Hyperf\Watcher;

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

class ConfigProvider
{
Expand All @@ -24,6 +25,9 @@ public function __invoke(): array
'commands' => [
WatchCommand::class,
],
'listeners' => [
ReloadDotenvAndConfigListener::class,
],
'publish' => [
[
'id' => 'config',
Expand Down
20 changes: 20 additions & 0 deletions src/Event/BeforeServerRestart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/

namespace Hyperf\Watcher\Event;

class BeforeServerRestart
{
public function __construct(public string $pid)
{
}
}
69 changes: 69 additions & 0 deletions src/Listener/ReloadDotenvAndConfigListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/

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
{
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
{
DotenvManager::reload([BASE_PATH]);
}
}
4 changes: 4 additions & 0 deletions src/Watcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
use Hyperf\Support\Filesystem\FileNotFoundException;
use Hyperf\Support\Filesystem\Filesystem;
use Hyperf\Watcher\Driver\DriverInterface;
use Hyperf\Watcher\Event\BeforeServerRestart;
use PhpParser\PrettyPrinter\Standard;
use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;

Expand Down Expand Up @@ -109,6 +111,8 @@ public function restart($isStart = true)
$pid = $this->filesystem->get($file);
try {
$this->output->writeln('Stop server...');
$this->container->get(EventDispatcherInterface::class)
->dispatch(new BeforeServerRestart($pid));
if (posix_kill((int) $pid, 0)) {
posix_kill((int) $pid, SIGTERM);
}
Expand Down

0 comments on commit 3ad2e1d

Please sign in to comment.