-
Notifications
You must be signed in to change notification settings - Fork 0
/
broker.py
54 lines (46 loc) · 1.77 KB
/
broker.py
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
"""An kafka consumer handler wich loops on the messages and commit method."""
from confluent_kafka import Consumer
from confluent_kafka.cimpl import KafkaError, KafkaException
from config import Config
kafka = Config()
topics = kafka.topics
address = ",".join(kafka.address)
class KafkaHandler:
def __init__(
self,
address=address,
group_id=kafka.consumer,
topics=topics):
connection = {'bootstrap.servers': address,
'group.id': group_id,
'auto.offset.reset': kafka.offset_reset,
'session.timeout.ms': kafka.timeout,
'enable.auto.commit': False}
self.consumer = Consumer(connection)
self.msg_count = 0
self.MIN_COMMIT_COUNT = 1
self.consumer.subscribe(topics)
def consume_loop(self):
try:
running = True
while running:
msg = self.consumer.poll(timeout=1.0)
if msg is None:
continue
if msg.error():
if msg.error().code() == KafkaError._PARTITION_EOF:
# End of partition event
print('%% %s [%d] reached end at offset %d\n' %
(msg.topic(), msg.partition(), msg.offset()))
elif msg.error():
raise KafkaException(msg.error())
else:
yield msg
finally:
# Close down consumer to commit final offsets.
self.consumer.close()
def try_commit(self):
self.msg_count += 1
if self.msg_count % self.MIN_COMMIT_COUNT == 0:
self.consumer.commit(asynchronous=False)
self.msg_count = 0