diff --git a/config/autoload/logger.local.php.dist b/config/autoload/logger.local.php.dist index 59feeb7b..cf7e4801 100644 --- a/config/autoload/logger.local.php.dist +++ b/config/autoload/logger.local.php.dist @@ -14,7 +14,7 @@ $logger = $isSwoole ? [ ], 'shlink_stdout_handler' => [ 'class' => StreamHandler::class, - 'level' => Logger::INFO, + 'level' => Logger::DEBUG, 'stream' => 'php://stdout', 'formatter' => 'dashed', ], diff --git a/module/CLI/src/Util/GeolocationDbUpdater.php b/module/CLI/src/Util/GeolocationDbUpdater.php index 34354a0e..4f7881a1 100644 --- a/module/CLI/src/Util/GeolocationDbUpdater.php +++ b/module/CLI/src/Util/GeolocationDbUpdater.php @@ -5,11 +5,11 @@ namespace Shlinkio\Shlink\CLI\Util; use Cake\Chronos\Chronos; use GeoIp2\Database\Reader; -use InvalidArgumentException; use Shlinkio\Shlink\CLI\Exception\GeolocationDbUpdateFailedException; use Shlinkio\Shlink\Common\Exception\RuntimeException; use Shlinkio\Shlink\Common\IpGeolocation\GeoLite2\DbUpdaterInterface; use Symfony\Component\Lock\Factory as Locker; +use Throwable; class GeolocationDbUpdater implements GeolocationDbUpdaterInterface { @@ -32,24 +32,33 @@ class GeolocationDbUpdater implements GeolocationDbUpdaterInterface /** * @throws GeolocationDbUpdateFailedException */ - public function checkDbUpdate(callable $mustBeUpdated = null, callable $handleProgress = null): void + public function checkDbUpdate(?callable $mustBeUpdated = null, ?callable $handleProgress = null): void { $lock = $this->locker->createLock(self::LOCK_NAME); $lock->acquire(true); // Block until lock is released try { - $meta = $this->geoLiteDbReader->metadata(); - if ($this->buildIsTooOld($meta->__get('buildEpoch'))) { - $this->downloadNewDb(true, $mustBeUpdated, $handleProgress); - } - } catch (InvalidArgumentException $e) { - // This is the exception thrown by the reader when the database file does not exist - $this->downloadNewDb(false, $mustBeUpdated, $handleProgress); + $this->downloadIfNeeded($mustBeUpdated, $handleProgress); + } catch (Throwable $e) { + throw $e; } finally { $lock->release(); } } + private function downloadIfNeeded(?callable $mustBeUpdated, ?callable $handleProgress): void + { + if (! $this->dbUpdater->databaseFileExists()) { + $this->downloadNewDb(false, $mustBeUpdated, $handleProgress); + return; + } + + $meta = $this->geoLiteDbReader->metadata(); + if ($this->buildIsTooOld($meta->__get('buildEpoch'))) { + $this->downloadNewDb(true, $mustBeUpdated, $handleProgress); + } + } + private function buildIsTooOld(int $buildTimestamp): bool { $buildDate = Chronos::createFromTimestamp($buildTimestamp); diff --git a/module/CLI/src/Util/GeolocationDbUpdaterInterface.php b/module/CLI/src/Util/GeolocationDbUpdaterInterface.php index 1d5bcf48..b27ae01d 100644 --- a/module/CLI/src/Util/GeolocationDbUpdaterInterface.php +++ b/module/CLI/src/Util/GeolocationDbUpdaterInterface.php @@ -10,5 +10,5 @@ interface GeolocationDbUpdaterInterface /** * @throws GeolocationDbUpdateFailedException */ - public function checkDbUpdate(callable $mustBeUpdated = null, callable $handleProgress = null): void; + public function checkDbUpdate(?callable $mustBeUpdated = null, ?callable $handleProgress = null): void; } diff --git a/module/Common/src/IpGeolocation/GeoLite2/DbUpdater.php b/module/Common/src/IpGeolocation/GeoLite2/DbUpdater.php index 039ce477..4efebdbe 100644 --- a/module/Common/src/IpGeolocation/GeoLite2/DbUpdater.php +++ b/module/Common/src/IpGeolocation/GeoLite2/DbUpdater.php @@ -98,4 +98,9 @@ class DbUpdater implements DbUpdaterInterface // Ignore any error produced when trying to delete temp files } } + + public function databaseFileExists(): bool + { + return $this->filesystem->exists($this->options->getDbLocation()); + } } diff --git a/module/Common/src/IpGeolocation/GeoLite2/DbUpdaterInterface.php b/module/Common/src/IpGeolocation/GeoLite2/DbUpdaterInterface.php index bf304c6f..e5694aeb 100644 --- a/module/Common/src/IpGeolocation/GeoLite2/DbUpdaterInterface.php +++ b/module/Common/src/IpGeolocation/GeoLite2/DbUpdaterInterface.php @@ -7,6 +7,8 @@ use Shlinkio\Shlink\Common\Exception\RuntimeException; interface DbUpdaterInterface { + public function databaseFileExists(): bool; + /** * @throws RuntimeException */