8000 [Messenger] Add a way to no ack message automatically · symfony/symfony@89bb008 · GitHub
[go: up one dir, main page]

Skip to content

Commit 89bb008

Browse files
committed
[Messenger] Add a way to no ack message automatically
1 parent 073501a commit 89bb008

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\Handler;
13+
14+
use Symfony\Component\Messenger\Envelope;
15+
16+
/**
17+
* Marker interface for message handlers to configure if auto ACK is disabled.
18+
*
19+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
20+
*/
21+
interface ConfigureAutoAckInterface
22+
{
23+
public function disableAutoAck(Envelope $envelope): bool;
24+
}

src/Symfony/Component/Messenger/Middleware/HandleMessageMiddleware.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
use Symfony\Component\Messenger\Envelope;
1717
use Symfony\Component\Messenger\Exception\HandlerFailedException;
1818
use Symfony\Component\Messenger\Exception\NoHandlerForMessageException;
19+
use Symfony\Component\Messenger\Handler\ConfigureAutoAckInterface;
1920
use Symfony\Component\Messenger\Handler\HandlerDescriptor;
2021
use Symfony\Component\Messenger\Handler\HandlersLocatorInterface;
22+
use Symfony\Component\Messenger\Stamp\DelayedAckStamp;
2123
use Symfony\Component\Messenger\Stamp\HandledStamp;
2224

2325
/**
@@ -60,7 +62,10 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope
6062

6163
try {
6264
$handler = $handlerDescriptor->getHandler();
63-
$handledStamp = HandledStamp::fromDescriptor($handlerDescriptor, $handler($message));
65+
if ($handler instanceof ConfigureAutoAckInterface && $handler->disableAutoAck($envelope)) {
66+
$envelope = $envelope->with(new DelayedAckStamp());
67+
}
68+
$handledStamp = HandledStamp::fromDescriptor($handlerDescriptor, $handler($message, $envelope));
6469
$envelope = $envelope->with($handledStamp);
6570
$this->logger->info('Message {class} handled by {handler}', $context + ['handler' => $handledStamp->getHandlerName()]);
6671
} catch (\Throwable $e) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\Stamp;
13+
14+
/**
15+
* Apply this stamp to delay ACK of your message on a transport.
16+
*/
17+
final class DelayedAckStamp implements StampInterface
18+
{
19+
}

src/Symfony/Component/Messenger/Worker.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Symfony\Component\Messenger\Exception\RejectRedeliveredMessageException;
2525
use Symfony\Component\Messenger\Exception\RuntimeException;
2626
use Symfony\Component\Messenger\Stamp\ConsumedByWorkerStamp;
27+
use Symfony\Component\Messenger\Stamp\DelayedAckStamp;
2728
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
2829
use Symfony\Component\Messenger\Transport\Receiver\QueueReceiverInterface;
2930
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface;
@@ -165,7 +166,9 @@ private function handleMessage(Envelope $envelope, ReceiverInterface $receiver,
165166
$this->logger->info('{class} was handled successfully (acknowledging to transport).', $context);
166167
}
167168

168-
$receiver->ack($envelope);
169+
if (null === $envelope->last(DelayedAckStamp::class)) {
170+
$receiver->ack($envelope);
171+
}
169172
}
170173

171174
public function stop(): void

0 commit comments

Comments
 (0)
0