Skip to content

Commit

Permalink
Merge pull request #103 from kbond/ui/retry-failed
Browse files Browse the repository at this point in the history
[RFC] feat: allow retrying failed messages
  • Loading branch information
kbond authored Nov 12, 2024
2 parents 8c930fb + 678f28b commit 4467faf
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
28 changes: 28 additions & 0 deletions src/Controller/MessengerMonitorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\Messenger\Message\RedispatchMessage;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Stamp\SentToFailureTransportStamp;
use Symfony\Component\Messenger\Stamp\TransportNamesStamp;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Scheduler\Generator\MessageContext;
Expand Down Expand Up @@ -169,6 +170,33 @@ public function removeTransportMessage(
return $this->redirectToRoute('zenstruck_messenger_monitor_transport', ['name' => $name]);
}

#[Route('/transport/{name}/{id}/retry', name: 'zenstruck_messenger_monitor_transport_retry', methods: 'POST')]
public function retryFailedMessage(
string $name,
string $id,
Request $request,
ViewHelper $helper,
MessageBusInterface $bus,
): Response {
if (!$this->isCsrfTokenValid(\sprintf('retry-%s-%s', $id, $name), $request->request->getString('_token'))) {
throw new HttpException(419, 'Invalid CSRF token.');
}

$transport = $helper->transports->get($name);
$message = $transport->find($id) ?? throw $this->createNotFoundException('Message not found.');
$originalTransportName = $message->envelope()->last(SentToFailureTransportStamp::class)?->getOriginalReceiverName() ?? throw $this->createNotFoundException('Original transport not found.');

$bus->dispatch($message->envelope(), [
new TagStamp('retry'),
new TagStamp('manual'),
]);
$transport->get()->reject($message->envelope());

$this->addFlash('success', \sprintf('Retrying message "%s" on transport "%s".', $message->message()->shortName(), $originalTransportName));

return $this->redirectToRoute('zenstruck_messenger_monitor_transport', ['name' => $name]);
}

#[Route('/schedule/{name}', name: 'zenstruck_messenger_monitor_schedule', defaults: ['name' => null])]
public function schedules(
ViewHelper $helper,
Expand Down
11 changes: 9 additions & 2 deletions templates/transport.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,24 @@
{% if transport.isFailure %}
<td>
{% if queued.exception %}
<strong class="text-danger"><abbr title="{{ queued.exception }}">{{ queued.exception.shortName }}</abbr>: {{ queued.exception.description }}</strong>
<strong class="text-danger"><abbr title="{{ queued.exception }}">{{ queued.exception.shortName }}</abbr></strong>
<br><small class="text-secondary">{{ queued.exception.description }}</small>
{% else %}
<em class="text-secondary">n/a</em>
{% endif %}
</td>
{% endif %}
<td>
<td class="text-end">
<a href="{{ path('zenstruck_messenger_monitor_transport_remove', {name: transport.name, id: queued.id}) }}" class="post-link btn btn-sm btn-outline-danger" data-confirm="Are you sure?" data-token="{{ csrf_token(['remove', queued.id, transport.name]|join('-')) }}">
<svg fill="currentcolor" height="1em" width="1em" class="me-1 align-text-bottom" role="img" aria-label="Info:"><use xlink:href="#trash-icon"/></svg>
Remove
</a>
{% if transport.isFailure %}
<a href="{{ path('zenstruck_messenger_monitor_transport_retry', {name: transport.name, id: queued.id}) }}" class="post-link btn btn-sm btn-outline-secondary" data-token="{{ csrf_token(['retry', queued.id, transport.name]|join('-')) }}">
<svg fill="currentcolor" height="1em" width="1em" class="me-1 align-text-bottom" role="img" aria-label="Info:"><use xlink:href="#refresh-icon"/></svg>
Retry
</a>
{% endif %}
</td>
</tr>
{% endfor %}
Expand Down

0 comments on commit 4467faf

Please sign in to comment.