From 50c3b34659f4307e40eba47f571b8d6e6e218e7a Mon Sep 17 00:00:00 2001 From: Takayasu Oyama Date: Tue, 13 Dec 2022 11:15:18 +0900 Subject: [PATCH] feature: add refresh option for warmup (#57) --- src/Console/WarmupCommand.php | 10 +++++++++- tests/Console/WarmupCommandTest.php | 17 +++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Console/WarmupCommand.php b/src/Console/WarmupCommand.php index 29b218ed..a5b19877 100644 --- a/src/Console/WarmupCommand.php +++ b/src/Console/WarmupCommand.php @@ -23,7 +23,8 @@ class WarmupCommand extends Command { - protected $signature = 'spanner:warmup {connections?* : The database connections to be warmed up}'; + protected $signature = 'spanner:warmup {connections?* : The database connections to be warmed up} + {--refresh : Will clear all existing sessions first.}'; protected $description = 'Warmup Spanner\'s Session Pool.'; @@ -39,9 +40,16 @@ public function handle(DatabaseManager $db): void static fn(string $name): bool => config("database.connections.{$name}.driver") === 'spanner', ); + $refresh = (bool)($this->option('refresh') ?? false); + foreach ($spannerConnectionNames as $name) { $connection = $db->connection($name); if ($connection instanceof SpannerConnection) { + if ($refresh) { + $this->info("Cleared all existing sessions for {$name}"); + $connection->clearSessionPool(); + } + $count = $connection->warmupSessionPool(); $this->info("Warmed up {$count} sessions for {$name}"); } diff --git a/tests/Console/WarmupCommandTest.php b/tests/Console/WarmupCommandTest.php index fdac98d2..4ffdaf35 100644 --- a/tests/Console/WarmupCommandTest.php +++ b/tests/Console/WarmupCommandTest.php @@ -60,4 +60,21 @@ public function test_with_args(): void ->assertSuccessful() ->run(); } + + public function test_with_refresh(): void + { + foreach (['main', 'alternative'] as $name) { + $conn = $this->getConnection($name); + assert($conn instanceof Connection); + $this->setUpDatabaseOnce($conn); + } + + $this->artisan('spanner:warmup', ['--refresh' => true]) + ->expectsOutputToContain('Cleared all existing sessions for main') + ->expectsOutputToContain('Cleared all existing sessions for alternative') + ->expectsOutputToContain("Warmed up 1 sessions for main") + ->expectsOutputToContain("Warmed up 1 sessions for alternative") + ->assertSuccessful() + ->run(); + } }