2
2
3
3
namespace Ensi \LaravelPhpRdKafkaConsumer \Commands ;
4
4
5
+ use Ensi \LaravelPhpRdKafka \KafkaFacade ;
5
6
use Ensi \LaravelPhpRdKafkaConsumer \ConsumerOptions ;
6
7
use Ensi \LaravelPhpRdKafkaConsumer \HighLevelConsumer ;
7
8
use Ensi \LaravelPhpRdKafkaConsumer \ProcessorData ;
@@ -15,7 +16,7 @@ class KafkaConsumeCommand extends Command implements SignalableCommandInterface
15
16
* The name and signature of the console command.
16
17
*/
17
18
protected $ signature = 'kafka:consume
18
- {topic : The name of the topic}
19
+ {topic-key : The key of a topic in the kafka.topics list }
19
20
{consumer=default : The name of the consumer}
20
21
{--max-events=0 : The number of events to consume before stopping}
21
22
{--max-time=0 : The maximum number of seconds the worker should run}
@@ -53,20 +54,12 @@ public function handleSignal(int $signal): void
53
54
public function handle (HighLevelConsumer $ highLevelConsumer ): int
54
55
{
55
56
$ this ->consumer = $ highLevelConsumer ;
56
- $ topic = $ this ->argument ('topic ' );
57
+ $ topicKey = $ this ->argument ('topic-key ' );
57
58
$ consumer = $ this ->argument ('consumer ' );
58
- $ availableConsumers = array_keys (config ('kafka.consumers ' , []));
59
59
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 );
68
61
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 " );
70
63
$ this ->line ('Processors are set in /config/kafka-consumers.php ' );
71
64
72
65
return 1 ;
@@ -93,12 +86,13 @@ public function handle(HighLevelConsumer $highLevelConsumer): int
93
86
middleware: $ this ->collectMiddleware ($ consumerPackageOptions ['middleware ' ] ?? []),
94
87
);
95
88
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 }\"" );
97
91
98
92
try {
99
93
$ highLevelConsumer
100
94
->for ($ consumer )
101
- ->listen ($ topic , $ processorData , $ consumerOptions );
95
+ ->listen ($ topicName , $ processorData , $ consumerOptions );
102
96
} catch (Throwable $ e ) {
103
97
$ this ->error ('An error occurred while listening to the topic: ' . $ e ->getMessage (). ' ' . $ e ->getFile () . ':: ' . $ e ->getLine ());
104
98
@@ -111,13 +105,12 @@ public function handle(HighLevelConsumer $highLevelConsumer): int
111
105
protected function findMatchedProcessor (string $ topic , string $ consumer ): ?ProcessorData
112
106
{
113
107
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 ) {
118
111
return new ProcessorData (
119
112
class: $ processor ['class ' ],
120
- topic : $ processor ['topic ' ] ?? null ,
113
+ topicKey : $ processor ['topic ' ] ?? null ,
121
114
consumer: $ processor ['consumer ' ] ?? null ,
122
115
type: $ processor ['type ' ] ?? 'action ' ,
123
116
queue: $ processor ['queue ' ] ?? false ,
0 commit comments