Skip to content

Commit

Permalink
simplify restart
Browse files Browse the repository at this point in the history
  • Loading branch information
joedixon committed Jan 5, 2024
1 parent 8a4cecd commit a590ea5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 106 deletions.
54 changes: 0 additions & 54 deletions src/Concerns/InteractsWithServerState.php

This file was deleted.

45 changes: 6 additions & 39 deletions src/Servers/Reverb/Console/Commands/RestartServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.');
}
}
22 changes: 9 additions & 13 deletions src/Servers/Reverb/Console/Commands/StartServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,7 +21,7 @@

class StartServer extends Command implements SignalableCommandInterface
{
use InteractsWithAsyncRedis, InteractsWithServerState;
use InteractsWithAsyncRedis;

/**
* The name and signature of the console command.
Expand All @@ -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));
}

Expand All @@ -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}");

Expand All @@ -82,7 +81,6 @@ public function getSubscribedSignals(): array
public function handleSignal(int $signal): void
{
$this->gracefullyDisconnect();
$this->destroyState();
}

/**
Expand All @@ -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}");
});
}

Expand Down

0 comments on commit a590ea5

Please sign in to comment.