Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable failure services when failure_transport disable in user land #29

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,4 @@
<directory name="vendor"/>
</ignoreFiles>
</projectFiles>
<issueHandlers>
<MissingPropertyType>
<errorLevel type="suppress">
<file name="src/Storage/Doctrine/StoredMessage.php"/>
</errorLevel>
</MissingPropertyType>
<PossiblyFalseArgument>
<errorLevel type="suppress">
<file name="src/Storage/Doctrine/EventListener/UpdateStoredMessageListener.php"/>
<file name="src/Storage/Doctrine/EventListener/SaveRetriedMessageListener.php"/>
<file name="src/Storage/Doctrine/StoredMessage.php"/>
</errorLevel>
</PossiblyFalseArgument>
</issueHandlers>
</psalm>
20 changes: 11 additions & 9 deletions src/Controller/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\Transport\Receiver\MessageCountAwareInterface;
use SymfonyCasts\MessengerMonitorBundle\Exception\FailureReceiverDoesNotExistException;
use SymfonyCasts\MessengerMonitorBundle\Exception\FailureReceiverNotListableException;
use SymfonyCasts\MessengerMonitorBundle\FailedMessage\FailedMessageRepository;
use SymfonyCasts\MessengerMonitorBundle\Locator\ReceiverLocator;
Expand All @@ -24,8 +23,8 @@ final class DashboardController
public function __construct(
private Environment $twig,
private ReceiverLocator $receiverLocator,
private FailedMessageRepository $failedMessageRepository,
private StatisticsProcessorInterface $statisticsProcessor
private StatisticsProcessorInterface $statisticsProcessor,
private ?FailedMessageRepository $failedMessageRepository = null,
) {
}

Expand All @@ -37,13 +36,16 @@ public function __invoke(): Response
}

