fix: lock domain purchase if already launched

This commit is contained in:
Maël Gangloff
2025-11-09 17:38:31 +01:00
parent 7f288c01e3
commit 66e2c25b18
5 changed files with 32 additions and 5 deletions

View File

@@ -17,8 +17,11 @@ use App\Service\Provider\AbstractProvider;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\DependencyInjection\Attribute\Target;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Lock\Key;
use Symfony\Component\Lock\LockFactory;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
@@ -43,7 +46,10 @@ final readonly class OrderDomainHandler
#[Autowire(service: 'service_container')]
private ContainerInterface $locator,
#[Autowire(param: 'influxdb_enabled')]
private bool $influxdbEnabled, private EntityManagerInterface $em, private DomainPurchaseRepository $domainPurchaseRepository,
private bool $influxdbEnabled, private EntityManagerInterface $em,
private DomainPurchaseRepository $domainPurchaseRepository,
#[Target('lock')]
private LockFactory $lockFactory,
) {
$this->sender = new Address($mailerSenderEmail, $mailerSenderName);
}
@@ -72,6 +78,23 @@ final readonly class OrderDomainHandler
return;
}
$lock = $this->lockFactory->createLockFromKey(
new Key('domain_purchase.'.$domain->getLdhName().'.'.$connector->getId()),
ttl: 600,
autoRelease: false
);
if (!$lock->acquire()) {
$this->logger->notice('Purchase attempt is already launched for this domain name with this connector', [
'watchlist' => $message->watchlistToken,
'connector' => $connector->getId(),
'ldhName' => $message->ldhName,
'provider' => $connector->getProvider()->value,
]);
return;
}
$this->logger->notice('Watchlist is linked to a connector : a purchase attempt will be made for this domain name', [
'watchlist' => $message->watchlistToken,
'connector' => $connector->getId(),
@@ -162,6 +185,8 @@ final readonly class OrderDomainHandler
$this->em->flush();
}
$lock->release();
throw $exception;
}
}

View File

@@ -20,7 +20,7 @@ class DomainPurchaseRepository extends ServiceEntityRepository
{
return $this->createQueryBuilder('dp')
->select('COUNT(dp)')
->where('dp.domainOrderedAt not NULL')
->where('dp.domainOrderedAt IS NOT NULL')
->getQuery()->getSingleScalarResult();
}
@@ -28,7 +28,7 @@ class DomainPurchaseRepository extends ServiceEntityRepository
{
return $this->createQueryBuilder('dp')
->select('COUNT(dp)')
->where('dp.domainOrderedAt is NULL')
->where('dp.domainOrderedAt IS NULL')
->getQuery()->getSingleScalarResult();
}
}