Skip to content

[Messenger] Add a memory limit option for ConsumeMessagesCommand #2

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
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
10 changes: 10 additions & 0 deletions Command/ConsumeMessagesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Transport\Enhancers\MaximumCountReceiver;
use Symfony\Component\Messenger\Transport\Enhancers\MemoryLimitReceiver;
use Symfony\Component\Messenger\Transport\ReceiverInterface;
use Symfony\Component\Messenger\Worker;

Expand Down Expand Up @@ -51,6 +52,7 @@ protected function configure()
->setDefinition(array(
new InputArgument('receiver', InputArgument::REQUIRED, 'Name of the receiver'),
new InputOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Limit the number of received messages'),
new InputOption('memory', 'm', InputOption::VALUE_REQUIRED, 'The memory limit in megabytes the worker can consume'),
))
->setDescription('Consumes messages')
->setHelp(<<<'EOF'
Expand All @@ -61,6 +63,10 @@ protected function configure()
Use the --limit option to limit the number of messages received:

<info>php %command.full_name% <receiver-name> --limit=10</info>

Use the --memory option to limit the memory consumed by the worker:

<info>php %command.full_name% <receiver-name> --memory=128</info>
EOF
)
;
Expand All @@ -83,6 +89,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
$receiver = new MaximumCountReceiver($receiver, $limit);
}

if ($memory = $input->getOption('memory')) {
$receiver = new MemoryLimitReceiver($receiver, $memory);
}

$worker = new Worker($receiver, $this->bus);
$worker->run();
}
Expand Down
45 changes: 45 additions & 0 deletions Transport/Enhancers/MemoryLimitReceiver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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\Transport\Enhancers;

use Symfony\Component\Messenger\Transport\ReceiverInterface;

/**
* @author Simon Delicata <[email protected]>
*/
class MemoryLimitReceiver implements ReceiverInterface
{
private $decoratedReceiver;
private $memoryLimit;

public function __construct(ReceiverInterface $decoratedReceiver, int $memoryLimit)
{
$this->decoratedReceiver = $decoratedReceiver;
$this->memoryLimit = $memoryLimit;
}

public function receive(callable $handler): void
{
$this->decoratedReceiver->receive(function($message) use ($handler) {
$handler($message);

if (memory_get_usage() / 1024 / 1024 >= $this->memoryLimit) {
$this->stop();
}
});
}

public function stop(): void
{
$this->decoratedReceiver->stop();
}
}