Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

[feature] Add restart command for WebSocket server #284

Merged
merged 1 commit into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/Console/RestartWebSocketServer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace BeyondCode\LaravelWebSockets\Console;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\InteractsWithTime;

class RestartWebSocketServer extends Command
{
use InteractsWithTime;

protected $signature = 'websockets:restart';

protected $description = 'Restart the Laravel WebSocket Server';

public function handle()
{
Cache::forever('beyondcode:websockets:restart', $this->currentTime());

$this->info('Broadcasting WebSocket server restart signal.');
}
}
23 changes: 23 additions & 0 deletions src/Console/StartWebSocketServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
use Clue\React\Buzz\Browser;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use React\Dns\Config\Config as DnsConfig;
use React\Dns\Resolver\Factory as DnsFactory;
use React\Dns\Resolver\ResolverInterface;
Expand All @@ -29,6 +30,9 @@ class StartWebSocketServer extends Command
/** @var \React\EventLoop\LoopInterface */
protected $loop;

/** @var int */
protected $lastRestart;

public function __construct()
{
parent::__construct();
Expand All @@ -43,6 +47,7 @@ public function handle()
->configureHttpLogger()
->configureMessageLogger()
->configureConnectionLogger()
->configureRestartTimer()
->registerEchoRoutes()
->registerCustomRoutes()
->startWebSocketServer();
Expand Down Expand Up @@ -104,6 +109,19 @@ protected function configureConnectionLogger()
return $this;
}

public function configureRestartTimer()
{
$this->lastRestart = $this->getLastRestart();

$this->loop->addPeriodicTimer(10, function () {
if ($this->getLastRestart() !== $this->lastRestart) {
$this->loop->stop();
}
});

return $this;
}

protected function registerEchoRoutes()
{
WebSocketsRouter::echo();
Expand Down Expand Up @@ -150,4 +168,9 @@ protected function getDnsResolver(): ResolverInterface
$this->loop
);
}

protected function getLastRestart()
{
return Cache::get('beyondcode:websockets:restart', 0);
}
}
1 change: 1 addition & 0 deletions src/WebSocketsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function boot()
$this->commands([
Console\StartWebSocketServer::class,
Console\CleanStatistics::class,
Console\RestartWebSocketServer::class,
]);
}

Expand Down
23 changes: 23 additions & 0 deletions tests/Commands/RestartWebSocketServerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace BeyondCode\LaravelWebSockets\Tests\Commands;

use Artisan;
use BeyondCode\LaravelWebSockets\Tests\TestCase;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\InteractsWithTime;

class RestartWebSocketServerTest extends TestCase
{
use InteractsWithTime;

/** @test */
public function it_can_broadcast_restart_signal()
{
$start = $this->currentTime();

Artisan::call('websockets:restart');

$this->assertGreaterThanOrEqual($start, Cache::get('beyondcode:websockets:restart', 0));
}
}