From e715a0fb6f15a9132d58559ba95ebab1664b9cc6 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Mon, 16 Dec 2024 09:23:30 +0100 Subject: [PATCH] Track reason for which a geolocation db download was attempted --- ...Geolocation.Entity.GeolocationDbUpdate.php | 5 +++++ .../Core/migrations/Version20241212131058.php | 1 + .../Entity/GeolocationDbUpdate.php | 5 +++-- .../src/Geolocation/GeolocationDbUpdater.php | 19 ++++++++++++++----- 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Geolocation.Entity.GeolocationDbUpdate.php b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Geolocation.Entity.GeolocationDbUpdate.php index e7f10ca1..7b60abdc 100644 --- a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Geolocation.Entity.GeolocationDbUpdate.php +++ b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Geolocation.Entity.GeolocationDbUpdate.php @@ -46,6 +46,11 @@ return static function (ClassMetadata $metadata, array $emConfig): void { ->nullable() ->build(); + fieldWithUtf8Charset($builder->createField('reason', Types::STRING), $emConfig) + ->columnName('reason') + ->length(1024) + ->build(); + fieldWithUtf8Charset($builder->createField('filesystemId', Types::STRING), $emConfig) ->columnName('filesystem_id') ->length(512) diff --git a/module/Core/migrations/Version20241212131058.php b/module/Core/migrations/Version20241212131058.php index e27958f6..23e61803 100644 --- a/module/Core/migrations/Version20241212131058.php +++ b/module/Core/migrations/Version20241212131058.php @@ -38,6 +38,7 @@ final class Version20241212131058 extends AbstractMigration ]); $table->addColumn('filesystem_id', Types::STRING, ['length' => 512]); + $table->addColumn('reason', Types::STRING, ['length' => 1024]); $table->addColumn('error', Types::STRING, [ 'length' => 1024, 'default' => null, diff --git a/module/Core/src/Geolocation/Entity/GeolocationDbUpdate.php b/module/Core/src/Geolocation/Entity/GeolocationDbUpdate.php index 35a9697d..42cdfa4b 100644 --- a/module/Core/src/Geolocation/Entity/GeolocationDbUpdate.php +++ b/module/Core/src/Geolocation/Entity/GeolocationDbUpdate.php @@ -13,6 +13,7 @@ class GeolocationDbUpdate extends AbstractEntity { private function __construct( private readonly string $filesystemId, + private readonly string $reason, private GeolocationDbUpdateStatus $status = GeolocationDbUpdateStatus::IN_PROGRESS, private readonly Chronos $dateCreated = new Chronos(), private Chronos $dateUpdated = new Chronos(), @@ -20,9 +21,9 @@ class GeolocationDbUpdate extends AbstractEntity ) { } - public static function forFilesystemId(string|null $filesystemId = null): self + public static function withReason(string $reason, string|null $filesystemId = null): self { - return new self($filesystemId ?? self::currentFilesystemId()); + return new self($reason, $filesystemId ?? self::currentFilesystemId()); } public static function currentFilesystemId(): string diff --git a/module/Core/src/Geolocation/GeolocationDbUpdater.php b/module/Core/src/Geolocation/GeolocationDbUpdater.php index 662e6b7a..7be1cd56 100644 --- a/module/Core/src/Geolocation/GeolocationDbUpdater.php +++ b/module/Core/src/Geolocation/GeolocationDbUpdater.php @@ -89,12 +89,20 @@ readonly class GeolocationDbUpdater implements GeolocationDbUpdaterInterface } // Try to download if: - // - There are no attempts or the database file does not exist + // - There are no attempts tracked + // - The database file does not exist // - Last update errored (and implicitly, the max amount of consecutive errors has not been reached) // - Most recent attempt is older than 30 days (and implicitly, successful) - $olderDbExists = $mostRecentDownload !== null && $this->dbUpdater->databaseFileExists(); - if (! $olderDbExists || $lastAttemptIsError || $mostRecentDownload->isOlderThan(days: 30)) { - return $this->downloadAndTrackUpdate($downloadProgressHandler, $olderDbExists); + $reasonMatch = match (true) { + $mostRecentDownload === null => [false, 'No download attempts tracked for this instance'], + $this->dbUpdater->databaseFileExists() => [false, 'Geolocation db file does not exist'], + $lastAttemptIsError => [true, 'Max consecutive errors not reached'], + $mostRecentDownload->isOlderThan(days: 30) => [true, 'Last successful attempt'], + default => null, + }; + if ($reasonMatch !== null) { + [$olderDbExists, $reason] = $reasonMatch; + return $this->downloadAndTrackUpdate($downloadProgressHandler, $olderDbExists, $reason); } return GeolocationResult::DB_IS_UP_TO_DATE; @@ -106,8 +114,9 @@ readonly class GeolocationDbUpdater implements GeolocationDbUpdaterInterface private function downloadAndTrackUpdate( GeolocationDownloadProgressHandlerInterface|null $downloadProgressHandler, bool $olderDbExists, + string $reason, ): GeolocationResult { - $dbUpdate = GeolocationDbUpdate::forFilesystemId(); + $dbUpdate = GeolocationDbUpdate::withReason($reason); $this->em->persist($dbUpdate); $this->em->flush();