mirror of
https://github.com/shlinkio/shlink.git
synced 2024-11-27 16:26:37 +03:00
Added support for legacy and new publishing of visits in RabbitMQ
This commit is contained in:
parent
53b937be63
commit
6ce2049935
9 changed files with 70 additions and 18 deletions
|
@ -13,6 +13,9 @@ return [
|
||||||
'user' => EnvVars::RABBITMQ_USER->loadFromEnv(),
|
'user' => EnvVars::RABBITMQ_USER->loadFromEnv(),
|
||||||
'password' => EnvVars::RABBITMQ_PASSWORD->loadFromEnv(),
|
'password' => EnvVars::RABBITMQ_PASSWORD->loadFromEnv(),
|
||||||
'vhost' => EnvVars::RABBITMQ_VHOST->loadFromEnv('/'),
|
'vhost' => EnvVars::RABBITMQ_VHOST->loadFromEnv('/'),
|
||||||
|
|
||||||
|
// Deprecated
|
||||||
|
'legacy_visits_publishing' => (bool) EnvVars::RABBITMQ_LEGACY_VISITS_PUBLISHING->loadFromEnv(false),
|
||||||
],
|
],
|
||||||
|
|
||||||
];
|
];
|
||||||
|
|
|
@ -27,6 +27,7 @@ return [
|
||||||
Options\UrlShortenerOptions::class => ConfigAbstractFactory::class,
|
Options\UrlShortenerOptions::class => ConfigAbstractFactory::class,
|
||||||
Options\TrackingOptions::class => ConfigAbstractFactory::class,
|
Options\TrackingOptions::class => ConfigAbstractFactory::class,
|
||||||
Options\QrCodeOptions::class => ConfigAbstractFactory::class,
|
Options\QrCodeOptions::class => ConfigAbstractFactory::class,
|
||||||
|
Options\RabbitMqOptions::class => ConfigAbstractFactory::class,
|
||||||
Options\WebhookOptions::class => ConfigAbstractFactory::class,
|
Options\WebhookOptions::class => ConfigAbstractFactory::class,
|
||||||
|
|
||||||
Service\UrlShortener::class => ConfigAbstractFactory::class,
|
Service\UrlShortener::class => ConfigAbstractFactory::class,
|
||||||
|
@ -91,6 +92,7 @@ return [
|
||||||
Options\UrlShortenerOptions::class => ['config.url_shortener'],
|
Options\UrlShortenerOptions::class => ['config.url_shortener'],
|
||||||
Options\TrackingOptions::class => ['config.tracking'],
|
Options\TrackingOptions::class => ['config.tracking'],
|
||||||
Options\QrCodeOptions::class => ['config.qr_codes'],
|
Options\QrCodeOptions::class => ['config.qr_codes'],
|
||||||
|
Options\RabbitMqOptions::class => ['config.rabbitmq'],
|
||||||
Options\WebhookOptions::class => ['config.visits_webhooks'],
|
Options\WebhookOptions::class => ['config.visits_webhooks'],
|
||||||
|
|
||||||
Service\UrlShortener::class => [
|
Service\UrlShortener::class => [
|
||||||
|
|
|
@ -98,14 +98,14 @@ return [
|
||||||
'Logger_Shlink',
|
'Logger_Shlink',
|
||||||
Visit\Transformer\OrphanVisitDataTransformer::class,
|
Visit\Transformer\OrphanVisitDataTransformer::class,
|
||||||
ShortUrl\Transformer\ShortUrlDataTransformer::class,
|
ShortUrl\Transformer\ShortUrlDataTransformer::class,
|
||||||
'config.rabbitmq.enabled',
|
Options\RabbitMqOptions::class,
|
||||||
],
|
],
|
||||||
EventDispatcher\RabbitMq\NotifyNewShortUrlToRabbitMq::class => [
|
EventDispatcher\RabbitMq\NotifyNewShortUrlToRabbitMq::class => [
|
||||||
RabbitMqPublishingHelper::class,
|
RabbitMqPublishingHelper::class,
|
||||||
'em',
|
'em',
|
||||||
'Logger_Shlink',
|
'Logger_Shlink',
|
||||||
ShortUrl\Transformer\ShortUrlDataTransformer::class,
|
ShortUrl\Transformer\ShortUrlDataTransformer::class,
|
||||||
'config.rabbitmq.enabled',
|
Options\RabbitMqOptions::class,
|
||||||
],
|
],
|
||||||
EventDispatcher\UpdateGeoLiteDb::class => [GeolocationDbUpdater::class, 'Logger_Shlink'],
|
EventDispatcher\UpdateGeoLiteDb::class => [GeolocationDbUpdater::class, 'Logger_Shlink'],
|
||||||
],
|
],
|
||||||
|
|
|
@ -33,6 +33,8 @@ enum EnvVars: string
|
||||||
case RABBITMQ_USER = 'RABBITMQ_USER';
|
case RABBITMQ_USER = 'RABBITMQ_USER';
|
||||||
case RABBITMQ_PASSWORD = 'RABBITMQ_PASSWORD';
|
case RABBITMQ_PASSWORD = 'RABBITMQ_PASSWORD';
|
||||||
case RABBITMQ_VHOST = 'RABBITMQ_VHOST';
|
case RABBITMQ_VHOST = 'RABBITMQ_VHOST';
|
||||||
|
/** @deprecated */
|
||||||
|
case RABBITMQ_LEGACY_VISITS_PUBLISHING = 'RABBITMQ_LEGACY_VISITS_PUBLISHING';
|
||||||
case DEFAULT_INVALID_SHORT_URL_REDIRECT = 'DEFAULT_INVALID_SHORT_URL_REDIRECT';
|
case DEFAULT_INVALID_SHORT_URL_REDIRECT = 'DEFAULT_INVALID_SHORT_URL_REDIRECT';
|
||||||
case DEFAULT_REGULAR_404_REDIRECT = 'DEFAULT_REGULAR_404_REDIRECT';
|
case DEFAULT_REGULAR_404_REDIRECT = 'DEFAULT_REGULAR_404_REDIRECT';
|
||||||
case DEFAULT_BASE_URL_REDIRECT = 'DEFAULT_BASE_URL_REDIRECT';
|
case DEFAULT_BASE_URL_REDIRECT = 'DEFAULT_BASE_URL_REDIRECT';
|
||||||
|
|
|
@ -11,6 +11,7 @@ use Shlinkio\Shlink\Common\Rest\DataTransformerInterface;
|
||||||
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||||
use Shlinkio\Shlink\Core\EventDispatcher\Event\ShortUrlCreated;
|
use Shlinkio\Shlink\Core\EventDispatcher\Event\ShortUrlCreated;
|
||||||
use Shlinkio\Shlink\Core\EventDispatcher\Topic;
|
use Shlinkio\Shlink\Core\EventDispatcher\Topic;
|
||||||
|
use Shlinkio\Shlink\Core\Options\RabbitMqOptions;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
|
|
||||||
class NotifyNewShortUrlToRabbitMq
|
class NotifyNewShortUrlToRabbitMq
|
||||||
|
@ -20,13 +21,13 @@ class NotifyNewShortUrlToRabbitMq
|
||||||
private readonly EntityManagerInterface $em,
|
private readonly EntityManagerInterface $em,
|
||||||
private readonly LoggerInterface $logger,
|
private readonly LoggerInterface $logger,
|
||||||
private readonly DataTransformerInterface $shortUrlTransformer,
|
private readonly DataTransformerInterface $shortUrlTransformer,
|
||||||
private readonly bool $isEnabled,
|
private readonly RabbitMqOptions $options,
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __invoke(ShortUrlCreated $shortUrlCreated): void
|
public function __invoke(ShortUrlCreated $shortUrlCreated): void
|
||||||
{
|
{
|
||||||
if (! $this->isEnabled) {
|
if (! $this->options->isEnabled()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,7 @@ use Shlinkio\Shlink\Common\Rest\DataTransformerInterface;
|
||||||
use Shlinkio\Shlink\Core\Entity\Visit;
|
use Shlinkio\Shlink\Core\Entity\Visit;
|
||||||
use Shlinkio\Shlink\Core\EventDispatcher\Event\VisitLocated;
|
use Shlinkio\Shlink\Core\EventDispatcher\Event\VisitLocated;
|
||||||
use Shlinkio\Shlink\Core\EventDispatcher\Topic;
|
use Shlinkio\Shlink\Core\EventDispatcher\Topic;
|
||||||
|
use Shlinkio\Shlink\Core\Options\RabbitMqOptions;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
|
|
||||||
class NotifyVisitToRabbitMq
|
class NotifyVisitToRabbitMq
|
||||||
|
@ -20,14 +21,14 @@ class NotifyVisitToRabbitMq
|
||||||
private readonly EntityManagerInterface $em,
|
private readonly EntityManagerInterface $em,
|
||||||
private readonly LoggerInterface $logger,
|
private readonly LoggerInterface $logger,
|
||||||
private readonly DataTransformerInterface $orphanVisitTransformer,
|
private readonly DataTransformerInterface $orphanVisitTransformer,
|
||||||
private readonly DataTransformerInterface $shortUrlTransformer, // @phpstan-ignore-line
|
private readonly DataTransformerInterface $shortUrlTransformer,
|
||||||
private readonly bool $isEnabled,
|
private readonly RabbitMqOptions $options,
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __invoke(VisitLocated $shortUrlLocated): void
|
public function __invoke(VisitLocated $shortUrlLocated): void
|
||||||
{
|
{
|
||||||
if (! $this->isEnabled) {
|
if (! $this->options->isEnabled()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,14 +71,15 @@ class NotifyVisitToRabbitMq
|
||||||
|
|
||||||
private function visitToPayload(Visit $visit): array
|
private function visitToPayload(Visit $visit): array
|
||||||
{
|
{
|
||||||
// FIXME This was defined incorrectly.
|
// This was defined incorrectly.
|
||||||
// According to the spec, both the visit and the short URL it belongs to, should be published.
|
// According to the spec, both the visit and the short URL it belongs to, should be published.
|
||||||
// The shape should be ['visit' => [...], 'shortUrl' => ?[...]]
|
// The shape should be ['visit' => [...], 'shortUrl' => ?[...]]
|
||||||
// However, this would be a breaking change, so we need a flag that determines the shape of the payload.
|
// However, this would be a breaking change, so we need a flag that determines the shape of the payload.
|
||||||
|
if ($this->options->legacyVisitsPublishing()) {
|
||||||
|
return ! $visit->isOrphan() ? $visit->jsonSerialize() : $this->orphanVisitTransformer->transform($visit);
|
||||||
|
}
|
||||||
|
|
||||||
return ! $visit->isOrphan() ? $visit->jsonSerialize() : $this->orphanVisitTransformer->transform($visit);
|
if ($visit->isOrphan()) {
|
||||||
|
|
||||||
if ($visit->isOrphan()) { // @phpstan-ignore-line
|
|
||||||
return ['visit' => $this->orphanVisitTransformer->transform($visit)];
|
return ['visit' => $this->orphanVisitTransformer->transform($visit)];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
40
module/Core/src/Options/RabbitMqOptions.php
Normal file
40
module/Core/src/Options/RabbitMqOptions.php
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Shlinkio\Shlink\Core\Options;
|
||||||
|
|
||||||
|
use Laminas\Stdlib\AbstractOptions;
|
||||||
|
|
||||||
|
class RabbitMqOptions extends AbstractOptions
|
||||||
|
{
|
||||||
|
protected $__strictMode__ = false; // phpcs:ignore
|
||||||
|
|
||||||
|
private bool $enabled = false;
|
||||||
|
/** @deprecated */
|
||||||
|
private bool $legacyVisitsPublishing = false;
|
||||||
|
|
||||||
|
public function isEnabled(): bool
|
||||||
|
{
|
||||||
|
return $this->enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function setEnabled(bool $enabled): self
|
||||||
|
{
|
||||||
|
$this->enabled = $enabled;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @deprecated */
|
||||||
|
public function legacyVisitsPublishing(): bool
|
||||||
|
{
|
||||||
|
return $this->legacyVisitsPublishing;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @deprecated */
|
||||||
|
protected function setLegacyVisitsPublishing(bool $legacyVisitsPublishing): self
|
||||||
|
{
|
||||||
|
$this->legacyVisitsPublishing = $legacyVisitsPublishing;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
}
|
|
@ -18,6 +18,7 @@ use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||||
use Shlinkio\Shlink\Core\EventDispatcher\Event\ShortUrlCreated;
|
use Shlinkio\Shlink\Core\EventDispatcher\Event\ShortUrlCreated;
|
||||||
use Shlinkio\Shlink\Core\EventDispatcher\RabbitMq\NotifyNewShortUrlToRabbitMq;
|
use Shlinkio\Shlink\Core\EventDispatcher\RabbitMq\NotifyNewShortUrlToRabbitMq;
|
||||||
use Shlinkio\Shlink\Core\EventDispatcher\Topic;
|
use Shlinkio\Shlink\Core\EventDispatcher\Topic;
|
||||||
|
use Shlinkio\Shlink\Core\Options\RabbitMqOptions;
|
||||||
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifier;
|
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifier;
|
||||||
use Shlinkio\Shlink\Core\ShortUrl\Transformer\ShortUrlDataTransformer;
|
use Shlinkio\Shlink\Core\ShortUrl\Transformer\ShortUrlDataTransformer;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
|
@ -42,7 +43,7 @@ class NotifyNewShortUrlToRabbitMqTest extends TestCase
|
||||||
$this->em->reveal(),
|
$this->em->reveal(),
|
||||||
$this->logger->reveal(),
|
$this->logger->reveal(),
|
||||||
new ShortUrlDataTransformer(new ShortUrlStringifier([])),
|
new ShortUrlDataTransformer(new ShortUrlStringifier([])),
|
||||||
true,
|
new RabbitMqOptions(['enabled' => true]),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,7 +55,7 @@ class NotifyNewShortUrlToRabbitMqTest extends TestCase
|
||||||
$this->em->reveal(),
|
$this->em->reveal(),
|
||||||
$this->logger->reveal(),
|
$this->logger->reveal(),
|
||||||
new ShortUrlDataTransformer(new ShortUrlStringifier([])),
|
new ShortUrlDataTransformer(new ShortUrlStringifier([])),
|
||||||
false,
|
new RabbitMqOptions(['enabled' => false]),
|
||||||
);
|
);
|
||||||
|
|
||||||
$listener(new ShortUrlCreated('123'));
|
$listener(new ShortUrlCreated('123'));
|
||||||
|
|
|
@ -20,6 +20,7 @@ use Shlinkio\Shlink\Core\EventDispatcher\Event\VisitLocated;
|
||||||
use Shlinkio\Shlink\Core\EventDispatcher\RabbitMq\NotifyVisitToRabbitMq;
|
use Shlinkio\Shlink\Core\EventDispatcher\RabbitMq\NotifyVisitToRabbitMq;
|
||||||
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
|
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
|
||||||
use Shlinkio\Shlink\Core\Model\Visitor;
|
use Shlinkio\Shlink\Core\Model\Visitor;
|
||||||
|
use Shlinkio\Shlink\Core\Options\RabbitMqOptions;
|
||||||
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifier;
|
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifier;
|
||||||
use Shlinkio\Shlink\Core\ShortUrl\Transformer\ShortUrlDataTransformer;
|
use Shlinkio\Shlink\Core\ShortUrl\Transformer\ShortUrlDataTransformer;
|
||||||
use Shlinkio\Shlink\Core\Visit\Transformer\OrphanVisitDataTransformer;
|
use Shlinkio\Shlink\Core\Visit\Transformer\OrphanVisitDataTransformer;
|
||||||
|
@ -49,7 +50,7 @@ class NotifyVisitToRabbitMqTest extends TestCase
|
||||||
$this->logger->reveal(),
|
$this->logger->reveal(),
|
||||||
new OrphanVisitDataTransformer(),
|
new OrphanVisitDataTransformer(),
|
||||||
new ShortUrlDataTransformer(new ShortUrlStringifier([])),
|
new ShortUrlDataTransformer(new ShortUrlStringifier([])),
|
||||||
true,
|
new RabbitMqOptions(['enabled' => true, 'legacy_visits_publishing' => true]),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,7 +63,7 @@ class NotifyVisitToRabbitMqTest extends TestCase
|
||||||
$this->logger->reveal(),
|
$this->logger->reveal(),
|
||||||
new OrphanVisitDataTransformer(),
|
new OrphanVisitDataTransformer(),
|
||||||
new ShortUrlDataTransformer(new ShortUrlStringifier([])),
|
new ShortUrlDataTransformer(new ShortUrlStringifier([])),
|
||||||
false,
|
new RabbitMqOptions(['enabled' => false, 'legacy_visits_publishing' => true]),
|
||||||
);
|
);
|
||||||
|
|
||||||
$listener(new VisitLocated('123'));
|
$listener(new VisitLocated('123'));
|
||||||
|
|
Loading…
Reference in a new issue