Skip to content

[Console][Messenger] Asynchronously notify transports which messages are still being processed #53508

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

Merged
merged 1 commit into from
Oct 7, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.2
---

* Implement the `KeepaliveReceiverInterface` to enable asynchronously notifying Beanstalkd that the job is still being processed, in order to avoid timeouts

5.2.0
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Messenger\Bridge\Beanstalkd\Transport\BeanstalkdReceivedStamp;
use Symfony\Component\Messenger\Bridge\Beanstalkd\Transport\BeanstalkdReceiver;
use Symfony\Component\Messenger\Bridge\Beanstalkd\Transport\Connection;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\MessageDecodingFailedException;
use Symfony\Component\Messenger\Transport\Serialization\PhpSerializer;
use Symfony\Component\Messenger\Transport\Serialization\Serializer;
Expand Down Expand Up @@ -78,6 +79,17 @@ public function testItRejectTheMessageIfThereIsAMessageDecodingFailedException()
$receiver->get();
}

public function testKeepalive()
{
$serializer = $this->createSerializer();

$connection = $this->createMock(Connection::class);
$connection->expects($this->once())->method('keepalive')->with(1);

$receiver = new BeanstalkdReceiver($connection, $serializer);
$receiver->keepalive(new Envelope(new DummyMessage('foo'), [new BeanstalkdReceivedStamp(1, 'bar')]));
}

private function createBeanstalkdEnvelope(): array
{
return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Bridge\Beanstalkd\Tests\Fixtures\DummyMessage;
use Symfony\Component\Messenger\Bridge\Beanstalkd\Transport\BeanstalkdReceivedStamp;
use Symfony\Component\Messenger\Bridge\Beanstalkd\Transport\BeanstalkdTransport;
use Symfony\Component\Messenger\Bridge\Beanstalkd\Transport\Connection;
use Symfony\Component\Messenger\Envelope;
Expand Down Expand Up @@ -50,6 +51,18 @@ public function testReceivesMessages()
$this->assertSame($decodedMessage, $envelopes[0]->getMessage());
}

public function testKeepalive()
{
$transport = $this->getTransport(
null,
$connection = $this->createMock(Connection::class),
);

$connection->expects($this->once())->method('keepalive')->with(1);

$transport->keepalive(new Envelope(new DummyMessage('foo'), [new BeanstalkdReceivedStamp(1, 'bar')]));
}

