mirror of
https://github.com/shlinkio/shlink.git
synced 2024-12-22 15:13:59 -06:00
Created new listener to update the GeoLite db after a visit occurs
This commit is contained in:
parent
c4718e7523
commit
74ea5969be
@ -181,7 +181,7 @@ class LocateVisitsCommand extends AbstractLockedCommand implements VisitGeolocat
|
||||
try {
|
||||
$this->dbUpdater->checkDbUpdate(function (bool $olderDbExists): void {
|
||||
$this->io->writeln(
|
||||
sprintf('<fg=blue>%s GeoLite2 database...</>', $olderDbExists ? 'Updating' : 'Downloading'),
|
||||
sprintf('<fg=blue>%s GeoLite2 db file...</>', $olderDbExists ? 'Updating' : 'Downloading'),
|
||||
);
|
||||
$this->progressBar = new ProgressBar($this->io);
|
||||
}, function (int $total, int $downloaded): void {
|
||||
|
@ -227,7 +227,7 @@ class LocateVisitsCommandTest extends TestCase
|
||||
$output = $this->commandTester->getDisplay();
|
||||
|
||||
self::assertStringContainsString(
|
||||
sprintf('%s GeoLite2 database...', $olderDbExists ? 'Updating' : 'Downloading'),
|
||||
sprintf('%s GeoLite2 db file...', $olderDbExists ? 'Updating' : 'Downloading'),
|
||||
$output,
|
||||
);
|
||||
self::assertStringContainsString($expectedMessage, $output);
|
||||
|
@ -6,6 +6,7 @@ namespace Shlinkio\Shlink\Core;
|
||||
|
||||
use Laminas\ServiceManager\AbstractFactory\ConfigAbstractFactory;
|
||||
use Psr\EventDispatcher\EventDispatcherInterface;
|
||||
use Shlinkio\Shlink\CLI\Util\GeolocationDbUpdater;
|
||||
use Shlinkio\Shlink\IpGeolocation\GeoLite2\DbUpdater;
|
||||
use Shlinkio\Shlink\IpGeolocation\Resolver\IpLocationResolverInterface;
|
||||
use Symfony\Component\Mercure\Hub;
|
||||
@ -22,7 +23,7 @@ return [
|
||||
EventDispatcher\Event\VisitLocated::class => [
|
||||
EventDispatcher\NotifyVisitToMercure::class,
|
||||
EventDispatcher\NotifyVisitToWebHooks::class,
|
||||
// EventDispatcher\UpdateGeoLiteDb::class,
|
||||
EventDispatcher\UpdateGeoLiteDb::class,
|
||||
],
|
||||
],
|
||||
],
|
||||
@ -32,6 +33,7 @@ return [
|
||||
EventDispatcher\LocateVisit::class => ConfigAbstractFactory::class,
|
||||
EventDispatcher\NotifyVisitToWebHooks::class => ConfigAbstractFactory::class,
|
||||
EventDispatcher\NotifyVisitToMercure::class => ConfigAbstractFactory::class,
|
||||
EventDispatcher\UpdateGeoLiteDb::class => ConfigAbstractFactory::class,
|
||||
],
|
||||
|
||||
'delegators' => [
|
||||
@ -66,6 +68,7 @@ return [
|
||||
'em',
|
||||
'Logger_Shlink',
|
||||
],
|
||||
EventDispatcher\UpdateGeoLiteDb::class => [GeolocationDbUpdater::class, 'Logger_Shlink'],
|
||||
],
|
||||
|
||||
];
|
||||
|
43
module/Core/src/EventDispatcher/UpdateGeoLiteDb.php
Normal file
43
module/Core/src/EventDispatcher/UpdateGeoLiteDb.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\EventDispatcher;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Shlinkio\Shlink\CLI\Util\GeolocationDbUpdaterInterface;
|
||||
use Throwable;
|
||||
|
||||
use function sprintf;
|
||||
|
||||
class UpdateGeoLiteDb
|
||||
{
|
||||
private GeolocationDbUpdaterInterface $dbUpdater;
|
||||
private LoggerInterface $logger;
|
||||
|
||||
public function __construct(GeolocationDbUpdaterInterface $dbUpdater, LoggerInterface $logger)
|
||||
{
|
||||
$this->dbUpdater = $dbUpdater;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
public function __invoke(): void
|
||||
{
|
||||
$beforeDownload = fn (bool $olderDbExists) => $this->logger->notice(
|
||||
sprintf('%s GeoLite2 db file...', $olderDbExists ? 'Updating' : 'Downloading'),
|
||||
);
|
||||
$handleProgress = function (int $total, int $downloaded, bool $olderDbExists): void {
|
||||
if ($total > $downloaded) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->logger->notice(sprintf('Finished %s GeoLite2 db file', $olderDbExists ? 'updating' : 'downloading'));
|
||||
};
|
||||
|
||||
try {
|
||||
$this->dbUpdater->checkDbUpdate($beforeDownload, $handleProgress);
|
||||
} catch (Throwable $e) {
|
||||
$this->logger->error('GeoLite2 database download failed. {e}', ['e' => $e]);
|
||||
}
|
||||
}
|
||||
}
|
@ -226,67 +226,4 @@ class LocateVisitTest extends TestCase
|
||||
yield 'invalid short url' => [Visit::forInvalidShortUrl(new Visitor('', '', '1.2.3.4', '')), '1.2.3.4'];
|
||||
yield 'regular not found' => [Visit::forRegularNotFound(new Visitor('', '', '1.2.3.4', '')), '1.2.3.4'];
|
||||
}
|
||||
|
||||
// /** @test */
|
||||
// public function errorWhenUpdatingGeoLiteWithExistingCopyLogsWarning(): void
|
||||
// {
|
||||
// $e = GeolocationDbUpdateFailedException::withOlderDb();
|
||||
// $ipAddr = '1.2.3.0';
|
||||
// $visit = Visit::forValidShortUrl(ShortUrl::createEmpty(), new Visitor('', '', $ipAddr, ''));
|
||||
// $location = new Location('', '', '', '', 0.0, 0.0, '');
|
||||
// $event = new UrlVisited('123');
|
||||
//
|
||||
// $findVisit = $this->em->find(Visit::class, '123')->willReturn($visit);
|
||||
// $flush = $this->em->flush()->will(function (): void {
|
||||
// });
|
||||
// $resolveIp = $this->ipLocationResolver->resolveIpLocation($ipAddr)->willReturn($location);
|
||||
// $checkUpdateDb = $this->dbUpdater->checkDbUpdate(Argument::cetera())->willThrow($e);
|
||||
// $dispatch = $this->eventDispatcher->dispatch(new VisitLocated('123'))->will(function (): void {
|
||||
// });
|
||||
//
|
||||
// ($this->locateVisit)($event);
|
||||
//
|
||||
// self::assertEquals($visit->getVisitLocation(), new VisitLocation($location));
|
||||
// $findVisit->shouldHaveBeenCalledOnce();
|
||||
// $flush->shouldHaveBeenCalledOnce();
|
||||
// $resolveIp->shouldHaveBeenCalledOnce();
|
||||
// $checkUpdateDb->shouldHaveBeenCalledOnce();
|
||||
// $this->logger->warning(
|
||||
// 'GeoLite2 database update failed. Proceeding with old version. {e}',
|
||||
// ['e' => $e],
|
||||
// )->shouldHaveBeenCalledOnce();
|
||||
// $dispatch->shouldHaveBeenCalledOnce();
|
||||
// }
|
||||
//
|
||||
// /** @test */
|
||||
// public function errorWhenDownloadingGeoLiteCancelsLocation(): void
|
||||
// {
|
||||
// $e = GeolocationDbUpdateFailedException::withoutOlderDb();
|
||||
// $ipAddr = '1.2.3.0';
|
||||
// $visit = Visit::forValidShortUrl(ShortUrl::createEmpty(), new Visitor('', '', $ipAddr, ''));
|
||||
// $location = new Location('', '', '', '', 0.0, 0.0, '');
|
||||
// $event = new UrlVisited('123');
|
||||
//
|
||||
// $findVisit = $this->em->find(Visit::class, '123')->willReturn($visit);
|
||||
// $flush = $this->em->flush()->will(function (): void {
|
||||
// });
|
||||
// $resolveIp = $this->ipLocationResolver->resolveIpLocation($ipAddr)->willReturn($location);
|
||||
// $checkUpdateDb = $this->dbUpdater->checkDbUpdate(Argument::cetera())->willThrow($e);
|
||||
// $logError = $this->logger->error(
|
||||
// 'GeoLite2 database download failed. It is not possible to locate visit with id {visitId}. {e}',
|
||||
// ['e' => $e, 'visitId' => 123],
|
||||
// );
|
||||
// $dispatch = $this->eventDispatcher->dispatch(new VisitLocated('123'))->will(function (): void {
|
||||
// });
|
||||
//
|
||||
// ($this->locateVisit)($event);
|
||||
//
|
||||
// self::assertNull($visit->getVisitLocation());
|
||||
// $findVisit->shouldHaveBeenCalledOnce();
|
||||
// $flush->shouldNotHaveBeenCalled();
|
||||
// $resolveIp->shouldNotHaveBeenCalled();
|
||||
// $checkUpdateDb->shouldHaveBeenCalledOnce();
|
||||
// $logError->shouldHaveBeenCalledOnce();
|
||||
// $dispatch->shouldHaveBeenCalledOnce();
|
||||
// }
|
||||
}
|
||||
|
114
module/Core/test/EventDispatcher/UpdateGeoLiteDbTest.php
Normal file
114
module/Core/test/EventDispatcher/UpdateGeoLiteDbTest.php
Normal file
@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioTest\Shlink\Core\EventDispatcher;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\Argument;
|
||||
use Prophecy\PhpUnit\ProphecyTrait;
|
||||
use Prophecy\Prophecy\ObjectProphecy;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use RuntimeException;
|
||||
use Shlinkio\Shlink\CLI\Util\GeolocationDbUpdaterInterface;
|
||||
use Shlinkio\Shlink\Core\EventDispatcher\UpdateGeoLiteDb;
|
||||
|
||||
class UpdateGeoLiteDbTest extends TestCase
|
||||
{
|
||||
use ProphecyTrait;
|
||||
|
||||
private UpdateGeoLiteDb $listener;
|
||||
private ObjectProphecy $dbUpdater;
|
||||
private ObjectProphecy $logger;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->dbUpdater = $this->prophesize(GeolocationDbUpdaterInterface::class);
|
||||
$this->logger = $this->prophesize(LoggerInterface::class);
|
||||
|
||||
$this->listener = new UpdateGeoLiteDb($this->dbUpdater->reveal(), $this->logger->reveal());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function exceptionWhileUpdatingDbLogsError(): void
|
||||
{
|
||||
$e = new RuntimeException();
|
||||
|
||||
$checkDbUpdate = $this->dbUpdater->checkDbUpdate(Argument::cetera())->willThrow($e);
|
||||
$logError = $this->logger->error('GeoLite2 database download failed. {e}', ['e' => $e]);
|
||||
|
||||
($this->listener)();
|
||||
|
||||
$checkDbUpdate->shouldHaveBeenCalledOnce();
|
||||
$logError->shouldHaveBeenCalledOnce();
|
||||
$this->logger->notice(Argument::cetera())->shouldNotHaveBeenCalled();
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideFlags
|
||||
*/
|
||||
public function noticeMessageIsPrintedWhenFirstCallbackIsInvoked(bool $oldDbExists, string $expectedMessage): void
|
||||
{
|
||||
$checkDbUpdate = $this->dbUpdater->checkDbUpdate(Argument::cetera())->will(
|
||||
function (array $args) use ($oldDbExists): void {
|
||||
[$firstCallback] = $args;
|
||||
$firstCallback($oldDbExists);
|
||||
},
|
||||
);
|
||||
$logNotice = $this->logger->notice($expectedMessage);
|
||||
|
||||
($this->listener)();
|
||||
|
||||
$checkDbUpdate->shouldHaveBeenCalledOnce();
|
||||
$logNotice->shouldHaveBeenCalledOnce();
|
||||
$this->logger->error(Argument::cetera())->shouldNotHaveBeenCalled();
|
||||
}
|
||||
|
||||
public function provideFlags(): iterable
|
||||
{
|
||||
yield 'existing old db' => [true, 'Updating GeoLite2 db file...'];
|
||||
yield 'not existing old db' => [false, 'Downloading GeoLite2 db file...'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideDownloaded
|
||||
*/
|
||||
public function noticeMessageIsPrintedWhenSecondCallbackIsInvoked(
|
||||
int $total,
|
||||
int $downloaded,
|
||||
bool $oldDbExists,
|
||||
?string $expectedMessage
|
||||
): void {
|
||||
$checkDbUpdate = $this->dbUpdater->checkDbUpdate(Argument::cetera())->will(
|
||||
function (array $args) use ($total, $downloaded, $oldDbExists): void {
|
||||
[, $secondCallback] = $args;
|
||||
$secondCallback($total, $downloaded, $oldDbExists);
|
||||
},
|
||||
);
|
||||
$logNotice = $this->logger->notice($expectedMessage ?? Argument::cetera());
|
||||
|
||||
($this->listener)();
|
||||
|
||||
if ($expectedMessage !== null) {
|
||||
$logNotice->shouldHaveBeenCalledOnce();
|
||||
} else {
|
||||
$logNotice->shouldNotHaveBeenCalled();
|
||||
}
|
||||
$checkDbUpdate->shouldHaveBeenCalledOnce();
|
||||
$this->logger->error(Argument::cetera())->shouldNotHaveBeenCalled();
|
||||
}
|
||||
|
||||
public function provideDownloaded(): iterable
|
||||
{
|
||||
yield [100, 0, true, null];
|
||||
yield [100, 0, false, null];
|
||||
yield [100, 99, true, null];
|
||||
yield [100, 99, false, null];
|
||||
yield [100, 100, true, 'Finished updating GeoLite2 db file'];
|
||||
yield [100, 100, false, 'Finished downloading GeoLite2 db file'];
|
||||
yield [100, 101, true, 'Finished updating GeoLite2 db file'];
|
||||
yield [100, 101, false, 'Finished downloading GeoLite2 db file'];
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user