-
Notifications
You must be signed in to change notification settings - Fork 11.3k
/
Copy pathKafkaProcessorCommand.php
182 lines (151 loc) · 5.07 KB
/
KafkaProcessorCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
namespace Illuminate\Concurrency\Console;
use Illuminate\Console\Command;
use RdKafka\Conf;
use RdKafka\KafkaConsumer;
use RdKafka\Producer;
class KafkaProcessorCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'concurrency:kafka-processor
{--brokers=localhost:9092 : Kafka bootstrap servers}
{--task-topic=laravel-concurrency-tasks : Kafka topic for tasks}
{--result-topic=laravel-concurrency-results : Kafka topic for results}
{--deferred-topic=laravel-concurrency-deferred : Kafka topic for deferred tasks}
{--group-id=laravel-concurrency-group : Kafka consumer group ID}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Process concurrent tasks from Kafka';
/**
* The Kafka consumer instance.
*
* @var \RdKafka\KafkaConsumer
*/
protected $consumer;
/**
* The Kafka producer instance.
*
* @var \RdKafka\Producer
*/
protected $producer;
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$brokers = $this->option('brokers');
$taskTopic = $this->option('task-topic');
$resultTopic = $this->option('result-topic');
$deferredTopic = $this->option('deferred-topic');
$groupId = $this->option('group-id');
$this->producer = $this->createProducer($brokers);
$this->consumer = $this->createConsumer($brokers, $groupId, [$taskTopic, $deferredTopic]);
$this->info('Starting Kafka processor...');
$this->info("Listening for tasks on topics: {$taskTopic}, {$deferredTopic}");
$this->info("Sending results to topic: {$resultTopic}");
while (true) {
try {
// Poll for messages with a 1000ms timeout
$message = $this->consumer->consume(1000);
// Skip invalid messages
if ($message === null || $message->err !== RD_KAFKA_RESP_ERR_NO_ERROR) {
continue;
}
// Process the message
$this->processMessage($message, $resultTopic);
// Poll to handle delivery reports
$this->producer->poll(0);
} catch (\Exception $e) {
$this->error("Error processing message: {$e->getMessage()}");
}
}
return 0;
}
/**
* Process a Kafka message.
*
* @param \RdKafka\Message $message
* @param string $resultTopic
* @return void
*/
protected function processMessage($message, $resultTopic)
{
$payload = json_decode($message->payload, true);
if (! isset($payload['task_id']) || ! isset($payload['task'])) {
$this->warn('Invalid task message format');
return;
}
$taskId = $payload['task_id'];
$task = unserialize($payload['task']);
$this->info("Processing task: {$taskId}");
try {
// Execute the task
$result = $task();
// Send the result back
$this->sendResult($taskId, $result, null, $resultTopic);
$this->info("Task {$taskId} completed successfully");
} catch (\Exception $e) {
$this->error("Task {$taskId} failed: {$e->getMessage()}");
// Send the error back
$this->sendResult($taskId, null, $e->getMessage(), $resultTopic);
}
}
/**
* Send a result to the specified Kafka topic.
*
* @param string $taskId
* @param mixed $result
* @param string|null $error
* @param string $topic
* @return void
*/
protected function sendResult($taskId, $result, $error, $topic)
{
$kafkaTopic = $this->producer->newTopic($topic);
$payload = json_encode([
'task_id' => $taskId,
'result' => $result !== null ? serialize($result) : null,
'error' => $error,
]);
$kafkaTopic->produce(RD_KAFKA_PARTITION_UA, 0, $payload, $taskId);
}
/**
* Create a Kafka producer instance.
*
* @param string $brokers
* @return \RdKafka\Producer
*/
protected function createProducer($brokers)
{
$conf = new Conf();
$conf->set('bootstrap.servers', $brokers);
return new Producer($conf);
}
/**
* Create a Kafka consumer instance.
*
* @param string $brokers
* @param string $groupId
* @param array $topics
* @return \RdKafka\KafkaConsumer
*/
protected function createConsumer($brokers, $groupId, array $topics)
{
$conf = new Conf();
$conf->set('bootstrap.servers', $brokers);
$conf->set('group.id', $groupId);
$conf->set('auto.offset.reset', 'latest');
$consumer = new KafkaConsumer($conf);
$consumer->subscribe($topics);
return $consumer;
}
}