Fixed random 503 responses from the HealthAction when the database connection injected on it has expired

This commit is contained in:
Alejandro Celaya 2020-04-25 19:58:49 +02:00
parent 469b70d708
commit 15a8305209
3 changed files with 11 additions and 8 deletions

View File

@ -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,

View File

@ -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;
}

View File

@ -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 */