22
33namespace Ensi \LaravelPhpRdKafkaConsumer \Commands ;
44
5+ use Ensi \LaravelPhpRdKafka \KafkaFacade ;
56use Ensi \LaravelPhpRdKafkaConsumer \ConsumerOptions ;
67use Ensi \LaravelPhpRdKafkaConsumer \HighLevelConsumer ;
78use Ensi \LaravelPhpRdKafkaConsumer \ProcessorData ;
@@ -15,7 +16,7 @@ class KafkaConsumeCommand extends Command implements SignalableCommandInterface
1516 * The name and signature of the console command.
1617 */
1718 protected $ signature = 'kafka:consume
18- {topic : The name of the topic}
19+ {topic-key : The key of a topic in the kafka.topics list }
1920 {consumer=default : The name of the consumer}
2021 {--max-events=0 : The number of events to consume before stopping}
2122 {--max-time=0 : The maximum number of seconds the worker should run}
@@ -53,7 +54,7 @@ public function handleSignal(int $signal): void
5354 public function handle (HighLevelConsumer $ highLevelConsumer ): int
5455 {
5556 $ this ->consumer = $ highLevelConsumer ;
56- $ topic = $ this ->argument ('topic ' );
57+ $ topicKey = $ this ->argument ('topic-key ' );
5758 $ consumer = $ this ->argument ('consumer ' );
5859 $ availableConsumers = array_keys (config ('kafka.consumers ' , []));
5960
@@ -64,9 +65,9 @@ public function handle(HighLevelConsumer $highLevelConsumer): int
6465 return 1 ;
6566 }
6667
67- $ processorData = $ this ->findMatchedProcessor ($ topic , $ consumer );
68+ $ processorData = $ this ->findMatchedProcessor ($ topicKey , $ consumer );
6869 if (is_null ($ processorData )) {
69- $ this ->error ("Processor for topic \"$ topic \" and consumer \"$ consumer \" is not found " );
70+ $ this ->error ("Processor for topic-key \"$ topicKey \" and consumer \"$ consumer \" is not found " );
7071 $ this ->line ('Processors are set in /config/kafka-consumers.php ' );
7172
7273 return 1 ;
@@ -93,12 +94,13 @@ public function handle(HighLevelConsumer $highLevelConsumer): int
9394 middleware: $ this ->collectMiddleware ($ consumerPackageOptions ['middleware ' ] ?? []),
9495 );
9596
96- $ this ->info ("Start listenning to topic: \"$ topic \", consumer \"$ consumer \"" );
97+ $ topicName = KafkaFacade::topicName ($ topicKey );
98+ $ this ->info ("Start listenning to topic: \"$ topicName \", consumer \"$ consumer \"" );
9799
98100 try {
99101 $ highLevelConsumer
100102 ->for ($ consumer )
101- ->listen ($ topic , $ processorData , $ consumerOptions );
103+ ->listen ($ topicName , $ processorData , $ consumerOptions );
102104 } catch (Throwable $ e ) {
103105 $ this ->error ('An error occurred while listening to the topic: ' . $ e ->getMessage (). ' ' . $ e ->getFile () . ':: ' . $ e ->getLine ());
104106
@@ -117,7 +119,7 @@ protected function findMatchedProcessor(string $topic, string $consumer): ?Proce
117119 ) {
118120 return new ProcessorData (
119121 class: $ processor ['class ' ],
120- topic : $ processor ['topic ' ] ?? null ,
122+ topicKey : $ processor ['topic ' ] ?? null ,
121123 consumer: $ processor ['consumer ' ] ?? null ,
122124 type: $ processor ['type ' ] ?? 'action ' ,
123125 queue: $ processor ['queue ' ] ?? false ,
0 commit comments