-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathKafkaAdapter.php
More file actions
281 lines (233 loc) · 7.19 KB
/
Copy pathKafkaAdapter.php
File metadata and controls
281 lines (233 loc) · 7.19 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
declare(strict_types=1);
namespace Bow\Queue\Adapters;
use Bow\Queue\QueueTask;
use RdKafka\Conf;
use RdKafka\Consumer;
use RdKafka\Producer;
use RdKafka\TopicConf;
class KafkaAdapter extends QueueAdapter
{
/**
* @var Producer|null
*/
protected ?Producer $producer = null;
/**
* @var Consumer|null
*/
protected ?Consumer $consumer = null;
/**
* @var array
*/
protected array $config = [];
/**
* @var string
*/
protected string $topic = 'default';
/**
* @var string
*/
protected string $group_id = 'bow_queue_group';
/**
* Configure the adapter
*
* @param array $config
* @return QueueAdapter
*/
public function configure(array $config): QueueAdapter
{
if (!extension_loaded('rdkafka')) {
throw new \RuntimeException("Please install the rdkafka extension");
}
$this->config = $config;
$this->topic = $config['topic'] ?? $config['queue'] ?? 'default';
$this->queue = $this->topic;
$this->group_id = $config['group_id'] ?? 'bow_queue_group';
$this->initProducer();
$this->initConsumer();
return $this;
}
/**
* Initialize the Kafka producer
*
* @return void
*/
protected function initProducer(): void
{
$conf = new Conf();
$conf->set('metadata.broker.list', $this->getBrokers());
if (isset($this->config['security_protocol'])) {
$conf->set('security.protocol', $this->config['security_protocol']);
}
if (isset($this->config['sasl_mechanisms'])) {
$conf->set('sasl.mechanisms', $this->config['sasl_mechanisms']);
}
if (isset($this->config['sasl_username'])) {
$conf->set('sasl.username', $this->config['sasl_username']);
}
if (isset($this->config['sasl_password'])) {
$conf->set('sasl.password', $this->config['sasl_password']);
}
$this->producer = new Producer($conf);
}
/**
* Initialize the Kafka consumer
*
* @return void
*/
protected function initConsumer(): void
{
$conf = new Conf();
$conf->set('metadata.broker.list', $this->getBrokers());
$conf->set('group.id', $this->group_id);
$conf->set('auto.offset.reset', $this->config['auto_offset_reset'] ?? 'earliest');
$conf->set('enable.auto.commit', $this->config['enable_auto_commit'] ?? 'true');
if (isset($this->config['security_protocol'])) {
$conf->set('security.protocol', $this->config['security_protocol']);
}
if (isset($this->config['sasl_mechanisms'])) {
$conf->set('sasl.mechanisms', $this->config['sasl_mechanisms']);
}
if (isset($this->config['sasl_username'])) {
$conf->set('sasl.username', $this->config['sasl_username']);
}
if (isset($this->config['sasl_password'])) {
$conf->set('sasl.password', $this->config['sasl_password']);
}
$this->consumer = new Consumer($conf);
}
/**
* Get broker list from config
*
* @return string
*/
protected function getBrokers(): string
{
if (isset($this->config['brokers'])) {
return is_array($this->config['brokers'])
? implode(',', $this->config['brokers'])
: $this->config['brokers'];
}
$host = $this->config['host'] ?? 'localhost';
$port = $this->config['port'] ?? 9092;
return "{$host}:{$port}";
}
/**
* Push a new task onto the queue
*
* @param QueueTask $task
* @return bool
*/
public function push(QueueTask $task): bool
{
$task->setId($this->generateId());
$topic = $this->producer->newTopic($this->topic);
$body = $this->serializeProducer($task);
$topic->produce(RD_KAFKA_PARTITION_UA, 0, $body);
$this->producer->poll(0);
// Wait for message to be sent
$result = $this->producer->flush(10000);
return $result === RD_KAFKA_RESP_ERR_NO_ERROR;
}
/**
* Run the worker to consume tasks
*
* @param string|null $queue
* @return void
*/
public function run(?string $queue = null): void
{
$topic_name = $queue ?? $this->topic;
$topic = $this->consumer->newTopic($topic_name, $this->getTopicConf());
// Start consuming from partition 0, at the stored offset
$topic->consumeStart(0, RD_KAFKA_OFFSET_STORED);
$message = $topic->consume(0, $this->timeout * 1000);
if ($message === null) {
return;
}
switch ($message->err) {
case RD_KAFKA_RESP_ERR_NO_ERROR:
$this->processMessage($message);
break;
case RD_KAFKA_RESP_ERR__PARTITION_EOF:
// Reached end of partition, wait for more messages
$this->sleep($this->sleep ?: 1);
break;
case RD_KAFKA_RESP_ERR__TIMED_OUT:
// Timeout, continue waiting
break;
default:
error_log('Kafka error: ' . $message->errstr());
break;
}
}
/**
* Process a consumed message
*
* @param \RdKafka\Message $message
* @return void
*/
protected function processMessage($message): void
{
try {
$task = $this->unserializeProducer($message->payload);
$this->logProcessingTask($task);
if (method_exists($task, 'process')) {
$task->process();
$this->logProcessedTask($task);
} else {
throw new \RuntimeException('Job does not have a process method.');
}
} catch (\Throwable $e) {
$this->logFailedTask($task ?? null, $e);
}
}
/**
* Get topic configuration
*
* @return TopicConf
*/
protected function getTopicConf(): TopicConf
{
$topic_conf = new TopicConf();
$topic_conf->set('auto.offset.reset', $this->config['auto_offset_reset'] ?? 'earliest');
return $topic_conf;
}
/**
* Get the queue size
*
* @param string|null $queue
* @return int
*/
public function size(?string $queue = null): int
{
// Kafka doesn't have a direct way to get queue size like traditional queues
// This would require querying the broker for partition offsets
// Returning 0 as a placeholder
return 0;
}
/**
* Flush the queue
*
* @param string|null $queue
* @return void
*/
public function flush(?string $queue = null): void
{
// Kafka topics cannot be easily flushed like traditional queues
// This would require deleting and recreating the topic
// or using retention policies
error_log('Warning: Kafka topics cannot be flushed directly. Use topic retention policies instead.');
}
/**
* Set the queue/topic name
*
* @param string $queue
* @return void
*/
public function setQueue(string $queue): void
{
$this->queue = $queue;
$this->topic = $queue;
}
}