Ensured orphan visits are located ASAP when using swoole

This commit is contained in:
Alejandro Celaya
2021-02-09 20:25:28 +01:00
parent b01487ac91
commit ab9042db24
8 changed files with 57 additions and 39 deletions

View File

@@ -109,6 +109,11 @@ class Visit extends AbstractEntity implements JsonSerializable
return $this;
}
public function isOrphan(): bool
{
return $this->shortUrl === null;
}
public function jsonSerialize(): array
{
return [

View File

@@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\EventDispatcher\Event;
final class ShortUrlVisited extends AbstractVisitEvent
final class UrlVisited extends AbstractVisitEvent
{
private ?string $originalIpAddress;

View File

@@ -11,7 +11,7 @@ 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\ShortUrlVisited;
use Shlinkio\Shlink\Core\EventDispatcher\Event\UrlVisited;
use Shlinkio\Shlink\Core\EventDispatcher\Event\VisitLocated;
use Shlinkio\Shlink\IpGeolocation\Exception\WrongIpException;
use Shlinkio\Shlink\IpGeolocation\Model\Location;
@@ -19,7 +19,7 @@ use Shlinkio\Shlink\IpGeolocation\Resolver\IpLocationResolverInterface;
use function sprintf;
class LocateShortUrlVisit
class LocateVisit
{
private IpLocationResolverInterface $ipLocationResolver;
private EntityManagerInterface $em;
@@ -41,7 +41,7 @@ class LocateShortUrlVisit
$this->eventDispatcher = $eventDispatcher;
}
public function __invoke(ShortUrlVisited $shortUrlVisited): void
public function __invoke(UrlVisited $shortUrlVisited): void
{
$visitId = $shortUrlVisited->visitId();
@@ -58,7 +58,9 @@ class LocateShortUrlVisit
$this->locateVisit($visitId, $shortUrlVisited->originalIpAddress(), $visit);
}
$this->eventDispatcher->dispatch(new VisitLocated($visitId));
if (! $visit->isOrphan()) {
$this->eventDispatcher->dispatch(new VisitLocated($visitId));
}
}
private function downloadOrUpdateGeoLiteDb(string $visitId): bool

View File

@@ -8,7 +8,7 @@ use Doctrine\ORM;
use Psr\EventDispatcher\EventDispatcherInterface;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Entity\Visit;
use Shlinkio\Shlink\Core\EventDispatcher\Event\ShortUrlVisited;
use Shlinkio\Shlink\Core\EventDispatcher\Event\UrlVisited;
use Shlinkio\Shlink\Core\Model\Visitor;
class VisitsTracker implements VisitsTrackerInterface
@@ -29,30 +29,29 @@ class VisitsTracker implements VisitsTrackerInterface
public function track(ShortUrl $shortUrl, Visitor $visitor): void
{
$visit = $this->trackVisit(Visit::forValidShortUrl($shortUrl, $visitor, $this->anonymizeRemoteAddr));
$this->eventDispatcher->dispatch(new ShortUrlVisited($visit->getId(), $visitor->getRemoteAddress()));
$this->trackVisit(Visit::forValidShortUrl($shortUrl, $visitor, $this->anonymizeRemoteAddr), $visitor);
}
public function trackInvalidShortUrlVisit(Visitor $visitor): void
{
$this->trackVisit(Visit::forInvalidShortUrl($visitor));
$this->trackVisit(Visit::forInvalidShortUrl($visitor, $this->anonymizeRemoteAddr), $visitor);
}
public function trackBaseUrlVisit(Visitor $visitor): void
{
$this->trackVisit(Visit::forBasePath($visitor));
$this->trackVisit(Visit::forBasePath($visitor, $this->anonymizeRemoteAddr), $visitor);
}
public function trackRegularNotFoundVisit(Visitor $visitor): void
{
$this->trackVisit(Visit::forRegularNotFound($visitor));
$this->trackVisit(Visit::forRegularNotFound($visitor, $this->anonymizeRemoteAddr), $visitor);
}
private function trackVisit(Visit $visit): Visit
private function trackVisit(Visit $visit, Visitor $visitor): void
{
$this->em->persist($visit);
$this->em->flush();
return $visit;
$this->eventDispatcher->dispatch(new UrlVisited($visit->getId(), $visitor->getRemoteAddress()));
}
}