fix/164-health-and-logging-resilience #167
3 changed files with 66 additions and 0 deletions
23
app/Listeners/VerifyDatabaseIsReachable.php
Normal file
23
app/Listeners/VerifyDatabaseIsReachable.php
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use Illuminate\Foundation\Events\DiagnosingHealth;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
|
||||
class VerifyDatabaseIsReachable
|
||||
{
|
||||
/**
|
||||
* A container whose database is unreachable is not healthy, however well it serves /up.
|
||||
*/
|
||||
public function handle(DiagnosingHealth $event): void
|
||||
{
|
||||
try {
|
||||
DB::connection()->select('select 1');
|
||||
} catch (Throwable $e) {
|
||||
throw new RuntimeException('The database is not reachable: '.$e->getMessage(), previous: $e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -9,6 +9,7 @@
|
|||
use App\Events\RouteActivated;
|
||||
use App\Events\RouteArticleApproved;
|
||||
use Illuminate\Contracts\Debug\ExceptionHandler;
|
||||
use Illuminate\Foundation\Events\DiagnosingHealth;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use RuntimeException;
|
||||
|
|
@ -28,6 +29,7 @@ public static function eventProvider(): array
|
|||
'NewArticleFetched' => [NewArticleFetched::class],
|
||||
'RouteActivated' => [RouteActivated::class],
|
||||
'RouteArticleApproved' => [RouteArticleApproved::class],
|
||||
'DiagnosingHealth' => [DiagnosingHealth::class],
|
||||
];
|
||||
}
|
||||
|
||||
|
|
|
|||
41
tests/Feature/HealthEndpointTest.php
Normal file
41
tests/Feature/HealthEndpointTest.php
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Listeners\VerifyDatabaseIsReachable;
|
||||
use Illuminate\Foundation\Events\DiagnosingHealth;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use RuntimeException;
|
||||
use Tests\TestCase;
|
||||
|
||||
class HealthEndpointTest extends TestCase
|
||||
{
|
||||
public function test_up_returns_ok_when_the_database_is_reachable(): void
|
||||
{
|
||||
$this->get('/up')->assertOk();
|
||||
}
|
||||
|
||||
public function test_up_fails_when_the_database_is_unreachable(): void
|
||||
{
|
||||
DB::shouldReceive('connection')->andThrow(new RuntimeException('getaddrinfo for db failed'));
|
||||
|
||||
$this->get('/up')->assertServerError();
|
||||
}
|
||||
|
||||
public function test_the_listener_throws_when_the_connection_fails(): void
|
||||
{
|
||||
DB::shouldReceive('connection')->andThrow(new RuntimeException('getaddrinfo for db failed'));
|
||||
|
||||
$this->expectException(RuntimeException::class);
|
||||
$this->expectExceptionMessage('The database is not reachable');
|
||||
|
||||
app(VerifyDatabaseIsReachable::class)->handle(new DiagnosingHealth);
|
||||
}
|
||||
|
||||
public function test_the_listener_passes_when_the_connection_works(): void
|
||||
{
|
||||
app(VerifyDatabaseIsReachable::class)->handle(new DiagnosingHealth);
|
||||
|
||||
$this->addToAssertionCount(1);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue