mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Added check to ensure DB connection is gracefully recovered on swoole task workers
This commit is contained in:
parent
f79a369884
commit
28e0fb049b
@ -9,6 +9,7 @@ use Psr\EventDispatcher\EventDispatcherInterface;
|
|||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
use Shlinkio\Shlink\CLI\Exception\GeolocationDbUpdateFailedException;
|
use Shlinkio\Shlink\CLI\Exception\GeolocationDbUpdateFailedException;
|
||||||
use Shlinkio\Shlink\CLI\Util\GeolocationDbUpdaterInterface;
|
use Shlinkio\Shlink\CLI\Util\GeolocationDbUpdaterInterface;
|
||||||
|
use Shlinkio\Shlink\Common\Doctrine\ReopeningEntityManager;
|
||||||
use Shlinkio\Shlink\Core\Entity\Visit;
|
use Shlinkio\Shlink\Core\Entity\Visit;
|
||||||
use Shlinkio\Shlink\Core\Entity\VisitLocation;
|
use Shlinkio\Shlink\Core\Entity\VisitLocation;
|
||||||
use Shlinkio\Shlink\IpGeolocation\Exception\WrongIpException;
|
use Shlinkio\Shlink\IpGeolocation\Exception\WrongIpException;
|
||||||
@ -41,22 +42,35 @@ class LocateShortUrlVisit
|
|||||||
|
|
||||||
public function __invoke(ShortUrlVisited $shortUrlVisited): void
|
public function __invoke(ShortUrlVisited $shortUrlVisited): void
|
||||||
{
|
{
|
||||||
|
// FIXME Temporarily handling DB connection reset here to fix https://github.com/shlinkio/shlink/issues/717
|
||||||
|
// Remove when https://github.com/shlinkio/shlink-event-dispatcher/issues/23 is implemented
|
||||||
|
if ($this->em instanceof ReopeningEntityManager) {
|
||||||
|
$this->em->open();
|
||||||
|
}
|
||||||
|
|
||||||
$visitId = $shortUrlVisited->visitId();
|
$visitId = $shortUrlVisited->visitId();
|
||||||
|
|
||||||
/** @var Visit|null $visit */
|
try {
|
||||||
$visit = $this->em->find(Visit::class, $visitId);
|
/** @var Visit|null $visit */
|
||||||
if ($visit === null) {
|
$visit = $this->em->find(Visit::class, $visitId);
|
||||||
$this->logger->warning('Tried to locate visit with id "{visitId}", but it does not exist.', [
|
if ($visit === null) {
|
||||||
'visitId' => $visitId,
|
$this->logger->warning('Tried to locate visit with id "{visitId}", but it does not exist.', [
|
||||||
]);
|
'visitId' => $visitId,
|
||||||
return;
|
]);
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if ($this->downloadOrUpdateGeoLiteDb($visitId)) {
|
if ($this->downloadOrUpdateGeoLiteDb($visitId)) {
|
||||||
$this->locateVisit($visitId, $shortUrlVisited->originalIpAddress(), $visit);
|
$this->locateVisit($visitId, $shortUrlVisited->originalIpAddress(), $visit);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->eventDispatcher->dispatch(new VisitLocated($visitId));
|
$this->eventDispatcher->dispatch(new VisitLocated($visitId));
|
||||||
|
} finally {
|
||||||
|
// FIXME Temporarily handling DB connection reset here to fix https://github.com/shlinkio/shlink/issues/717
|
||||||
|
// Remove when https://github.com/shlinkio/shlink-event-dispatcher/issues/23 is implemented
|
||||||
|
$this->em->getConnection()->close();
|
||||||
|
$this->em->clear();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function downloadOrUpdateGeoLiteDb(string $visitId): bool
|
private function downloadOrUpdateGeoLiteDb(string $visitId): bool
|
||||||
|
@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace ShlinkioTest\Shlink\Core\EventDispatcher;
|
namespace ShlinkioTest\Shlink\Core\EventDispatcher;
|
||||||
|
|
||||||
|
use Doctrine\DBAL\Connection;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Prophecy\Argument;
|
use Prophecy\Argument;
|
||||||
@ -37,6 +38,10 @@ class LocateShortUrlVisitTest extends TestCase
|
|||||||
{
|
{
|
||||||
$this->ipLocationResolver = $this->prophesize(IpLocationResolverInterface::class);
|
$this->ipLocationResolver = $this->prophesize(IpLocationResolverInterface::class);
|
||||||
$this->em = $this->prophesize(EntityManagerInterface::class);
|
$this->em = $this->prophesize(EntityManagerInterface::class);
|
||||||
|
$conn = $this->prophesize(Connection::class);
|
||||||
|
$this->em->getConnection()->willReturn($conn->reveal());
|
||||||
|
$this->em->clear()->will(function (): void {
|
||||||
|
});
|
||||||
$this->logger = $this->prophesize(LoggerInterface::class);
|
$this->logger = $this->prophesize(LoggerInterface::class);
|
||||||
$this->dbUpdater = $this->prophesize(GeolocationDbUpdaterInterface::class);
|
$this->dbUpdater = $this->prophesize(GeolocationDbUpdaterInterface::class);
|
||||||
$this->eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
|
$this->eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
|
||||||
|
Loading…
Reference in New Issue
Block a user