From f04cce73d30024223a68d57a7a3655c6d4e8b70a Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Fri, 11 Sep 2020 16:39:56 +0300 Subject: [PATCH] Added websockets:flush command --- .../Commands/FlushCollectedStatistics.php | 37 +++++++++++++++++++ src/Console/Commands/StartServer.php | 9 ----- src/WebSocketsServiceProvider.php | 10 +++++ tests/TestCase.php | 13 +------ 4 files changed, 49 insertions(+), 20 deletions(-) create mode 100644 src/Console/Commands/FlushCollectedStatistics.php diff --git a/src/Console/Commands/FlushCollectedStatistics.php b/src/Console/Commands/FlushCollectedStatistics.php new file mode 100644 index 0000000..274129f --- /dev/null +++ b/src/Console/Commands/FlushCollectedStatistics.php @@ -0,0 +1,37 @@ +comment('Flushing the collected WebSocket Statistics...'); + + StatisticsCollector::flush(); + + $this->line('Flush complete!'); + } +} diff --git a/src/Console/Commands/StartServer.php b/src/Console/Commands/StartServer.php index c67426b..664d6a9 100644 --- a/src/Console/Commands/StartServer.php +++ b/src/Console/Commands/StartServer.php @@ -3,7 +3,6 @@ namespace BeyondCode\LaravelWebSockets\Console\Commands; use BeyondCode\LaravelWebSockets\Contracts\ChannelManager; -use BeyondCode\LaravelWebSockets\Contracts\StatisticsCollector; use BeyondCode\LaravelWebSockets\Facades\StatisticsCollector as StatisticsCollectorFacade; use BeyondCode\LaravelWebSockets\Facades\WebSocketRouter; use BeyondCode\LaravelWebSockets\Server\Loggers\ConnectionLogger; @@ -120,14 +119,6 @@ class StartServer extends Command */ protected function configureStatistics() { - $this->laravel->singleton(StatisticsCollector::class, function () { - $replicationMode = config('websockets.replication.mode', 'local'); - - $class = config("websockets.replication.modes.{$replicationMode}.collector"); - - return new $class; - }); - if (! $this->option('disable-statistics')) { $intervalInSeconds = $this->option('statistics-interval') ?: config('websockets.statistics.interval_in_seconds', 3600); diff --git a/src/WebSocketsServiceProvider.php b/src/WebSocketsServiceProvider.php index 5498184..e498c11 100644 --- a/src/WebSocketsServiceProvider.php +++ b/src/WebSocketsServiceProvider.php @@ -2,6 +2,7 @@ namespace BeyondCode\LaravelWebSockets; +use BeyondCode\LaravelWebSockets\Contracts\StatisticsCollector; use BeyondCode\LaravelWebSockets\Contracts\StatisticsStore; use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\AuthenticateDashboard; use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\SendMessage; @@ -65,6 +66,14 @@ class WebSocketsServiceProvider extends ServiceProvider return new $class; }); + + $this->app->singleton(StatisticsCollector::class, function () { + $replicationMode = config('websockets.replication.mode', 'local'); + + $class = config("websockets.replication.modes.{$replicationMode}.collector"); + + return new $class; + }); } /** @@ -91,6 +100,7 @@ class WebSocketsServiceProvider extends ServiceProvider Console\Commands\StartServer::class, Console\Commands\RestartServer::class, Console\Commands\CleanStatistics::class, + Console\Commands\FlushCollectedStatistics::class, ]); } diff --git a/tests/TestCase.php b/tests/TestCase.php index b63fcf3..db68ef7 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -222,24 +222,15 @@ abstract class TestCase extends Orchestra } /** - * Register the statistics collectors that are - * not resolved by the package service provider. + * Register the statistics collectors. * * @return void */ protected function registerStatisticsCollectors() { - $this->app->singleton(StatisticsCollector::class, function () { - $mode = config('websockets.replication.mode', $this->replicationMode); - - $class = config("websockets.replication.modes.{$mode}.collector"); - - return new $class; - }); - $this->statisticsCollector = $this->app->make(StatisticsCollector::class); - $this->statisticsCollector->flush(); + $this->artisan('websockets:flush'); } /**