private function getTransport(?SerializerInterface $serializer = null, ?Connection $connection = null): BeanstalkdTransport
{
$serializer ??= $this->createMock(SerializerInterface::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,4 +330,37 @@ public function testSendWhenABeanstalkdExceptionOccurs()

$connection->send($body, $headers, $delay);
}

public function testKeepalive()
{
$id = 123456;

$tube = 'baz';

$client = $this->createMock(PheanstalkInterface::class);
$client->expects($this->once())->method('useTube')->with($tube)->willReturn($client);
$client->expects($this->once())->method('touch')->with($this->callback(fn (JobId $jobId): bool => $jobId->getId() === $id));

$connection = new Connection(['tube_name' => $tube], $client);

$connection->keepalive((string) $id);
}

public function testKeepaliveWhenABeanstalkdExceptionOccurs()
{
$id = 123456;

$tube = 'baz123';

$exception = new ServerException('baz error');

$client = $this->createMock(PheanstalkInterface::class);
$client->expects($this->once())->method('useTube')->with($tube)->willReturn($client);
$client->expects($this->once())->method('touch')->with($this->callback(fn (JobId $jobId): bool => $jobId->getId() === $id))->willThrowException($exception);

$connection = new Connection(['tube_name' => $tube], $client);

$this->expectExceptionObject(new TransportException($exception->getMessage(), 0, $exception));
$connection->keepalive((string) $id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\LogicException;
use Symfony\Component\Messenger\Exception\MessageDecodingFailedException;
use Symfony\Component\Messenger\Transport\Receiver\KeepaliveReceiverInterface;
use Symfony\Component\Messenger\Transport\Receiver\MessageCountAwareInterface;
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface;
use Symfony\Component\Messenger\Transport\Serialization\PhpSerializer;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;

/**
* @author Antonio Pauletich <antonio.pauletich95@gmail.com>
*/
class BeanstalkdReceiver implements ReceiverInterface, MessageCountAwareInterface
class BeanstalkdReceiver implements KeepaliveReceiverInterface, MessageCountAwareInterface
{
private SerializerInterface $serializer;

Expand Down Expand Up @@ -65,6 +65,11 @@ public function reject(Envelope $envelope): void
$this->connection->reject($this->findBeanstalkdReceivedStamp($envelope)->getId());
}

public function keepalive(Envelope $envelope): void
{
$this->connection->keepalive($this->findBeanstalkdReceivedStamp($envelope)->getId());
}

public function getMessageCount(): int
{
return $this->connection->getMessageCount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Messenger\Bridge\Beanstalkd\Transport;

use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Transport\Receiver\KeepaliveReceiverInterface;
use Symfony\Component\Messenger\Transport\Receiver\MessageCountAwareInterface;
use Symfony\Component\Messenger\Transport\Serialization\PhpSerializer;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
Expand All @@ -20,7 +21,7 @@
/**
* @author Antonio Pauletich <antonio.pauletich95@gmail.com>
*/
class BeanstalkdTransport implements TransportInterface, MessageCountAwareInterface
class BeanstalkdTransport implements TransportInterface, KeepaliveReceiverInterface, MessageCountAwareInterface
{
private SerializerInterface $serializer;
private BeanstalkdReceiver $receiver;
Expand Down Expand Up @@ -48,6 +49,11 @@ public function reject(Envelope $envelope): void
$this->getReceiver()->reject($envelope);
}

public function keepalive(Envelope $envelope): void
{
$this->getReceiver()->keepalive($envelope);
}

public function getMessageCount(): int
{
return $this->getReceiver()->getMessageCount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,15 @@ public function reject(string $id): void
}
}

public function keepalive(string $id): void
{
try {
$this->client->useTube($this->tube)->touch(new JobId((int) $id));
} catch (Exception $exception) {
throw new TransportException($exception->getMessage(), 0, $exception);
}
}

public function getMessageCount(): int
{
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"require": {
"php": ">=8.2",
"pda/pheanstalk": "^4.0",
"symfony/messenger": "^6.4|^7.0"
"symfony/messenger": "^7.2"
},
"require-dev": {
"symfony/property-access": "^6.4|^7.0",
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ CHANGELOG
* Add `--format` option to the `messenger:stats` command
* Add `getRetryDelay()` method to `RecoverableExceptionInterface`
* Add `skip` option to `messenger:failed:retry` command when run interactively to skip message and requeue it
* Add the ability to asynchronously notify transports about which messages are still being processed by the worker, using `pcntl_alarm()`

7.1
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
#[AsCommand(name: 'messenger:consume', description: 'Consume messages')]
class ConsumeMessagesCommand extends Command implements SignalableCommandInterface
{
private const DEFAULT_KEEPALIVE_INTERVAL = 5;

private ?Worker $worker = null;

public function __construct(
Expand Down Expand Up @@ -75,6 +77,7 @@ protected function configure(): void
new InputOption('queues', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Limit receivers to only consume from the specified queues'),
new InputOption('no-reset', null, InputOption::VALUE_NONE, 'Do not reset container services after each message'),
new InputOption('all', null, InputOption::VALUE_NONE, 'Consume messages from all receivers'),
new InputOption('keepalive', null, InputOption::VALUE_OPTIONAL, 'Whether to use the transport\'s keepalive mechanism if implemented', self::DEFAULT_KEEPALIVE_INTERVAL),
])
->setHelp(<<<'EOF'
The <info>%command.name%</info> command consumes messages and dispatches them to the message bus.
Expand Down Expand Up @@ -124,6 +127,13 @@ protected function configure(): void
;
}

protected function initialize(InputInterface $input, OutputInterface $output): void
{
if ($input->hasParameterOption('--keepalive')) {
$this->getApplication()->setAlarmInterval((int) ($input->getOption('keepalive') ?? self::DEFAULT_KEEPALIVE_INTERVAL));
}
}

protected function interact(InputInterface $input, OutputInterface $output): void
{
$io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);
Expand Down Expand Up @@ -264,7 +274,7 @@ public function complete(CompletionInput $input, CompletionSuggestions $suggesti

public function getSubscribedSignals(): array
{
return $this->signals ?? (\extension_loaded('pcntl') ? [\SIGTERM, \SIGINT, \SIGQUIT] : []);
return $this->signals ?? (\extension_loaded('pcntl') ? [\SIGTERM, \SIGINT, \SIGQUIT, \SIGALRM] : []);
}

public function handleSignal(int $signal, int|false $previousExitCode = 0): int|false
Expand All @@ -273,6 +283,14 @@ public function handleSignal(int $signal, int|false $previousExitCode = 0): int|
return false;
}

if (\SIGALRM === $signal) {
$this->logger?->info('Sending keepalive request.', ['transport_names' => $this->worker->getMetadata()->getTransportNames()]);

$this->worker->keepalive();

return false;
}

$this->logger?->info('Received signal {signal}.', ['signal' => $signal, 'transport_names' => $this->worker->getMetadata()->getTransportNames()]);

$this->worker->stop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
#[AsCommand(name: 'messenger:failed:retry', description: 'Retry one or more messages from the failure transport')]
class FailedMessagesRetryCommand extends AbstractFailedMessagesCommand implements SignalableCommandInterface
{
private const DEFAULT_KEEPALIVE_INTERVAL = 5;

private bool $shouldStop = false;
private bool $forceExit = false;
private ?Worker $worker = null;
Expand All @@ -64,6 +66,7 @@ protected function configure(): void
new InputArgument('id', InputArgument::IS_ARRAY, 'Specific message id(s) to retry'),
new InputOption('force', null, InputOption::VALUE_NONE, 'Force action without confirmation'),
new InputOption('transport', null, InputOption::VALUE_OPTIONAL, 'Use a specific failure transport', self::DEFAULT_TRANSPORT_OPTION),
new InputOption('keepalive', null, InputOption::VALUE_OPTIONAL, 'Whether to use the transport\'s keepalive mechanism if implemented', self::DEFAULT_KEEPALIVE_INTERVAL),
])
->setHelp(<<<'EOF'
The <info>%command.name%</info> retries message in the failure transport.
Expand All @@ -87,6 +90,13 @@ protected function configure(): void
;
}

protected function initialize(InputInterface $input, OutputInterface $output): void
{
if ($input->hasParameterOption('--keepalive')) {
$this->getApplication()->setAlarmInterval((int) ($input->getOption('keepalive') ?? self::DEFAULT_KEEPALIVE_INTERVAL));
}
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->eventDispatcher->addSubscriber(new StopWorkerOnMessageLimitListener(1));
Expand Down Expand Up @@ -134,7 +144,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

public function getSubscribedSignals(): array
{
return $this->signals ?? (\extension_loaded('pcntl') ? [\SIGTERM, \SIGINT, \SIGQUIT] : []);
return $this->signals ?? (\extension_loaded('pcntl') ? [\SIGTERM, \SIGINT, \SIGQUIT, \SIGALRM] : []);
}

public function handleSignal(int $signal, int|false $previousExitCode = 0): int|false
Expand All @@ -143,6 +153,14 @@ public function handleSignal(int $signal, int|false $previousExitCode = 0): int|
return false;
}

if (\SIGALRM === $signal) {
$this->logger?->info('Sending keepalive request.', ['transport_names' => $this->worker->getMetadata()->getTransportNames()]);

$this->worker->keepalive();

return false;
}

$this->logger?->info('Received signal {signal}.', ['signal' => $signal, 'transport_names' => $this->worker->getMetadata()->getTransportNames()]);

$this->worker->stop();
Expand Down
Loading