Ensured events triggered as a result of a new visit are never skipped

This commit is contained in:
Alejandro Celaya 2021-04-08 14:09:26 +02:00
parent 86230d9bf3
commit 7b4456e73f
6 changed files with 17 additions and 11 deletions

3
.gitignore vendored
View File

@ -6,8 +6,7 @@ composer.phar
vendor/ vendor/
data/database.sqlite data/database.sqlite
data/shlink-tests.db data/shlink-tests.db
data/GeoLite2-City.mmdb data/GeoLite2-City.*
data/GeoLite2-City.mmdb.*
docs/swagger-ui* docs/swagger-ui*
docs/mercure.html docs/mercure.html
docker-compose.override.yml docker-compose.override.yml

View File

@ -22,6 +22,7 @@ use Shlinkio\Shlink\Core\Visit\VisitsStatsHelperInterface;
use Shlinkio\Shlink\IpGeolocation\Model\Location; use Shlinkio\Shlink\IpGeolocation\Model\Location;
use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait; use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait;
use Symfony\Component\Console\Tester\CommandTester; use Symfony\Component\Console\Tester\CommandTester;
use function sprintf; use function sprintf;
class GetVisitsCommandTest extends TestCase class GetVisitsCommandTest extends TestCase

View File

@ -52,6 +52,12 @@ class LocateVisit
return; return;
} }
$this->locateVisit($visitId, $shortUrlVisited->originalIpAddress(), $visit);
$this->eventDispatcher->dispatch(new VisitLocated($visitId));
}
private function locateVisit(string $visitId, ?string $originalIpAddress, Visit $visit): void
{
if (! $this->dbUpdater->databaseFileExists()) { if (! $this->dbUpdater->databaseFileExists()) {
$this->logger->warning('Tried to locate visit with id "{visitId}", but a GeoLite2 db was not found.', [ $this->logger->warning('Tried to locate visit with id "{visitId}", but a GeoLite2 db was not found.', [
'visitId' => $visitId, 'visitId' => $visitId,
@ -59,12 +65,6 @@ class LocateVisit
return; return;
} }
$this->locateVisit($visitId, $shortUrlVisited->originalIpAddress(), $visit);
$this->eventDispatcher->dispatch(new VisitLocated($visitId));
}
private function locateVisit(string $visitId, ?string $originalIpAddress, Visit $visit): void
{
$isLocatable = $originalIpAddress !== null || $visit->isLocatable(); $isLocatable = $originalIpAddress !== null || $visit->isLocatable();
$addr = $originalIpAddress ?? $visit->getRemoteAddr(); $addr = $originalIpAddress ?? $visit->getRemoteAddr();

View File

@ -26,11 +26,13 @@ class UpdateGeoLiteDb
$beforeDownload = fn (bool $olderDbExists) => $this->logger->notice( $beforeDownload = fn (bool $olderDbExists) => $this->logger->notice(
sprintf('%s GeoLite2 db file...', $olderDbExists ? 'Updating' : 'Downloading'), sprintf('%s GeoLite2 db file...', $olderDbExists ? 'Updating' : 'Downloading'),
); );
$handleProgress = function (int $total, int $downloaded, bool $olderDbExists): void { $messageLogged = false;
if ($total > $downloaded) { $handleProgress = function (int $total, int $downloaded, bool $olderDbExists) use (&$messageLogged): void {
if ($messageLogged || $total > $downloaded) {
return; return;
} }
$messageLogged = true;
$this->logger->notice(sprintf('Finished %s GeoLite2 db file', $olderDbExists ? 'updating' : 'downloading')); $this->logger->notice(sprintf('Finished %s GeoLite2 db file', $olderDbExists ? 'updating' : 'downloading'));
}; };

View File

@ -97,7 +97,7 @@ class LocateVisitTest extends TestCase
$this->em->flush()->shouldNotHaveBeenCalled(); $this->em->flush()->shouldNotHaveBeenCalled();
$this->ipLocationResolver->resolveIpLocation(Argument::cetera())->shouldNotHaveBeenCalled(); $this->ipLocationResolver->resolveIpLocation(Argument::cetera())->shouldNotHaveBeenCalled();
$logWarning->shouldHaveBeenCalled(); $logWarning->shouldHaveBeenCalled();
$dispatch->shouldNotHaveBeenCalled(); $dispatch->shouldHaveBeenCalledOnce();
} }
/** @test */ /** @test */

View File

@ -84,6 +84,10 @@ class UpdateGeoLiteDbTest extends TestCase
$checkDbUpdate = $this->dbUpdater->checkDbUpdate(Argument::cetera())->will( $checkDbUpdate = $this->dbUpdater->checkDbUpdate(Argument::cetera())->will(
function (array $args) use ($total, $downloaded, $oldDbExists): void { function (array $args) use ($total, $downloaded, $oldDbExists): void {
[, $secondCallback] = $args; [, $secondCallback] = $args;
// Invoke several times to ensure the log is printed only once
$secondCallback($total, $downloaded, $oldDbExists);
$secondCallback($total, $downloaded, $oldDbExists);
$secondCallback($total, $downloaded, $oldDbExists); $secondCallback($total, $downloaded, $oldDbExists);
}, },
); );