Skip to content

Commit

Permalink
feat: allow retrying failed messages
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond committed Nov 12, 2024
1 parent 8c930fb commit 187b6b0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
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
8 changes: 7 additions & 1 deletion templates/transport.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,17 @@
{% 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 187b6b0

Please sign in to comment.