Repository navigation
Replace queue commands with queue:consume #353
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
Open
samdark
wants to merge
5
commits into
master
Choose a base branch
from
issue-350-consume
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d6a3335
Replace queue commands with consume #350
samdark 9dd20ed
Cover negative consume pause fallback #350
samdark e6bda37
Remove command migration section #350
samdark 3957762
Reject ignored consume options and clarify help
samdark f979513
Merge branch 'master' into issue-350-consume
samdark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Queue\Command; | ||
|
|
||
| use Symfony\Component\Console\Attribute\AsCommand; | ||
| use Symfony\Component\Console\Command\Command; | ||
| use Symfony\Component\Console\Input\InputArgument; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Input\InputOption; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
| use Yiisoft\Queue\Cli\LoopInterface; | ||
| use Yiisoft\Queue\Cli\ScopedLoopInterface; | ||
| use Yiisoft\Queue\DefaultQueue; | ||
| use Yiisoft\Queue\Provider\QueueConsumerProviderInterface; | ||
|
|
||
| use function count; | ||
|
|
||
| #[AsCommand('queue:consume', 'Consumes queued messages. Listens continuously unless --once is passed.')] | ||
| final class ConsumeCommand extends Command | ||
| { | ||
| public function __construct( | ||
| private readonly QueueConsumerProviderInterface $queueProvider, | ||
| private readonly LoopInterface $loop, | ||
| ) { | ||
| parent::__construct(); | ||
| } | ||
|
|
||
| public function configure(): void | ||
| { | ||
| $this | ||
| ->addArgument('queue', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Queue names to consume.') | ||
| ->addOption( | ||
| 'once', | ||
| null, | ||
| InputOption::VALUE_NONE, | ||
| 'Process existing messages in one pass and exit. Without names, use all configured consumer queues.', | ||
| ) | ||
| ->addOption( | ||
| 'all', | ||
| null, | ||
| InputOption::VALUE_NONE, | ||
| 'Continuously poll queues. Without names, use all configured consumer queues. ' | ||
| . 'Recommended for development only.', | ||
| ) | ||
| ->addOption( | ||
| 'limit', | ||
| 'm', | ||
| InputOption::VALUE_REQUIRED, | ||
| 'Maximum messages per queue in each pass with --once or --all. Default: 0 (no limit).', | ||
| ) | ||
| ->addOption( | ||
| 'pause', | ||
| 'p', | ||
| InputOption::VALUE_REQUIRED, | ||
| 'Seconds between idle passes with --all without --once. Default: 1.', | ||
| ) | ||
| ->addUsage('[queue]') | ||
| ->addUsage('--once [queue1 [queue2 [...]]] --limit=100') | ||
| ->addUsage('--all [queue1 [queue2 [...]]] --pause=1 --limit=100') | ||
| ->setHelp( | ||
| 'Without queue names, listen to ' . DefaultQueue::NAME . '. ' | ||
| . '--once and --all instead use all configured consumer queues. ' | ||
| . '--once takes precedence over --all.', | ||
| ); | ||
| } | ||
|
|
||
| protected function execute(InputInterface $input, OutputInterface $output): int | ||
| { | ||
| /** @var string[] $queueNames */ | ||
| $queueNames = $input->getArgument('queue'); | ||
| $once = (bool) $input->getOption('once'); | ||
| $all = (bool) $input->getOption('all'); | ||
| $limit = $input->getOption('limit'); | ||
| $pauseSeconds = $input->getOption('pause'); | ||
|
|
||
| if (!$once && !$all && $limit !== null) { | ||
| $output->writeln('<error>--limit requires --once or --all.</error>'); | ||
|
|
||
| return Command::INVALID; | ||
| } | ||
|
|
||
| if ((!$all || $once) && $pauseSeconds !== null) { | ||
| $output->writeln('<error>--pause requires --all without --once.</error>'); | ||
|
|
||
| return Command::INVALID; | ||
| } | ||
|
|
||
| if (!$once && !$all) { | ||
| if (count($queueNames) > 1) { | ||
| $output->writeln('<error>Use --all or --once to consume multiple queues.</error>'); | ||
|
|
||
| return Command::INVALID; | ||
| } | ||
|
|
||
| $this->queueProvider->getConsumer($queueNames[0] ?? DefaultQueue::NAME)->listen(); | ||
|
|
||
| return Command::SUCCESS; | ||
| } | ||
|
|
||
| if ($queueNames === []) { | ||
| $queueNames = $this->queueProvider->getConsumerQueueNames(); | ||
| } | ||
|
|
||
| $consumers = []; | ||
| foreach ($queueNames as $queueName) { | ||
| $consumers[$queueName] = $this->queueProvider->getConsumer($queueName); | ||
| } | ||
|
|
||
| if ($consumers === []) { | ||
| $output->writeln('No consumers are configured.'); | ||
|
|
||
| return Command::SUCCESS; | ||
| } | ||
|
|
||
| $limit = (int) $limit; | ||
| $pauseSeconds = (int) ($pauseSeconds ?? 1); | ||
| if ($pauseSeconds < 0) { | ||
| $pauseSeconds = 1; | ||
| } | ||
|
|
||
| if ($this->loop instanceof ScopedLoopInterface) { | ||
| $this->loop->start(); | ||
| } | ||
|
|
||
| try { | ||
| while ($once || $this->loop->canContinue()) { | ||
| $hasMessages = false; | ||
| foreach ($consumers as $queueName => $consumer) { | ||
| if ($once) { | ||
| $output->write("Processing queue $queueName... "); | ||
| } | ||
|
|
||
| $count = $consumer->run($limit); | ||
| $hasMessages = $count > 0 || $hasMessages; | ||
|
|
||
| if ($once) { | ||
| $output->writeln("Messages processed: $count."); | ||
| } | ||
| } | ||
|
|
||
| if ($once) { | ||
| break; | ||
| } | ||
|
|
||
| if (!$hasMessages && $this->loop->canContinue()) { | ||
| /** @psalm-var 0|positive-int $pauseSeconds */ | ||
| sleep($pauseSeconds); | ||
| } | ||
| } | ||
| } finally { | ||
| if ($this->loop instanceof ScopedLoopInterface) { | ||
| $this->loop->finish(); | ||
| } | ||
| } | ||
|
|
||
| return Command::SUCCESS; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.