feat: fragment messages to gain efficiency

This commit is contained in:
Maël Gangloff 2024-07-21 16:55:39 +02:00
parent 43c4c9a33d
commit 8a5f69c333
No known key found for this signature in database
GPG Key ID: 11FDC81C24A7F629
10 changed files with 261 additions and 135 deletions

View File

@ -25,7 +25,9 @@ framework:
Symfony\Component\Notifier\Message\ChatMessage: async Symfony\Component\Notifier\Message\ChatMessage: async
Symfony\Component\Notifier\Message\SmsMessage: async Symfony\Component\Notifier\Message\SmsMessage: async
App\Message\UpdateRdapServers: async App\Message\UpdateRdapServers: async
App\Message\SendNotifWatchListTrigger: async App\Message\ProcessWatchListsTrigger: async
App\Message\ProcessWatchListTrigger: async
App\Message\ProcessDomainTrigger: async
# Route your messages to the transports # Route your messages to the transports
# 'App\Message\YourMessage': async # 'App\Message\YourMessage': async

View File

@ -3,12 +3,16 @@
namespace App\Controller; namespace App\Controller;
use App\Entity\Domain; use App\Entity\Domain;
use App\Entity\WatchList;
use App\Message\ProcessDomainTrigger;
use App\Repository\DomainRepository; use App\Repository\DomainRepository;
use App\Service\RDAPService; use App\Service\RDAPService;
use DateTimeImmutable; use DateTimeImmutable;
use Exception; use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException; use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
use Symfony\Component\Messenger\Exception\ExceptionInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\RateLimiter\RateLimiterFactory; use Symfony\Component\RateLimiter\RateLimiterFactory;
class DomainRefreshController extends AbstractController class DomainRefreshController extends AbstractController
@ -16,27 +20,34 @@ class DomainRefreshController extends AbstractController
public function __construct(private readonly DomainRepository $domainRepository, public function __construct(private readonly DomainRepository $domainRepository,
private readonly RDAPService $RDAPService, private readonly RDAPService $RDAPService,
private readonly RateLimiterFactory $authenticatedApiLimiter) private readonly RateLimiterFactory $authenticatedApiLimiter,
private readonly MessageBusInterface $bus)
{ {
} }
/** /**
* @throws Exception * @throws Exception
* @throws ExceptionInterface
*/ */
public function __invoke(string $ldhName,): ?Domain public function __invoke(string $ldhName,): ?Domain
{ {
/** @var Domain $domain */ /** @var Domain $domain */
$domain = $this->domainRepository->findOneBy(["ldhName" => $ldhName]); $domain = $this->domainRepository->findOneBy(["ldhName" => $ldhName]);
if ($domain === null || if ($domain !== null && $domain->getUpdatedAt()->diff(new DateTimeImmutable('now'))->days < 7) return $domain;
$domain->getUpdatedAt()->diff(new DateTimeImmutable('now'))->days >= 7) {
if ($this->container->getParameter('kernel.environment') !== 'dev') {
$limiter = $this->authenticatedApiLimiter->create($this->getUser()->getUserIdentifier()); $limiter = $this->authenticatedApiLimiter->create($this->getUser()->getUserIdentifier());
if (false === $limiter->consume()->isAccepted()) { if (false === $limiter->consume()->isAccepted()) throw new TooManyRequestsHttpException();
throw new TooManyRequestsHttpException();
} }
$updatedAt = $domain->getUpdatedAt();
$domain = $this->RDAPService->registerDomain($ldhName); $domain = $this->RDAPService->registerDomain($ldhName);
/** @var WatchList $watchList */
foreach ($domain->getWatchLists()->getIterator() as $watchList) {
$this->bus->dispatch(new ProcessDomainTrigger($watchList->getToken(), $domain->getLdhName(), $updatedAt));
} }
return $domain; return $domain;
} }
} }

View File

@ -0,0 +1,18 @@
<?php
namespace App\Message;
use App\Entity\Domain;
use App\Entity\WatchList;
use DateTimeImmutable;
final class ProcessDomainTrigger
{
public function __construct(
public string $watchListToken,
public string $ldhName,
public DateTimeImmutable $updatedAt
)
{
}
}

View File

@ -0,0 +1,15 @@
<?php
namespace App\Message;
use App\Entity\WatchList;
final readonly class ProcessWatchListTrigger
{
public function __construct(
public string $watchListToken,
)
{
}
}

View File

