Merge pull request #738 from acelaya-forks/feature/health-fix

Feature/health fix
This commit is contained in:
Alejandro Celaya 2020-04-25 20:07:09 +02:00 committed by GitHub
commit 704958994d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 8 deletions

View File

@ -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. * [#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. * [#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 ## 2.1.3 - 2020-04-09

View File

@ -4,7 +4,6 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Rest; namespace Shlinkio\Shlink\Rest;
use Doctrine\DBAL\Connection;
use Laminas\ServiceManager\AbstractFactory\ConfigAbstractFactory; use Laminas\ServiceManager\AbstractFactory\ConfigAbstractFactory;
use Laminas\ServiceManager\Factory\InvokableFactory; use Laminas\ServiceManager\Factory\InvokableFactory;
use Mezzio\Router\Middleware\ImplicitOptionsMiddleware; use Mezzio\Router\Middleware\ImplicitOptionsMiddleware;
@ -47,7 +46,7 @@ return [
ConfigAbstractFactory::class => [ ConfigAbstractFactory::class => [
ApiKeyService::class => ['em'], 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\MercureInfoAction::class => [LcobucciJwtProvider::class, 'config.mercure', 'Logger_Shlink'],
Action\ShortUrl\CreateShortUrlAction::class => [ Action\ShortUrl\CreateShortUrlAction::class => [
Service\UrlShortener::class, Service\UrlShortener::class,

View File

@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Action; namespace Shlinkio\Shlink\Rest\Action;
use Doctrine\DBAL\Connection; use Doctrine\ORM\EntityManagerInterface;
use Laminas\Diactoros\Response\JsonResponse; use Laminas\Diactoros\Response\JsonResponse;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
@ -21,13 +21,13 @@ class HealthAction extends AbstractRestAction
protected const ROUTE_PATH = '/health'; protected const ROUTE_PATH = '/health';
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET]; protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET];
private EntityManagerInterface $em;
private AppOptions $options; 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); parent::__construct($logger);
$this->conn = $conn; $this->em = $em;
$this->options = $options; $this->options = $options;
} }
@ -39,7 +39,7 @@ class HealthAction extends AbstractRestAction
public function handle(ServerRequestInterface $request): ResponseInterface public function handle(ServerRequestInterface $request): ResponseInterface
{ {
try { try {
$connected = $this->conn->ping(); $connected = $this->em->getConnection()->ping();
} catch (Throwable $e) { } catch (Throwable $e) {
$connected = false; $connected = false;
} }

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Action; namespace ShlinkioTest\Shlink\Rest\Action;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManagerInterface;
use Exception; use Exception;
use Laminas\Diactoros\Response\JsonResponse; use Laminas\Diactoros\Response\JsonResponse;
use Laminas\Diactoros\ServerRequest; use Laminas\Diactoros\ServerRequest;
@ -21,7 +22,10 @@ class HealthActionTest extends TestCase
public function setUp(): void public function setUp(): void
{ {
$this->conn = $this->prophesize(Connection::class); $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 */ /** @test */