diff --git a/module/CLI/src/Util/GeolocationDbUpdater.php b/module/CLI/src/Util/GeolocationDbUpdater.php index b8f5b756..ebdeb573 100644 --- a/module/CLI/src/Util/GeolocationDbUpdater.php +++ b/module/CLI/src/Util/GeolocationDbUpdater.php @@ -32,13 +32,13 @@ class GeolocationDbUpdater implements GeolocationDbUpdaterInterface /** * @throws GeolocationDbUpdateFailedException */ - public function checkDbUpdate(?callable $mustBeUpdated = null, ?callable $handleProgress = null): void + public function checkDbUpdate(?callable $beforeDownload = null, ?callable $handleProgress = null): void { $lock = $this->locker->createLock(self::LOCK_NAME); $lock->acquire(true); // Block until lock is released try { - $this->downloadIfNeeded($mustBeUpdated, $handleProgress); + $this->downloadIfNeeded($beforeDownload, $handleProgress); } finally { $lock->release(); } @@ -47,26 +47,26 @@ class GeolocationDbUpdater implements GeolocationDbUpdaterInterface /** * @throws GeolocationDbUpdateFailedException */ - private function downloadIfNeeded(?callable $mustBeUpdated, ?callable $handleProgress): void + private function downloadIfNeeded(?callable $beforeDownload, ?callable $handleProgress): void { if (! $this->dbUpdater->databaseFileExists()) { - $this->downloadNewDb(false, $mustBeUpdated, $handleProgress); + $this->downloadNewDb(false, $beforeDownload, $handleProgress); return; } $meta = $this->geoLiteDbReader->metadata(); if ($this->buildIsTooOld($meta)) { - $this->downloadNewDb(true, $mustBeUpdated, $handleProgress); + $this->downloadNewDb(true, $beforeDownload, $handleProgress); } } /** * @throws GeolocationDbUpdateFailedException */ - private function downloadNewDb(bool $olderDbExists, ?callable $mustBeUpdated, ?callable $handleProgress): void + private function downloadNewDb(bool $olderDbExists, ?callable $beforeDownload, ?callable $handleProgress): void { - if ($mustBeUpdated !== null) { - $mustBeUpdated($olderDbExists); + if ($beforeDownload !== null) { + $beforeDownload($olderDbExists); } try { diff --git a/module/CLI/src/Util/GeolocationDbUpdaterInterface.php b/module/CLI/src/Util/GeolocationDbUpdaterInterface.php index 1eda5123..714f6a11 100644 --- a/module/CLI/src/Util/GeolocationDbUpdaterInterface.php +++ b/module/CLI/src/Util/GeolocationDbUpdaterInterface.php @@ -11,5 +11,5 @@ interface GeolocationDbUpdaterInterface /** * @throws GeolocationDbUpdateFailedException */ - public function checkDbUpdate(?callable $mustBeUpdated = null, ?callable $handleProgress = null): void; + public function checkDbUpdate(?callable $beforeDownload = null, ?callable $handleProgress = null): void; } diff --git a/module/Core/src/EventDispatcher/LocateVisit.php b/module/Core/src/EventDispatcher/LocateVisit.php index 32da6060..d02f8be7 100644 --- a/module/Core/src/EventDispatcher/LocateVisit.php +++ b/module/Core/src/EventDispatcher/LocateVisit.php @@ -7,31 +7,28 @@ namespace Shlinkio\Shlink\Core\EventDispatcher; use Doctrine\ORM\EntityManagerInterface; use Psr\EventDispatcher\EventDispatcherInterface; use Psr\Log\LoggerInterface; -use Shlinkio\Shlink\CLI\Exception\GeolocationDbUpdateFailedException; -use Shlinkio\Shlink\CLI\Util\GeolocationDbUpdaterInterface; use Shlinkio\Shlink\Core\Entity\Visit; use Shlinkio\Shlink\Core\Entity\VisitLocation; use Shlinkio\Shlink\Core\EventDispatcher\Event\UrlVisited; use Shlinkio\Shlink\Core\EventDispatcher\Event\VisitLocated; use Shlinkio\Shlink\IpGeolocation\Exception\WrongIpException; +use Shlinkio\Shlink\IpGeolocation\GeoLite2\DbUpdaterInterface; use Shlinkio\Shlink\IpGeolocation\Model\Location; use Shlinkio\Shlink\IpGeolocation\Resolver\IpLocationResolverInterface; -use function sprintf; - class LocateVisit { private IpLocationResolverInterface $ipLocationResolver; private EntityManagerInterface $em; private LoggerInterface $logger; - private GeolocationDbUpdaterInterface $dbUpdater; + private DbUpdaterInterface $dbUpdater; private EventDispatcherInterface $eventDispatcher; public function __construct( IpLocationResolverInterface $ipLocationResolver, EntityManagerInterface $em, LoggerInterface $logger, - GeolocationDbUpdaterInterface $dbUpdater, + DbUpdaterInterface $dbUpdater, EventDispatcherInterface $eventDispatcher ) { $this->ipLocationResolver = $ipLocationResolver; @@ -54,33 +51,37 @@ class LocateVisit return; } - if ($this->downloadOrUpdateGeoLiteDb($visitId)) { - $this->locateVisit($visitId, $shortUrlVisited->originalIpAddress(), $visit); + if (! $this->dbUpdater->databaseFileExists()) { + $this->logger->warning('Tried to locate visit with id "{visitId}", but a GeoLite2 db was not found.', [ + 'visitId' => $visitId, + ]); + return; } + $this->locateVisit($visitId, $shortUrlVisited->originalIpAddress(), $visit); $this->eventDispatcher->dispatch(new VisitLocated($visitId)); } - private function downloadOrUpdateGeoLiteDb(string $visitId): bool - { - try { - $this->dbUpdater->checkDbUpdate(function (bool $olderDbExists): void { - $this->logger->notice(sprintf('%s GeoLite2 database...', $olderDbExists ? 'Updating' : 'Downloading')); - }); - } catch (GeolocationDbUpdateFailedException $e) { - if (! $e->olderDbExists()) { - $this->logger->error( - 'GeoLite2 database download failed. It is not possible to locate visit with id {visitId}. {e}', - ['e' => $e, 'visitId' => $visitId], - ); - return false; - } - - $this->logger->warning('GeoLite2 database update failed. Proceeding with old version. {e}', ['e' => $e]); - } - - return true; - } +// private function downloadOrUpdateGeoLiteDb(string $visitId): bool +// { +// try { +// $this->dbUpdater->checkDbUpdate(function (bool $olderDbExists): void { +// $this->logger->notice(sprintf('%s GeoLite2 database...', $olderDbExists ? 'Updating' : 'Downloading')); +// }); +// } catch (GeolocationDbUpdateFailedException $e) { +// if (! $e->olderDbExists()) { +// $this->logger->error( +// 'GeoLite2 database download failed. It is not possible to locate visit with id {visitId}. {e}', +// ['e' => $e, 'visitId' => $visitId], +// ); +// return false; +// } +// +// $this->logger->warning('GeoLite2 database update failed. Proceeding with old version. {e}', ['e' => $e]); +// } +// +// return true; +// } private function locateVisit(string $visitId, ?string $originalIpAddress, Visit $visit): void {