-
Notifications
You must be signed in to change notification settings - Fork 179
/
event-generator.py
104 lines (81 loc) · 2.87 KB
/
event-generator.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
################################################################################
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
################################################################################
import signal
import sys
import time
import threading
import random
from kafka.errors import NoBrokersAvailable
from messages_pb2 import GreetRequest, GreetResponse
from kafka import KafkaProducer
from kafka import KafkaConsumer
KAFKA_BROKER = "kafka-broker:9092"
NAMES = ["Jerry", "George", "Elaine", "Kramer", "Newman", "Frank"]
def random_requests():
"""Generate infinite sequence of random GreetRequests."""
while True:
request = GreetRequest()
request.name = random.choice(NAMES)
yield request
def produce():
if len(sys.argv) == 2:
delay_seconds = int(sys.argv[1])
else:
delay_seconds = 1
producer = KafkaProducer(bootstrap_servers=[KAFKA_BROKER])
for request in random_requests():
key = request.name.encode('utf-8')
val = request.SerializeToString()
producer.send(topic='names', key=key, value=val)
producer.flush()
time.sleep(delay_seconds)
def consume():
consumer = KafkaConsumer(
'greetings',
bootstrap_servers=[KAFKA_BROKER],
auto_offset_reset='earliest',
group_id='event-gen')
for message in consumer:
response = GreetResponse()
response.ParseFromString(message.value)
print("%s:\t%s" % (response.name, response.greeting), flush=True)
def handler(number, frame):
sys.exit(0)
def safe_loop(fn):
while True:
try:
fn()
except SystemExit:
print("Good bye!")
return
except NoBrokersAvailable:
time.sleep(2)
continue
except Exception as e:
print(e)
return
def main():
signal.signal(signal.SIGTERM, handler)
producer = threading.Thread(target=safe_loop, args=[produce])
producer.start()
consumer = threading.Thread(target=safe_loop, args=[consume])
consumer.start()
producer.join()
consumer.join()
if __name__ == "__main__":
main()