diff --git a/src/Concerns/InteractsWithServerState.php b/src/Concerns/InteractsWithServerState.php deleted file mode 100644 index 51898a17..00000000 --- a/src/Concerns/InteractsWithServerState.php +++ /dev/null @@ -1,54 +0,0 @@ -key); - } - - /** - * Get the current state of the running server. - * - * @return null|array{HOST: string, PORT: int, DEBUG: bool, RESTART: bool} - */ - protected function getState(): ?array - { - return Cache::get($this->key); - } - - /** - * Set the state of the running server. - */ - protected function setState(string $host, int $port, bool $debug, bool $restart = false): void - { - Cache::forever($this->key, [ - 'HOST' => $host, - 'PORT' => $port, - 'DEBUG' => $debug ??= false, - 'RESTART' => $restart, - ]); - } - - /** - * Destroy the server state. - */ - protected function destroyState(): void - { - Cache::forget($this->key); - } -} diff --git a/src/Servers/Reverb/Console/Commands/RestartServer.php b/src/Servers/Reverb/Console/Commands/RestartServer.php index fdbd9b00..2b0e37d4 100644 --- a/src/Servers/Reverb/Console/Commands/RestartServer.php +++ b/src/Servers/Reverb/Console/Commands/RestartServer.php @@ -3,11 +3,12 @@ namespace Laravel\Reverb\Servers\Reverb\Console\Commands; use Illuminate\Console\Command; -use Laravel\Reverb\Concerns\InteractsWithServerState; +use Illuminate\Support\Facades\Cache; +use Illuminate\Support\InteractsWithTime; class RestartServer extends Command { - use InteractsWithServerState; + use InteractsWithTime; /** * The name and signature of the console command. @@ -21,49 +22,15 @@ class RestartServer extends Command * * @var string */ - protected $description = 'Signal Reverb to restart the server'; + protected $description = 'Restart the Reverb server'; /** * Execute the console command. */ public function handle(): void { - if (! $state = $this->getState()) { - $this->error('No Reverb server running.'); + Cache::forever('laravel:reverb:restart', $this->currentTime()); - return; - } - - $this->sendStopSignal($state); - - $this->waitForServerToStop(fn () => $this->call('reverb:start', [ - '--host' => $state['HOST'], - '--port' => $state['PORT'], - '--debug' => $state['DEBUG'], - ])); - } - - /** - * Send the stop signal to the running server. - * - * @param array{HOST: string, PORT: int, DEBUG: bool, RESTART: bool} $state - */ - protected function sendStopSignal(array $state): void - { - $this->components->info('Sending stop signal to Reverb server.'); - - $this->setState($state['HOST'], $state['PORT'], $state['DEBUG'], true); - } - - /** - * Run the callback when the server has stopped. - */ - protected function waitForServerToStop(callable $callback): void - { - while ($this->serverIsRunning()) { - usleep(1000); - } - - $callback(); + $this->components->info('Broadcasting Reverb restart signal.'); } } diff --git a/src/Servers/Reverb/Console/Commands/StartServer.php b/src/Servers/Reverb/Console/Commands/StartServer.php index 02d2bf9d..8c757014 100644 --- a/src/Servers/Reverb/Console/Commands/StartServer.php +++ b/src/Servers/Reverb/Console/Commands/StartServer.php @@ -3,9 +3,9 @@ namespace Laravel\Reverb\Servers\Reverb\Console\Commands; use Illuminate\Console\Command; +use Illuminate\Support\Facades\Cache; use Laravel\Reverb\Application; use Laravel\Reverb\Concerns\InteractsWithAsyncRedis; -use Laravel\Reverb\Concerns\InteractsWithServerState; use Laravel\Reverb\Contracts\ApplicationProvider; use Laravel\Reverb\Contracts\Logger; use Laravel\Reverb\Jobs\PingInactiveConnections; @@ -21,7 +21,7 @@ class StartServer extends Command implements SignalableCommandInterface { - use InteractsWithAsyncRedis, InteractsWithServerState; + use InteractsWithAsyncRedis; /** * The name and signature of the console command. @@ -45,7 +45,7 @@ class StartServer extends Command implements SignalableCommandInterface */ public function handle(): void { - if ($debug = $this->option('debug')) { + if ($this->option('debug')) { $this->laravel->instance(Logger::class, new CliLogger($this->output)); } @@ -60,8 +60,7 @@ public function handle(): void $this->bindRedis($loop); $this->subscribeToRedis($loop); $this->scheduleCleanup($loop); - $this->checkForRestartSignal($server, $loop); - $this->setState($host, $port, $debug ??= false); + $this->checkForRestartSignal($server, $loop, $host, $port); $this->components->info("Starting server on {$host}:{$port}"); @@ -82,7 +81,6 @@ public function getSubscribedSignals(): array public function handleSignal(int $signal): void { $this->gracefullyDisconnect(); - $this->destroyState(); } /** @@ -100,22 +98,20 @@ protected function scheduleCleanup(LoopInterface $loop): void /** * Check to see whether the restart signal has been sent. */ - protected function checkForRestartSignal(Server $server, LoopInterface $loop): void + protected function checkForRestartSignal(Server $server, LoopInterface $loop, string $host, string $port): void { - $loop->addPeriodicTimer(5, function () use ($server) { - $state = $this->getState(); + $lastRestart = Cache::get('laravel:reverb:restart'); - if (! $state['RESTART']) { + $loop->addPeriodicTimer(5, function () use ($server, $host, $port, $lastRestart) { + if ($lastRestart === Cache::get('laravel:pulse:restart')) { return; } - $this->components->info('Stopping Reverb server.'); - $this->gracefullyDisconnect(); $server->stop(); - $this->destroyState(); + $this->components->info("Stopping server on {$host}:{$port}"); }); }