mirror of
https://github.com/maelgangloff/domain-watchdog.git
synced 2025-12-17 17:55:42 +00:00
feat: fragment messages to gain efficiency
This commit is contained in:
parent
43c4c9a33d
commit
8a5f69c333
@ -25,7 +25,9 @@ framework:
|
||||
Symfony\Component\Notifier\Message\ChatMessage: async
|
||||
Symfony\Component\Notifier\Message\SmsMessage: 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
|
||||
# 'App\Message\YourMessage': async
|
||||
|
||||
@ -3,40 +3,51 @@
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\Domain;
|
||||
use App\Entity\WatchList;
|
||||
use App\Message\ProcessDomainTrigger;
|
||||
use App\Repository\DomainRepository;
|
||||
use App\Service\RDAPService;
|
||||
use DateTimeImmutable;
|
||||
use Exception;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
|
||||
use Symfony\Component\Messenger\Exception\ExceptionInterface;
|
||||
use Symfony\Component\Messenger\MessageBusInterface;
|
||||
use Symfony\Component\RateLimiter\RateLimiterFactory;
|
||||
|
||||
class DomainRefreshController extends AbstractController
|
||||
{
|
||||
|
||||
public function __construct(private readonly DomainRepository $domainRepository,
|
||||
private readonly RDAPService $RDAPService,
|
||||
private readonly RateLimiterFactory $authenticatedApiLimiter)
|
||||
public function __construct(private readonly DomainRepository $domainRepository,
|
||||
private readonly RDAPService $RDAPService,
|
||||
private readonly RateLimiterFactory $authenticatedApiLimiter,
|
||||
private readonly MessageBusInterface $bus)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
* @throws ExceptionInterface
|
||||
*/
|
||||
public function __invoke(string $ldhName,): ?Domain
|
||||
{
|
||||
/** @var Domain $domain */
|
||||
$domain = $this->domainRepository->findOneBy(["ldhName" => $ldhName]);
|
||||
if ($domain === null ||
|
||||
$domain->getUpdatedAt()->diff(new DateTimeImmutable('now'))->days >= 7) {
|
||||
if ($domain !== null && $domain->getUpdatedAt()->diff(new DateTimeImmutable('now'))->days < 7) return $domain;
|
||||
|
||||
if ($this->container->getParameter('kernel.environment') !== 'dev') {
|
||||
$limiter = $this->authenticatedApiLimiter->create($this->getUser()->getUserIdentifier());
|
||||
if (false === $limiter->consume()->isAccepted()) {
|
||||
throw new TooManyRequestsHttpException();
|
||||
}
|
||||
|
||||
$domain = $this->RDAPService->registerDomain($ldhName);
|
||||
if (false === $limiter->consume()->isAccepted()) throw new TooManyRequestsHttpException();
|
||||
}
|
||||
|
||||
$updatedAt = $domain->getUpdatedAt();
|
||||
$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;
|
||||
}
|
||||
}
|
||||
18
src/Message/ProcessDomainTrigger.php
Normal file
18
src/Message/ProcessDomainTrigger.php
Normal 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
|
||||
)
|
||||
{
|
||||
}
|
||||
}
|
||||
15
src/Message/ProcessWatchListTrigger.php
Normal file
15
src/Message/ProcessWatchListTrigger.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App\Message;
|
||||
|
||||
use App\Entity\WatchList;
|
||||
|
||||
final readonly class ProcessWatchListTrigger
|
||||
{
|
||||
|
||||
public function __construct(
|
||||
public string $watchListToken,
|
||||
)
|
||||
{
|
||||
}
|
||||
}
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Message;
|
||||
|
||||
final class SendNotifWatchListTrigger
|
||||
final class ProcessWatchListsTrigger
|
||||
{
|
||||
/*
|
||||
* Add whatever properties and methods you need
|
||||
81
src/MessageHandler/ProcessDomainTriggerHandler.php
Normal file
81
src/MessageHandler/ProcessDomainTriggerHandler.php
Normal 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);
|
||||
}
|
||||
|
||||
}
|
||||
82
src/MessageHandler/ProcessWatchListTriggerHandler.php
Normal file
82
src/MessageHandler/ProcessWatchListTriggerHandler.php
Normal 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);
|
||||
}
|
||||
|
||||
}
|
||||
35
src/MessageHandler/ProcessWatchListsTriggerHandler.php
Normal file
35
src/MessageHandler/ProcessWatchListsTriggerHandler.php
Normal 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()));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Scheduler;
|
||||
|
||||
use App\Message\SendNotifWatchListTrigger;
|
||||
use App\Message\ProcessWatchListsTrigger;
|
||||
use Symfony\Component\Scheduler\Attribute\AsSchedule;
|
||||
use Symfony\Component\Scheduler\RecurringMessage;
|
||||
use Symfony\Component\Scheduler\Schedule;
|
||||
@ -14,16 +14,16 @@ final readonly class SendNotifWatchListTriggerSchedule implements ScheduleProvid
|
||||
{
|
||||
public function __construct(
|
||||
private CacheInterface $cache,
|
||||
) {
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
public function getSchedule(): Schedule
|
||||
{
|
||||
return (new Schedule())
|
||||
->add(
|
||||
RecurringMessage::every('10 seconds', new SendNotifWatchListTrigger()),
|
||||
RecurringMessage::every('10 seconds', new ProcessWatchListsTrigger()),
|
||||
)
|
||||
->stateful($this->cache)
|
||||
;
|
||||
->stateful($this->cache);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user