77use Symfony \Component \Console \Command \Command ;
88use Symfony \Component \Console \Input \InputArgument ;
99use Symfony \Component \Console \Input \InputInterface ;
10+ use Symfony \Component \Console \Input \InputOption ;
1011use Symfony \Component \Console \Output \OutputInterface ;
11- use Yiisoft \Queue \QueueFactory ;
1212use Yiisoft \Queue \QueueFactoryInterface ;
1313
1414final class RunCommand extends Command
1515{
1616 protected static $ defaultName = 'queue:run ' ;
17- protected static $ defaultDescription = 'Runs all the existing messages in the queue. Exits once messages are over. ' ;
17+ protected static $ defaultDescription = 'Runs all the existing messages in the given queues. ' .
18+ 'Exits once messages are over. ' ;
1819
19- public function __construct (private QueueFactoryInterface $ queueFactory )
20+ public function __construct (private QueueFactoryInterface $ queueFactory, private array $ channels )
2021 {
2122 parent ::__construct ();
2223 }
@@ -25,17 +26,31 @@ public function configure(): void
2526 {
2627 $ this ->addArgument (
2728 'channel ' ,
28- InputArgument::OPTIONAL ,
29- 'Queue channel name to connect to ' ,
30- QueueFactory::DEFAULT_CHANNEL_NAME
31- );
29+ InputArgument::OPTIONAL | InputArgument::IS_ARRAY ,
30+ 'Queue channel name list to connect to. ' ,
31+ $ this ->channels ,
32+ )
33+ ->addOption (
34+ 'maximum ' ,
35+ 'm ' ,
36+ InputOption::VALUE_REQUIRED ,
37+ 'Maximum number of messages to process in each channel. Default is 0 (no limits). ' ,
38+ 0 ,
39+ )
40+ ->addUsage ('[channel1 [channel2 [...]]] --maximum 100 ' );
3241 }
3342
3443 protected function execute (InputInterface $ input , OutputInterface $ output ): int
3544 {
36- $ this ->queueFactory
37- ->get ($ input ->getArgument ('channel ' ))
38- ->run ();
45+ /** @var string $channel */
46+ foreach ($ input ->getArgument ('channel ' ) as $ channel ) {
47+ $ output ->write ("Processing channel $ channel... " );
48+ $ count = $ this ->queueFactory
49+ ->get ($ channel )
50+ ->run ((int )$ input ->getOption ('maximum ' ));
51+
52+ $ output ->writeln ("Messages processed: $ count. " );
53+ }
3954
4055 return 0 ;
4156 }
0 commit comments