$failedMessages = null;
try {
$failedMessages = $this->failedMessageRepository->listFailedMessages();
$cannotListFailedMessages = null;
} catch (FailureReceiverNotListableException) {
$cannotListFailedMessages = self::FAILURE_RECEIVER_NOT_LISTABLE;
} catch (FailureReceiverDoesNotExistException) {

if (null === $this->failedMessageRepository) {
$cannotListFailedMessages = self::NO_FAILURE_RECEIVER;
} else {
try {
$failedMessages = $this->failedMessageRepository->listFailedMessages();
$cannotListFailedMessages = null;
} catch (FailureReceiverNotListableException) {
$cannotListFailedMessages = self::FAILURE_RECEIVER_NOT_LISTABLE;
}
}

return new Response(
Expand Down
32 changes: 32 additions & 0 deletions src/Controller/NoFailureTransportController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace SymfonyCasts\MessengerMonitorBundle\Controller;

use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
* @internal
*/
final class NoFailureTransportController
{
public function __construct(
private RequestStack $requestStack,
private UrlGeneratorInterface $urlGenerator
) {
}

public function __invoke(int $id): RedirectResponse
{
/** @var FlashBagInterface $sessionBag */
$sessionBag = $this->requestStack->getSession()->getBag('flashes');

$sessionBag->add('messenger_monitor.error', 'Impossible action: a failure transport should be configured first.');

return new RedirectResponse($this->urlGenerator->generate('symfonycasts.messenger_monitor.dashboard'));
}
}
45 changes: 38 additions & 7 deletions src/DependencyInjection/FailureReceiverPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@

/**
* @internal
*
* If failure_transport configured:
* - injects failure_transport in FailureReceiverName
* - disable NoFailureTransportController
* Otherwise:
* - replace RetryFailedMessageController and RejectFailedMessageController by NoFailureTransportController
* - disable every service related to failure transport
*/
final class FailureReceiverPass implements CompilerPassInterface
{
Expand All @@ -17,19 +24,43 @@ final class FailureReceiverPass implements CompilerPassInterface
*/
public function process(ContainerBuilder $container): void
{
if (!$container->hasDefinition('symfonycasts.messenger_monitor.failed_receiver.name')) {
return;
}

$failureReceiverNameDefinition = $container->getDefinition('symfonycasts.messenger_monitor.failed_receiver.name');

// if this service does not exist, failure_transport is not configured
if (!$container->hasDefinition('console.command.messenger_failed_messages_show')) {
$failureReceiverNameDefinition->replaceArgument(0, null);
$this->switchFailureTransportControllers($container);
$this->disableFailureTransportServices($container);

return;
}

$consumeCommandDefinition = $container->getDefinition('console.command.messenger_failed_messages_show');
$failureReceiverNameDefinition = $container->getDefinition('symfonycasts.messenger_monitor.failed_receiver.name');
$failureReceiverNameDefinition->replaceArgument(0, $consumeCommandDefinition->getArgument(0));

$container->removeDefinition('symfonycasts.messenger_monitor.controller.no_failure_transport');
}

private function switchFailureTransportControllers(ContainerBuilder $container): void
{
$container->removeDefinition('symfonycasts.messenger_monitor.controller.retry_failed_message');
$container->removeDefinition('symfonycasts.messenger_monitor.controller.reject_failed_message');

$container->setAlias(
'symfonycasts.messenger_monitor.controller.retry_failed_message',
'symfonycasts.messenger_monitor.controller.no_failure_transport'
)->setPublic(true);

$container->setAlias(
'symfonycasts.messenger_monitor.controller.reject_failed_message',
'symfonycasts.messenger_monitor.controller.no_failure_transport'
)->setPublic(true);
}

private function disableFailureTransportServices(ContainerBuilder $container): void
{
$container->removeDefinition('symfonycasts.messenger_monitor.failed_message.reject');
$container->removeDefinition('symfonycasts.messenger_monitor.failed_message.retry');
$container->removeDefinition('symfonycasts.messenger_monitor.failed_message.repository');
$container->removeDefinition('symfonycasts.messenger_monitor.failed_receiver.provider');
$container->removeDefinition('symfonycasts.messenger_monitor.failed_receiver.name');
}
}
18 changes: 0 additions & 18 deletions src/Exception/FailureReceiverDoesNotExistException.php

This file was deleted.

1 change: 1 addition & 0 deletions src/FailedMessage/FailedMessageDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

/**
* @internal
* @psalm-immutable
*/
final class FailedMessageDetails
{
Expand Down
1 change: 1 addition & 0 deletions src/FailedMessage/MessageRetriedByUserEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

/**
* @internal
* @psalm-immutable
*/
final class MessageRetriedByUserEvent
{
Expand Down
4 changes: 2 additions & 2 deletions src/FailureReceiver/FailureReceiverName.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
*/
final class FailureReceiverName
{
public function __construct(private ?string $failureReceiverName)
public function __construct(private string $failureReceiverName)
{
}

public function toString(): ?string
public function toString(): string
{
return $this->failureReceiverName;
}
Expand Down
5 changes: 0 additions & 5 deletions src/FailureReceiver/FailureReceiverProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace SymfonyCasts\MessengerMonitorBundle\FailureReceiver;

use Symfony\Component\Messenger\Transport\Receiver\ListableReceiverInterface;
use SymfonyCasts\MessengerMonitorBundle\Exception\FailureReceiverDoesNotExistException;
use SymfonyCasts\MessengerMonitorBundle\Exception\FailureReceiverNotListableException;
use SymfonyCasts\MessengerMonitorBundle\Locator\ReceiverLocator;

Expand All @@ -22,10 +21,6 @@ public function __construct(private ReceiverLocator $receiverLocator, private Fa

public function getFailureReceiver(): ListableReceiverInterface
{
if (null === $this->failureReceiverName->toString()) {
throw new FailureReceiverDoesNotExistException();
}

$failureReceiver = $this->receiverLocator->getReceiver($this->failureReceiverName->toString());

if (!$failureReceiver instanceof ListableReceiverInterface) {
Expand Down
9 changes: 7 additions & 2 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<service id="symfonycasts.messenger_monitor.controller.dashboard" class="SymfonyCasts\MessengerMonitorBundle\Controller\DashboardController" public="true">
<argument type="service" id="twig"/>
<argument type="service" id="symfonycasts.messenger_monitor.receiver_locator"/>
<argument type="service" id="symfonycasts.messenger_monitor.failed_message.repository"/>
<argument type="service" id="symfonycasts.messenger_monitor.statistics.processor"/>
<argument type="service" id="symfonycasts.messenger_monitor.failed_message.repository" on-invalid="null"/>
</service>

<service id="symfonycasts.messenger_monitor.controller.retry_failed_message" class="SymfonyCasts\MessengerMonitorBundle\Controller\RetryFailedMessageController" public="true">
Expand All @@ -25,6 +25,11 @@
<argument type="service" id="router"/>
</service>

<service id="symfonycasts.messenger_monitor.controller.no_failure_transport" class="SymfonyCasts\MessengerMonitorBundle\Controller\NoFailureTransportController" public="true">
<argument type="service" id="request_stack"/>
<argument type="service" id="router"/>
</service>

<!-- Locators -->
<service id="symfonycasts.messenger_monitor.receiver_locator" class="SymfonyCasts\MessengerMonitorBundle\Locator\ReceiverLocator">
<argument type="service" id="messenger.receiver_locator"/>
Expand Down Expand Up @@ -70,7 +75,7 @@
</service>

<service
id="symfony_casts_messenger_monitor.listener.send_event_on_retried_message"
id="symfonycasts_messenger_monitor.listener.send_event_on_retried_message"
decorates="messenger.retry.send_failed_message_for_retry_listener"
decoration-inner-name="messenger.retry.send_failed_message_for_retry_listener.inner"
class="SymfonyCasts\MessengerMonitorBundle\EventListener\SendEventOnRetriedMessageListener">
Expand Down
12 changes: 6 additions & 6 deletions src/Statistics/MetricsPerMessageType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public function __construct(
private \DateTimeImmutable $toDate,
private string $class,
private int $messagesCountOnPeriod,
private float $averageWaitingTime,
private float $averageHandlingTime
private ?float $averageWaitingTime,
private ?float $averageHandlingTime
) {
}

Expand All @@ -34,14 +34,14 @@ public function getMessagesHandledPerHour(): float
return round($this->getMessagesCount() / $this->getNbHoursInPeriod(), 2);
}

public function getAverageWaitingTime(): float
public function getAverageWaitingTime(): ?float
{
return round($this->averageWaitingTime, 2);
return null !== $this->averageWaitingTime ? round($this->averageWaitingTime, 6) : null;
}

public function getAverageHandlingTime(): float
public function getAverageHandlingTime(): ?float
{
return round($this->averageHandlingTime, 2);
return null !== $this->averageHandlingTime ? round($this->averageHandlingTime, 6) : null;
}

private function getNbHoursInPeriod(): float
Expand Down
Loading