Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add skip-on-error option to spanner:warmup command #70

Merged
merged 4 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ services:
depends_on:
- emulator
emulator:
image: gcr.io/cloud-spanner-emulator/emulator
image: gcr.io/cloud-spanner-emulator/emulator:1.4.9
18 changes: 15 additions & 3 deletions src/Console/WarmupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@
namespace Colopl\Spanner\Console;

use Colopl\Spanner\Connection as SpannerConnection;
use Google\Cloud\Core\Exception\ServiceException;
use Illuminate\Console\Command;
use Illuminate\Database\DatabaseManager;

class WarmupCommand extends Command
{
protected $signature = 'spanner:warmup {connections?* : The database connections to be warmed up}
{--refresh : Will clear all existing sessions first.}';
{--refresh : Will clear all existing sessions first.}
{--skip-on-error : Will skip the connection if error is thrown.}';

protected $description = 'Warmup Spanner\'s Session Pool.';
protected $description = "Warmup Spanner's Session Pool.";

public function handle(DatabaseManager $db): void
taka-oyama marked this conversation as resolved.
Show resolved Hide resolved
{
Expand All @@ -41,17 +43,27 @@ public function handle(DatabaseManager $db): void
);

$refresh = (bool)($this->option('refresh') ?? false);
$skipOnError = (bool)($this->option('skip-on-error') ?? false);

foreach ($spannerConnectionNames as $name) {
$connection = $db->connection($name);
if ($connection instanceof SpannerConnection) {

if (!$connection instanceof SpannerConnection) {
continue;
}

try {
if ($refresh) {
$this->info("Cleared all existing sessions for {$name}");
$connection->clearSessionPool();
}

$count = $connection->warmupSessionPool();
$this->info("Warmed up {$count} sessions for {$name}");
} catch (ServiceException $e) {
$skipOnError
? $this->warn("Skipping warmup for {$name} due to " . $e::class . ": {$e->getMessage()}")
: throw $e;
}
}
}
Expand Down
40 changes: 40 additions & 0 deletions tests/Console/WarmupCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
namespace Colopl\Spanner\Tests\Console;

use Colopl\Spanner\Connection;
use Google\Cloud\Core\Exception\NotFoundException;
use Illuminate\Support\Facades\DB;

class
WarmupCommandTest extends TestCase
Expand Down Expand Up @@ -77,4 +79,42 @@ public function test_with_refresh(): void
->assertSuccessful()
->run();
}

public function test_with_missing_instance(): void
{
$this->expectException(NotFoundException::class);
$this->expectExceptionMessageMatches('/Instance not found:/');

config()->set('database.connections.none', [
'driver' => 'spanner',
'instance' => 'nil',
'database' => 'nil',
]);

try {
$this->artisan('spanner:warmup', ['connections' => 'none'])
->assertFailed()
->run();
} finally {
// teardown で truncate が実行されないようにする
DB::purge('none');
}
}

public function test_with_skip_on_error(): void
{
config()->set('database.connections.none', [
'driver' => 'spanner',
'instance' => 'nil',
'database' => 'nil',
]);

$this->artisan('spanner:warmup', ['connections' => 'none', '--skip-on-error' => true])
->expectsOutputToContain('Skipping warmup for none')
->assertSuccessful()
->run();

// teardown で truncate が実行されないようにする
DB::purge('none');
}
}