Skip to content

Commit

Permalink
Refactor checks
Browse files Browse the repository at this point in the history
  • Loading branch information
ttrig committed Sep 23, 2021
1 parent 5016755 commit 9e6e1ef
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 15 deletions.
23 changes: 16 additions & 7 deletions src/Checks/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,40 @@ class Database extends Check

public function run(): Result
{
$connected = $checked = 0;
$connectedDatabases = 0;
$checkedDatabases = 0;
$connectionKeys = collect(config('database.connections'))->keys();

if ($connectionKeys->isEmpty()) {
return Result::unknown('No database connections found.');
}

foreach ($connectionKeys->all() as $connection) {
++$checked;
++$checkedDatabases;

try {
if (DB::connection($connection)->getPdo()) {
++$connected;
++$connectedDatabases;
}
} catch (\Exception) {
//
}
}

if ($connected === $checked) {
$result = Result::ok('Connected to all databases.');
if ($connectedDatabases === $checkedDatabases) {
$message = $checkedDatabases > 1
? "Connected to all $checkedDatabases databases."
: 'Connected to the database.';

$result = Result::ok($message);
} else {
$result = Result::critical("Connected to {$connected} of {$checked} databases.");
$message = $checkedDatabases > 1
? "Connected to $connectedDatabases of $checkedDatabases databases."
: 'Not connected to the database.';

$result = Result::critical($message);
}

return tap($result)->value($connected / $checked);
return tap($result)->value($connectedDatabases / $checkedDatabases);
}
}
6 changes: 4 additions & 2 deletions src/Checks/FailedJobs.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ public function run(): Result
$tableName = config('queue.failed.table');

try {
throw_unless(Schema::connection($connection)->hasTable($tableName), Exception::class);
throw_unless(Schema::connection($connection)->hasTable($tableName));
} catch (Exception) {
return Result::unknown("Table {$tableName} not found.");
}

if ($count = DB::connection($connection)->table($tableName)->count()) {
return tap(Result::critical("{$count} failed jobs."))->value($count);
$message = $count > 1 ? "$count failed jobs." : 'One failed job.';

return tap(Result::critical($message))->value($count);
}

return tap(Result::ok('No failed jobs.'))->value(0);
Expand Down
49 changes: 45 additions & 4 deletions tests/Checks/DatabaseCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function test_unknown_when_no_database_connections_exist()
$this->assertNull($result->value());
}

public function test_ok_when_database_connection_succeeds()
public function test_ok_when_single_database_connection_succeeds()
{
config([
'database.connections' => [
Expand All @@ -32,19 +32,60 @@ public function test_ok_when_database_connection_succeeds()

$result = (new Database())->run();

$this->assertEquals('Connected to all databases.', $result->message);
$this->assertEquals('Connected to the database.', $result->message);
$this->assertEquals(Result::OK, $result->state);
$this->assertEquals(1, $result->value());
}

public function test_critical_when_no_database_connection_fails()
public function test_ok_when_multiple_database_connections_succeeds()
{
config([
'database.connections' => [
'testing1' => [
'driver' => 'sqlite',
'database' => ':memory:',
],
'testing2' => [
'driver' => 'sqlite',
'database' => ':memory:',
],
]
]);

$result = (new Database())->run();

$this->assertEquals('Connected to all 2 databases.', $result->message);
$this->assertEquals(Result::OK, $result->state);
$this->assertEquals(1, $result->value());
}

public function test_critical_when_one_database_connection_fails()
{
config(['database.connections' => ['foobar']]);

$result = (new Database())->run();

$this->assertEquals('Connected to 0 of 1 databases.', $result->message);
$this->assertEquals('Not connected to the database.', $result->message);
$this->assertEquals(Result::CRITICAL, $result->state);
$this->assertEquals(0, $result->value());
}

public function test_critical_when_one_of_multiple_database_connections_fails()
{
config([
'database.connections' => [
'foobar',
'testing' => [
'driver' => 'sqlite',
'database' => ':memory:',
]
]
]);

$result = (new Database())->run();

$this->assertEquals('Connected to 1 of 2 databases.', $result->message);
$this->assertEquals(Result::CRITICAL, $result->state);
$this->assertEquals(0.5, $result->value());
}
}
18 changes: 16 additions & 2 deletions tests/Checks/FailedJobsCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,27 @@ public function test_ok_when_no_failed_jobs_exist()
$this->assertEquals(0, $result->value());
}

public function test_critical_when_failed_jobs_exist()
public function test_critical_when_multiple_failed_jobs_exist()
{
DB::table(config('queue.failed.table'))->insert([
['id' => 1],
['id' => 2],
]);

$result = (new FailedJobs())->run();

$this->assertEquals('2 failed jobs.', $result->message);
$this->assertEquals(Result::CRITICAL, $result->state);
$this->assertEquals(2, $result->value());
}

public function test_critical_when_one_failed_job_exist()
{
DB::table(config('queue.failed.table'))->insert(['id' => 1]);

$result = (new FailedJobs())->run();

$this->assertEquals('1 failed jobs.', $result->message);
$this->assertEquals('One failed job.', $result->message);
$this->assertEquals(Result::CRITICAL, $result->state);
$this->assertEquals(1, $result->value());
}
Expand Down

0 comments on commit 9e6e1ef

Please sign in to comment.