From db7506bd1162b72c2cf09cfbc3b8eb585f251180 Mon Sep 17 00:00:00 2001 From: Joe Dixon Date: Thu, 7 Dec 2023 11:54:16 +0000 Subject: [PATCH] add debug log option (#33) --- src/Jobs/PingInactiveConnections.php | 5 +++ src/Jobs/PruneStaleConnections.php | 5 +++ src/Loggers/Log.php | 31 +++++++++++++++++++ src/Pusher/Server.php | 20 ++++++++++-- .../ApiGateway/Jobs/SendToConnection.php | 4 ++- .../Reverb/Console/Commands/StartServer.php | 9 +++++- src/ServiceProvider.php | 4 +++ 7 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 src/Loggers/Log.php diff --git a/src/Jobs/PingInactiveConnections.php b/src/Jobs/PingInactiveConnections.php index 5d45f29d..746b7489 100644 --- a/src/Jobs/PingInactiveConnections.php +++ b/src/Jobs/PingInactiveConnections.php @@ -5,6 +5,7 @@ use Illuminate\Foundation\Bus\Dispatchable; use Laravel\Reverb\Contracts\ApplicationProvider; use Laravel\Reverb\Contracts\ChannelManager; +use Laravel\Reverb\Loggers\Log; use Laravel\Reverb\Pusher\Event as PusherEvent; class PingInactiveConnections @@ -16,6 +17,8 @@ class PingInactiveConnections */ public function handle(ChannelManager $channels): void { + Log::info('Pinging Inactive Connections'); + $pusher = new PusherEvent($channels); app(ApplicationProvider::class) @@ -27,6 +30,8 @@ public function handle(ChannelManager $channels): void } $pusher->ping($connection->connection()); + + Log::info('Connection Pinged', $connection->id()); } }); } diff --git a/src/Jobs/PruneStaleConnections.php b/src/Jobs/PruneStaleConnections.php index 175fa5a0..57ee1784 100644 --- a/src/Jobs/PruneStaleConnections.php +++ b/src/Jobs/PruneStaleConnections.php @@ -5,6 +5,7 @@ use Illuminate\Foundation\Bus\Dispatchable; use Laravel\Reverb\Contracts\ApplicationProvider; use Laravel\Reverb\Contracts\ChannelManager; +use Laravel\Reverb\Loggers\Log; class PruneStaleConnections { @@ -15,6 +16,8 @@ class PruneStaleConnections */ public function handle(ChannelManager $channels): void { + Log::info('Pruning Stale Connections'); + app(ApplicationProvider::class) ->all() ->each(function ($application) use ($channels) { @@ -36,6 +39,8 @@ public function handle(ChannelManager $channels): void ->unsubscribeFromAll($connection->connection()); $connection->disconnect(); + + Log::info('Connection Pruned', $connection->id()); } }); } diff --git a/src/Loggers/Log.php b/src/Loggers/Log.php new file mode 100644 index 00000000..de1f7010 --- /dev/null +++ b/src/Loggers/Log.php @@ -0,0 +1,31 @@ +$name(...$arguments); + } +} diff --git a/src/Pusher/Server.php b/src/Pusher/Server.php index 859cba87..4b57fab9 100644 --- a/src/Pusher/Server.php +++ b/src/Pusher/Server.php @@ -9,6 +9,7 @@ use Laravel\Reverb\Contracts\Connection; use Laravel\Reverb\Exceptions\InvalidOrigin; use Laravel\Reverb\Exceptions\PusherException; +use Laravel\Reverb\Loggers\Log; use Laravel\Reverb\Pusher\Event as PusherEvent; class Server @@ -29,6 +30,8 @@ public function open(Connection $connection): void $connection->touch(); $this->pusher->handle($connection, 'pusher:connection_established'); + + Log::info('Connection Established', $connection->id()); } catch (Exception $e) { $this->error($connection, $e); } @@ -39,6 +42,11 @@ public function open(Connection $connection): void */ public function message(Connection $from, string $message): void { + Log::info('Message Received', $from->id()); + Log::message($message); + + $from->touch(); + $event = json_decode($message, true); try { @@ -52,11 +60,11 @@ public function message(Connection $from, string $message): void default => ClientEvent::handle($from, $event) }; + Log::info('Message Handled', $from->id()); + } catch (Exception $e) { $this->error($from, $e); } - - $from->touch(); } /** @@ -69,6 +77,8 @@ public function close(Connection $connection): void ->unsubscribeFromAll($connection); $connection->disconnect(); + + Log::info('Connection Closed', $connection->id()); } /** @@ -79,6 +89,9 @@ public function error(Connection $connection, Exception $exception): void if ($exception instanceof PusherException) { $connection->send(json_encode($exception->payload())); + Log::error('Message from '.$connection->id().' resulted in a pusher error'); + Log::info($exception->getMessage()); + return; } @@ -89,6 +102,9 @@ public function error(Connection $connection, Exception $exception): void 'message' => 'Invalid message format', ]), ])); + + Log::error('Message from '.$connection->id().' resulted in an unknown error'); + Log::info($exception->getMessage()); } /** diff --git a/src/Servers/ApiGateway/Jobs/SendToConnection.php b/src/Servers/ApiGateway/Jobs/SendToConnection.php index 37f39e7c..3d9e79d2 100644 --- a/src/Servers/ApiGateway/Jobs/SendToConnection.php +++ b/src/Servers/ApiGateway/Jobs/SendToConnection.php @@ -6,6 +6,7 @@ use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Support\Facades\Config; +use Laravel\Reverb\Loggers\Log; use Throwable; class SendToConnection implements ShouldQueue @@ -36,7 +37,8 @@ public function handle(): void 'Data' => $this->message, ]); } catch (Throwable $e) { - // + Log::error('Unable to send message.'); + Log::info($e->getMessage()); } } } diff --git a/src/Servers/Reverb/Console/Commands/StartServer.php b/src/Servers/Reverb/Console/Commands/StartServer.php index cc45babe..d44ea710 100644 --- a/src/Servers/Reverb/Console/Commands/StartServer.php +++ b/src/Servers/Reverb/Console/Commands/StartServer.php @@ -4,8 +4,10 @@ use Illuminate\Console\Command; use Laravel\Reverb\Concerns\InteractsWithAsyncRedis; +use Laravel\Reverb\Contracts\Logger; use Laravel\Reverb\Jobs\PingInactiveConnections; use Laravel\Reverb\Jobs\PruneStaleConnections; +use Laravel\Reverb\Loggers\CliLogger; use Laravel\Reverb\Servers\Reverb\Factory as ServerFactory; use React\EventLoop\Loop; use React\EventLoop\LoopInterface; @@ -21,7 +23,8 @@ class StartServer extends Command */ protected $signature = 'reverb:start {--host=} - {--port=}'; + {--port=} + {--debug}'; /** * The console command description. @@ -35,6 +38,10 @@ class StartServer extends Command */ public function handle(): void { + if ($this->option('debug')) { + $this->laravel->instance(Logger::class, new CliLogger($this->output)); + } + $config = $this->laravel['config']['reverb.servers.reverb']; $host = $this->option('host') ?: $config['host']; $port = $this->option('port') ?: $config['port']; diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 3c120a09..2fa5ef10 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -5,6 +5,8 @@ use Illuminate\Support\ServiceProvider as BaseServiceProvider; use Laravel\Reverb\Contracts\ChannelConnectionManager; use Laravel\Reverb\Contracts\ChannelManager; +use Laravel\Reverb\Contracts\Logger; +use Laravel\Reverb\Loggers\NullLogger; class ServiceProvider extends BaseServiceProvider { @@ -31,6 +33,8 @@ public function register() __DIR__.'/../config/reverb.php', 'reverb' ); + $this->app->instance(Logger::class, new NullLogger); + $this->app->singleton(ServerManager::class); $this->initializeServer();