Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Messenger] Add a way to no ack message automatically #42873

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

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions src/Symfony/Component/Messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ CHANGELOG
* Added `WorkerMetadata` class which allows you to access the configuration details of a worker, like `queueNames` and `transportNames` it consumes from.
* New method `getMetadata()` was added to `Worker` class which returns the `WorkerMetadata` object.
* Deprecate not setting the `reset_on_message` config option, its default value will change to `true` in 6.0
* Add `ConfigurableAutoAckInterface` and `DelayedAckStamp` to not automatically ACK message.

5.3
---
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Messenger\Handler;

use Symfony\Component\Messenger\Envelope;

/**
* Marker interface for message handlers to configure if auto ACK is disabled.
*
* @author Grégoire Pineau <[email protected]>
*/
interface ConfigurableAutoAckInterface
{
public function isAutoAckDisabled(Envelope $envelope): bool;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\HandlerFailedException;
use Symfony\Component\Messenger\Exception\NoHandlerForMessageException;
use Symfony\Component\Messenger\Handler\ConfigurableAutoAckInterface;
use Symfony\Component\Messenger\Handler\HandlerDescriptor;
use Symfony\Component\Messenger\Handler\HandlersLocatorInterface;
use Symfony\Component\Messenger\Stamp\DelayedAckStamp;
use Symfony\Component\Messenger\Stamp\HandledStamp;

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

try {
$handler = $handlerDescriptor->getHandler();
$handledStamp = HandledStamp::fromDescriptor($handlerDescriptor, $handler($message));
if ($handler instanceof ConfigurableAutoAckInterface && $handler->isAutoAckDisabled($envelope)) {
$envelope = $envelope->with(new DelayedAckStamp());
}
$handledStamp = HandledStamp::fromDescriptor($handlerDescriptor, $handler($message, $envelope));
$envelope = $envelope->with($handledStamp);
$this->logger->info('Message {class} handled by {handler}', $context + ['handler' => $handledStamp->getHandlerName()]);
} catch (\Throwable $e) {
Expand Down
19 changes: 19 additions & 0 deletions src/Symfony/Component/Messenger/Stamp/DelayedAckStamp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Messenger\Stamp;

/**
* Apply this stamp to delay ACK of your message on a transport.
*/
final class DelayedAckStamp implements StampInterface
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\HandlerFailedException;
use Symfony\Component\Messenger\Exception\NoHandlerForMessageException;
use Symfony\Component\Messenger\Handler\ConfigurableAutoAckInterface;
use Symfony\Component\Messenger\Handler\HandlerDescriptor;
use Symfony\Component\Messenger\Handler\HandlersLocator;
use Symfony\Component\Messenger\Middleware\HandleMessageMiddleware;
use Symfony\Component\Messenger\Middleware\StackMiddleware;
use Symfony\Component\Messenger\Stamp\DelayedAckStamp;
use Symfony\Component\Messenger\Stamp\HandledStamp;
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
Expand Down Expand Up @@ -114,6 +116,45 @@ public function itAddsHandledStampsProvider(): iterable
];
}

/**
* @dataProvider itAddsDelayedAckStampProvider
*/
public function testItAddsDelayedAckStamp($handler, bool $stampIsExpected)
{
$message = new DummyMessage('Hey');
$envelope = new Envelope($message);

$middleware = new HandleMessageMiddleware(new HandlersLocator([
DummyMessage::class => [$handler],
]));

try {
$envelope = $middleware->handle($envelope, $this->getStackMock(true));
} catch (HandlerFailedException $e) {
$envelope = $e->getEnvelope();
}

$this->assertSame($stampIsExpected, null !== $envelope->last(DelayedAckStamp::class));
}

public function itAddsDelayedAckStampProvider(): iterable
{
yield 'It does not add stamp by default' => [
new HandleMessageMiddlewareTestCallable(),
false,
];

yield 'It does not add when object return false' => [
new HandleMessageMiddlewareWithAckConfigurationTestCallable(false),
false,
];

yield 'It adds when object return true' => [
new HandleMessageMiddlewareWithAckConfigurationTestCallable(true),
true,
];
}

public function testThrowsNoHandlerException()
{
$this->expectException(NoHandlerForMessageException::class);
Expand All @@ -137,3 +178,22 @@ public function __invoke()
{
}
}

class HandleMessageMiddlewareWithAckConfigurationTestCallable implements ConfigurableAutoAckInterface
{
private $autoAckDisabled;

public function __construct(bool $autoAckDisabled)
{
$this->autoAckDisabled = $autoAckDisabled;
}

public function isAutoAckDisabled(Envelope $envelope): bool
{
return $this->autoAckDisabled;
}

public function __invoke()
{
}
}
19 changes: 19 additions & 0 deletions src/Symfony/Component/Messenger/Tests/WorkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Symfony\Component\Messenger\Exception\RuntimeException;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Stamp\ConsumedByWorkerStamp;
use Symfony\Component\Messenger\Stamp\DelayedAckStamp;
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
use Symfony\Component\Messenger\Stamp\SentStamp;
use Symfony\Component\Messenger\Stamp\StampInterface;
Expand Down Expand Up @@ -329,6 +330,24 @@ public function testWorkerMessageReceivedEventMutability()
$envelope = current($receiver->getAcknowledgedEnvelopes());
$this->assertCount(1, $envelope->all(\get_class($stamp)));
}

public function testWorkerDoesNotCallAckWhenDelayedAckStamp()
{
$envelope = new Envelope(new DummyMessage('Hello'));
$envelope = $envelope->with(new DelayedAckStamp());
$receiver = new DummyReceiver([[$envelope]]);

$bus = $this->createMock(MessageBusInterface::class);
$bus->method('dispatch')->willReturnArgument(0);

$dispatcher = new EventDispatcher();
$dispatcher->addSubscriber(new StopWorkerOnMessageLimitListener(1));

$worker = new Worker([$receiver], $bus, $dispatcher);
$worker->run();

$this->assertSame(0, $receiver->getAcknowledgeCount());
}
}

class DummyReceiver implements ReceiverInterface
Expand Down
5 changes: 4 additions & 1 deletion src/Symfony/Component/Messenger/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Symfony\Component\Messenger\Exception\RejectRedeliveredMessageException;
use Symfony\Component\Messenger\Exception\RuntimeException;
use Symfony\Component\Messenger\Stamp\ConsumedByWorkerStamp;
use Symfony\Component\Messenger\Stamp\DelayedAckStamp;
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
use Symfony\Component\Messenger\Transport\Receiver\QueueReceiverInterface;
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface;
Expand Down Expand Up @@ -171,7 +172,9 @@ private function handleMessage(Envelope $envelope, ReceiverInterface $receiver,
$this->logger->info('{class} was handled successfully (acknowledging to transport).', $context);
}

$receiver->ack($envelope);
if (null === $envelope->last(DelayedAckStamp::class)) {
$receiver->ack($envelope);
}
}

public function stop(): void
Expand Down