Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jan 19, 2024
1 parent fd95461 commit 53027bf
Showing 1 changed file with 35 additions and 27 deletions.
62 changes: 35 additions & 27 deletions src/Servers/Reverb/Console/Commands/StartServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,60 +49,50 @@ public function handle(): void
}

$config = $this->laravel['config']['reverb.servers.reverb'];
$host = $this->option('host') ?: $config['host'];
$port = $this->option('port') ?: $config['port'];

$loop = Loop::get();

$server = ServerFactory::make($host, $port, loop: $loop);
$server = ServerFactory::make(
$host = $this->option('host') ?: $config['host'],
$port = $this->option('port') ?: $config['port'],
loop: $loop
);

if ($this->laravel->make(ServerProviderManager::class)->driver('reverb')->subscribesToEvents()) {
$this->laravel->make(PubSubProvider::class)->connect($loop);
$this->laravel->make(PubSubProvider::class)->subscribe();
}

$this->scheduleCleanup($loop);
$this->checkForRestartSignal($server, $loop, $host, $port);
$this->ensureHorizontalScalability($loop);
$this->ensureStaleConnectionsAreCleaned($loop);
$this->ensureRestartCommandIsRespected($server, $loop, $host, $port);

$this->components->info("Starting server on {$host}:{$port}");

$server->start();
}

/**
* Get the list of signals handled by the command.
* Ensure that horizontal scalability via broadcasting is enabled if configured.
*/
public function getSubscribedSignals(): array
protected function ensureHorizontalScalability(LoopInterface $loop): void
{
return [SIGINT, SIGTERM];
}

/**
* Handle the signals sent to the server.
*/
public function handleSignal(int $signal): void
{
$this->components->info('Gracefully terminating connections.');

$this->gracefullyDisconnect();
if ($this->laravel->make(ServerProviderManager::class)->driver('reverb')->subscribesToEvents()) {
$this->laravel->make(PubSubProvider::class)->connect($loop);
$this->laravel->make(PubSubProvider::class)->subscribe();
}
}

/**
* Use the event loop to schedule periodic cleanup of connections.
*/
protected function scheduleCleanup(LoopInterface $loop): void
protected function ensureStaleConnectionsAreCleaned(LoopInterface $loop): void
{
$loop->addPeriodicTimer(60, function () {
PruneStaleConnections::dispatch();

PingInactiveConnections::dispatch();
});
}

/**
* Check to see whether the restart signal has been sent.
*/
protected function checkForRestartSignal(Server $server, LoopInterface $loop, string $host, string $port): void
protected function ensureRestartCommandIsRespected(Server $server, LoopInterface $loop, string $host, string $port): void
{
$lastRestart = Cache::get('laravel:reverb:restart');

Expand Down Expand Up @@ -131,7 +121,25 @@ protected function gracefullyDisconnect(): void
$this->laravel->make(ChannelManager::class)
->for($application)
->connections()
)->each(fn (ChannelConnection $connection) => $connection->disconnect());
)->each->disconnect();
});
}

/**
* Get the list of signals handled by the command.
*/
public function getSubscribedSignals(): array
{
return [SIGINT, SIGTERM];
}

/**
* Handle the signals sent to the server.
*/
public function handleSignal(int $signal): void
{
$this->components->info('Gracefully terminating connections.');

$this->gracefullyDisconnect();
}
}

0 comments on commit 53027bf

Please sign in to comment.