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,20 +54,12 @@ 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 ' );
58- $ availableConsumers = array_keys (config ('kafka.consumers ' , []));
5959
60- if (!in_array ($ consumer , $ availableConsumers )) {
61- $ this ->error ("Unknown consumer \"$ consumer \"" );
62- $ this ->line ('Available consumers are: " ' . implode (', ' , $ availableConsumers ) . '" and can be found in /config/kafka.php ' );
63-
64- return 1 ;
65- }
66-
67- $ processorData = $ this ->findMatchedProcessor ($ topic , $ consumer );
60+ $ processorData = $ this ->findMatchedProcessor ($ topicKey , $ consumer );
6861 if (is_null ($ processorData )) {
69- $ this ->error ("Processor for topic \"$ topic \" and consumer \"$ consumer \" is not found " );
62+ $ this ->error ("Processor for topic-key \"$ topicKey \" and consumer \"$ consumer \" is not found " );
7063 $ this ->line ('Processors are set in /config/kafka-consumers.php ' );
7164
7265 return 1 ;
@@ -93,12 +86,13 @@ public function handle(HighLevelConsumer $highLevelConsumer): int
9386 middleware: $ this ->collectMiddleware ($ consumerPackageOptions ['middleware ' ] ?? []),
9487 );
9588
96- $ this ->info ("Start listenning to topic: \"$ topic \", consumer \"$ consumer \"" );
89+ $ topicName = KafkaFacade::topicNameByClient ('consumer ' , $ consumer , $ topicKey );
90+ $ this ->info ("Start listening to topic: \"{$ topicKey }\" ( {$ topicName }), consumer \"{$ consumer }\"" );
9791
9892 try {
9993 $ highLevelConsumer
10094 ->for ($ consumer )
101- ->listen ($ topic , $ processorData , $ consumerOptions );
95+ ->listen ($ topicName , $ processorData , $ consumerOptions );
10296 } catch (Throwable $ e ) {
10397 $ this ->error ('An error occurred while listening to the topic: ' . $ e ->getMessage (). ' ' . $ e ->getFile () . ':: ' . $ e ->getLine ());
10498
@@ -111,13 +105,12 @@ public function handle(HighLevelConsumer $highLevelConsumer): int
111105 protected function findMatchedProcessor (string $ topic , string $ consumer ): ?ProcessorData
112106 {
113107 foreach (config ('kafka-consumer.processors ' , []) as $ processor ) {
114- if (
115- (empty ($ processor ['topic ' ]) || $ processor ['topic ' ] === $ topic )
116- && (empty ($ processor ['consumer ' ]) || $ processor ['consumer ' ] === $ consumer )
117- ) {
108+ $ topicMatched = empty ($ processor ['topic ' ]) || $ processor ['topic ' ] === $ topic ;
109+ $ consumerMatched = empty ($ processor ['consumer ' ]) || $ processor ['consumer ' ] === $ consumer ;
110+ if ($ topicMatched && $ consumerMatched ) {
118111 return new ProcessorData (
119112 class: $ processor ['class ' ],
120- topic : $ processor ['topic ' ] ?? null ,
113+ topicKey : $ processor ['topic ' ] ?? null ,
121114 consumer: $ processor ['consumer ' ] ?? null ,
122115 type: $ processor ['type ' ] ?? 'action ' ,
123116 queue: $ processor ['queue ' ] ?? false ,
0 commit comments