@ -2,7 +2,7 @@
namespace App\Message; namespace App\Message;
final class SendNotifWatchListTrigger final class ProcessWatchListsTrigger
{ {
/* /*
* Add whatever properties and methods you need * Add whatever properties and methods you need

View File

@ -0,0 +1,81 @@
<?php
namespace App\MessageHandler;
use App\Config\TriggerAction;
use App\Entity\Domain;
use App\Entity\DomainEvent;
use App\Entity\User;
use App\Entity\WatchList;
use App\Entity\WatchListTrigger;
use App\Message\ProcessDomainTrigger;
use App\Repository\DomainRepository;
use App\Repository\WatchListRepository;
use Exception;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Mime\Email;
#[AsMessageHandler]
final readonly class ProcessDomainTriggerHandler
{
public function __construct(
private string $mailerSenderEmail,
private MailerInterface $mailer,
private WatchListRepository $watchListRepository,
private DomainRepository $domainRepository,
)
{
}
/**
* @throws TransportExceptionInterface
* @throws Exception
*/
public function __invoke(ProcessDomainTrigger $message): void
{
/** @var WatchList $watchList */
$watchList = $this->watchListRepository->findOneBy(["token" => $message->watchListToken]);
/** @var Domain $domain */
$domain = $this->domainRepository->findOneBy(["ldhName" => $message->ldhName]);
/** @var DomainEvent $event */
foreach ($domain->getEvents()->filter(fn($event) => $message->updatedAt < $event->getDate()) as $event) {
$watchListTriggers = $watchList->getWatchListTriggers()
->filter(fn($trigger) => $trigger->getEvent() === $event->getAction());
/** @var WatchListTrigger $watchListTrigger */
foreach ($watchListTriggers->getIterator() as $watchListTrigger) {
switch ($watchListTrigger->getAction()) {
case TriggerAction::SendEmail:
$this->sendEmailDomainUpdated($event, $watchList->getUser());
}
}
}
}
/**
* @throws TransportExceptionInterface
*/
private function sendEmailDomainUpdated(DomainEvent $domainEvent, User $user): void
{
$email = (new TemplatedEmail())
->from($this->mailerSenderEmail)
->to($user->getEmail())
->priority(Email::PRIORITY_HIGHEST)
->subject('A domain name has been changed')
->htmlTemplate('emails/domain_updated.html.twig')
->locale('en')
->context([
"event" => $domainEvent
]);
$this->mailer->send($email);
}
}

View File

@ -0,0 +1,82 @@
<?php
namespace App\MessageHandler;
use App\Entity\Domain;
use App\Entity\User;
use App\Entity\WatchList;
use App\Message\ProcessDomainTrigger;
use App\Message\ProcessWatchListTrigger;
use App\Repository\WatchListRepository;
use App\Service\RDAPService;
use DateTimeImmutable;
use Exception;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\Exception\ExceptionInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Throwable;
#[AsMessageHandler]
final readonly class ProcessWatchListTriggerHandler
{
public function __construct(
private RDAPService $RDAPService,
private MailerInterface $mailer,
private string $mailerSenderEmail,
private MessageBusInterface $bus,
private WatchListRepository $watchListRepository
)
{
}
/**
* @throws TransportExceptionInterface
* @throws Exception
* @throws ExceptionInterface
*/
public function __invoke(ProcessWatchListTrigger $message): void
{
/** @var WatchList $watchList */
$watchList = $this->watchListRepository->findOneBy(["token" => $message->watchListToken]);
/** @var Domain $domain */
foreach ($watchList->getDomains()
->filter(fn($domain) => $domain->getUpdatedAt()
->diff(new DateTimeImmutable('now'))->days >= 7) as $domain
) {
$updatedAt = $domain->getUpdatedAt();
try {
$domain = $this->RDAPService->registerDomain($domain->getLdhName());
} catch (Throwable) {
$this->sendEmailDomainUpdateError($domain, $watchList->getUser());
continue;
}
$this->bus->dispatch(new ProcessDomainTrigger($watchList->getToken(), $domain->getLdhName(), $updatedAt));
}
}
/**
* @throws TransportExceptionInterface
*/
private function sendEmailDomainUpdateError(Domain $domain, User $user): void
{
$email = (new TemplatedEmail())
->from($this->mailerSenderEmail)
->to($user->getEmail())
->subject('An error occurred while updating a domain name')
->htmlTemplate('emails/errors/domain_update.html.twig')
->locale('en')
->context([
"domain" => $domain
]);
$this->mailer->send($email);
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace App\MessageHandler;
use App\Entity\WatchList;
use App\Message\ProcessWatchListsTrigger;
use App\Message\ProcessWatchListTrigger;
use App\Repository\WatchListRepository;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\Exception\ExceptionInterface;
use Symfony\Component\Messenger\MessageBusInterface;
#[AsMessageHandler]
readonly final class ProcessWatchListsTriggerHandler
{
public function __construct(
private WatchListRepository $watchListRepository,
private MessageBusInterface $bus
)
{
}
/**
* @throws ExceptionInterface
*/
public function __invoke(ProcessWatchListsTrigger $message): void
{
/** @var WatchList $watchList */
foreach ($this->watchListRepository->findAll() as $watchList) {
$this->bus->dispatch(new ProcessWatchListTrigger($watchList->getToken()));
}
}
}

View File

@ -1,118 +0,0 @@
<?php
namespace App\MessageHandler;
use App\Config\TriggerAction;
use App\Entity\Domain;
use App\Entity\DomainEvent;
use App\Entity\User;
use App\Entity\WatchList;
use App\Entity\WatchListTrigger;
use App\Message\SendNotifWatchListTrigger;
use App\Repository\WatchListRepository;
use App\Service\RDAPService;
use DateTimeImmutable;
use Exception;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Mime\Email;
use Throwable;
#[AsMessageHandler]
readonly final class SendNotifWatchListTriggerHandler
{
public function __construct(
private WatchListRepository $watchListRepository,
private RDAPService $RDAPService,
private MailerInterface $mailer,
private string $mailerSenderEmail
)
{
}
/**
* @throws Exception
* @throws TransportExceptionInterface
*/
public function __invoke(SendNotifWatchListTrigger $message): void
{
/** @var WatchList $watchList */
foreach ($this->watchListRepository->findAll() as $watchList) {
/** @var Domain $domain */
foreach ($watchList->getDomains()
->filter(fn($domain) => $domain->getUpdatedAt()
->diff(new DateTimeImmutable('now'))->days >= 7) as $domain
) {
$updatedAt = $domain->getUpdatedAt();
try {
$domain = $this->RDAPService->registerDomain($domain->getLdhName());
} catch (Throwable) {
$this->sendEmailDomainUpdateError($domain, $watchList->getUser());
continue;
}
/** @var DomainEvent $event */
foreach ($domain->getEvents()->filter(fn($event) => $updatedAt < $event->getDate()) as $event) {
$watchListTriggers = $watchList->getWatchListTriggers()
->filter(fn($trigger) => $trigger->getEvent() === $event->getAction());
/** @var WatchListTrigger $watchListTrigger */
foreach ($watchListTriggers->getIterator() as $watchListTrigger) {
switch ($watchListTrigger->getAction()) {
case TriggerAction::SendEmail:
$this->sendEmailDomainUpdated($event, $watchList->getUser());
}
}
}
}
}
}
/**
* @throws TransportExceptionInterface
*/
public function sendEmailDomainUpdateError(Domain $domain, User $user): Email
{
$email = (new TemplatedEmail())
->from($this->mailerSenderEmail)
->to($user->getEmail())
->subject('An error occurred while updating a domain name')
->htmlTemplate('emails/errors/domain_update.html.twig')
->locale('en')
->context([
"domain" => $domain
]);
$this->mailer->send($email);
return $email;
}
/**
* @throws TransportExceptionInterface
*/
public function sendEmailDomainUpdated(DomainEvent $domainEvent, User $user): Email
{
$email = (new TemplatedEmail())
->from($this->mailerSenderEmail)
->to($user->getEmail())
->priority(Email::PRIORITY_HIGHEST)
->subject('A domain name has been changed')
->htmlTemplate('emails/domain_updated.html.twig')
->locale('en')
->context([
"event" => $domainEvent
]);
$this->mailer->send($email);
return $email;
}
}

View File

@ -2,7 +2,7 @@
namespace App\Scheduler; namespace App\Scheduler;
use App\Message\SendNotifWatchListTrigger; use App\Message\ProcessWatchListsTrigger;
use Symfony\Component\Scheduler\Attribute\AsSchedule; use Symfony\Component\Scheduler\Attribute\AsSchedule;
use Symfony\Component\Scheduler\RecurringMessage; use Symfony\Component\Scheduler\RecurringMessage;
use Symfony\Component\Scheduler\Schedule; use Symfony\Component\Scheduler\Schedule;
@ -14,16 +14,16 @@ final readonly class SendNotifWatchListTriggerSchedule implements ScheduleProvid
{ {
public function __construct( public function __construct(
private CacheInterface $cache, private CacheInterface $cache,
) { )
{
} }
public function getSchedule(): Schedule public function getSchedule(): Schedule
{ {
return (new Schedule()) return (new Schedule())
->add( ->add(
RecurringMessage::every('10 seconds', new SendNotifWatchListTrigger()), RecurringMessage::every('10 seconds', new ProcessWatchListsTrigger()),
) )
->stateful($this->cache) ->stateful($this->cache);
;
} }
} }