diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e26b013..3ac1fb53 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this * [#729](https://github.com/shlinkio/shlink/issues/729) Fixed weird error when fetching multiple visits result sets concurrently using mariadb or mysql. * [#735](https://github.com/shlinkio/shlink/issues/735) Fixed error when cleaning metadata cache during installation when APCu is enabled. +* [#677](https://github.com/shlinkio/shlink/issues/677) Fixed `/health` endpoint returning `503` fail responses when the database connection has expired. ## 2.1.3 - 2020-04-09 diff --git a/module/Rest/config/dependencies.config.php b/module/Rest/config/dependencies.config.php index 2434c88b..f6af6f85 100644 --- a/module/Rest/config/dependencies.config.php +++ b/module/Rest/config/dependencies.config.php @@ -4,7 +4,6 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Rest; -use Doctrine\DBAL\Connection; use Laminas\ServiceManager\AbstractFactory\ConfigAbstractFactory; use Laminas\ServiceManager\Factory\InvokableFactory; use Mezzio\Router\Middleware\ImplicitOptionsMiddleware; @@ -47,7 +46,7 @@ return [ ConfigAbstractFactory::class => [ ApiKeyService::class => ['em'], - Action\HealthAction::class => [Connection::class, AppOptions::class, 'Logger_Shlink'], + Action\HealthAction::class => ['em', AppOptions::class, 'Logger_Shlink'], Action\MercureInfoAction::class => [LcobucciJwtProvider::class, 'config.mercure', 'Logger_Shlink'], Action\ShortUrl\CreateShortUrlAction::class => [ Service\UrlShortener::class, diff --git a/module/Rest/src/Action/HealthAction.php b/module/Rest/src/Action/HealthAction.php index 4f3b9c64..ef1b6b88 100644 --- a/module/Rest/src/Action/HealthAction.php +++ b/module/Rest/src/Action/HealthAction.php @@ -4,7 +4,7 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Rest\Action; -use Doctrine\DBAL\Connection; +use Doctrine\ORM\EntityManagerInterface; use Laminas\Diactoros\Response\JsonResponse; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; @@ -21,13 +21,13 @@ class HealthAction extends AbstractRestAction protected const ROUTE_PATH = '/health'; protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET]; + private EntityManagerInterface $em; private AppOptions $options; - private Connection $conn; - public function __construct(Connection $conn, AppOptions $options, ?LoggerInterface $logger = null) + public function __construct(EntityManagerInterface $em, AppOptions $options, ?LoggerInterface $logger = null) { parent::__construct($logger); - $this->conn = $conn; + $this->em = $em; $this->options = $options; } @@ -39,7 +39,7 @@ class HealthAction extends AbstractRestAction public function handle(ServerRequestInterface $request): ResponseInterface { try { - $connected = $this->conn->ping(); + $connected = $this->em->getConnection()->ping(); } catch (Throwable $e) { $connected = false; } diff --git a/module/Rest/test/Action/HealthActionTest.php b/module/Rest/test/Action/HealthActionTest.php index 813fa5cc..2ec68d25 100644 --- a/module/Rest/test/Action/HealthActionTest.php +++ b/module/Rest/test/Action/HealthActionTest.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace ShlinkioTest\Shlink\Rest\Action; use Doctrine\DBAL\Connection; +use Doctrine\ORM\EntityManagerInterface; use Exception; use Laminas\Diactoros\Response\JsonResponse; use Laminas\Diactoros\ServerRequest; @@ -21,7 +22,10 @@ class HealthActionTest extends TestCase public function setUp(): void { $this->conn = $this->prophesize(Connection::class); - $this->action = new HealthAction($this->conn->reveal(), new AppOptions(['version' => '1.2.3'])); + $em = $this->prophesize(EntityManagerInterface::class); + $em->getConnection()->willReturn($this->conn->reveal()); + + $this->action = new HealthAction($em->reveal(), new AppOptions(['version' => '1.2.3'])); } /** @test */