-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkafka-intermediate.py
134 lines (113 loc) · 4.55 KB
/
kafka-intermediate.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
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
from kafka import KafkaConsumer, KafkaProducer
import time
import threading
import json
from datetime import datetime, timedelta
drivers = {}
delete_keys = []
def register():
consumer1 = KafkaConsumer(value_deserializer = lambda message: json.loads(message.decode('utf8')))
consumer1.subscribe(['register'])
while True:
msg = consumer1.poll(200)
if len(msg) > 0 :
msg = msg[next(iter(msg))][0].value
print('-'*10)
drivers[msg['node_id']] = [msg['node_IP'], {"last_heartbeat": datetime.now()}]
print()
print(f"node with id {msg['node_id']} successfully registered!")
print()
print("-"*10)
def heartbeat():
consumer2 = KafkaConsumer(value_deserializer = lambda message: json.loads(message.decode('utf8')))
consumer2.subscribe(['heartbeat'])
while True:
msg = consumer2.poll(200)
if len(msg) > 0 :
msg = msg[next(iter(msg))][0].value
print('-'*10)
drivers[msg['node_id']][1]["last_heartbeat"] = datetime.strptime(msg['timestamp'], "%Y-%m-%d %H:%M:%S")
print()
print(f"Recieved a heartbeat from node with id {msg['node_id']} ")
print()
print("-"*10)
def check_validity():
while True:
time.sleep(10)
delete_keys = []
for key in drivers.keys():
time_difference = abs( datetime.now() - drivers[key][1]["last_heartbeat"])
if time_difference >= timedelta(seconds=25):
print('-'*10)
delete_keys.append(key)
print()
print(f"Heartbeat not detected by driver id {key} for more than 25s, hence removing the driver from registered list!")
print()
print('-'*10)
for key in delete_keys:
del drivers[key]
def test():
consumer3 = KafkaConsumer(value_deserializer = lambda message: json.loads(message.decode('utf8')))
consumer3.subscribe(['kafka_test_config'])
while True:
msg = consumer3.poll(200)
if len(msg) > 0 :
msg = msg[next(iter(msg))][0].value
producer = KafkaProducer(value_serializer = lambda message:json.dumps(message).encode('utf8'))
producer.send(topic="test_config",value=msg)
producer.flush()
def triger():
consumer4 = KafkaConsumer(value_deserializer = lambda message: json.loads(message.decode('utf8')))
consumer4.subscribe(['kafka_trigger'])
while True:
msg = consumer4.poll(200)
if len(msg) > 0 :
msg = msg[next(iter(msg))][0].value
producer = KafkaProducer(value_serializer = lambda message:json.dumps(message).encode('utf8'))
producer.send(topic="trigger",value=msg)
producer.flush()
def metrics():
consumer4 = KafkaConsumer(value_deserializer = lambda message: json.loads(message.decode('utf8')))
consumer4.subscribe(['metrics'])
while True:
msg = consumer4.poll(200)
if len(msg) > 0 :
msg = msg[next(iter(msg))][0].value
producer = KafkaProducer(value_serializer = lambda message:json.dumps(message).encode('utf8'))
producer.send(topic="kafka_metrics",value=msg)
producer.flush()
def status():
consumer4 = KafkaConsumer(value_deserializer = lambda message: json.loads(message.decode('utf8')))
consumer4.subscribe(['status'])
while True:
msg = consumer4.poll(200)
if len(msg) > 0 :
producer = KafkaProducer(value_serializer = lambda message:json.dumps(message).encode('utf8'))
updated_drivers = {}
for key in drivers.keys():
updated_drivers[key] = drivers[key]
updated_drivers[key] = str(updated_drivers[key][0])
producer.send(topic="status",value=updated_drivers)
producer.flush()
if __name__=="__main__":
thread1 = threading.Thread(target=register)
thread2 = threading.Thread(target=heartbeat)
thread3 = threading.Thread(target=check_validity)
thread4 = threading.Thread(target=test)
thread5 = threading.Thread(target=triger)
thread6 = threading.Thread(target=metrics)
thread7 = threading.Thread(target=status)
thread1.start()
thread2.start()
thread3.start()
thread4.start()
thread5.start()
thread6.start()
thread7.start()
thread1.join()
thread2.join()
thread3.join()
thread4.join()
thread5.join()
thread6.join()
thread7.join()