Skip to content

Commit

Permalink
feature: add refresh option for warmup (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
taka-oyama committed Jan 12, 2023
1 parent 519de02 commit 50c3b34
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Console/WarmupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.';

Expand All @@ -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}");
}
Expand Down
17 changes: 17 additions & 0 deletions tests/Console/WarmupCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

0 comments on commit 50c3b34

Please sign in to comment.