-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
44 lines (37 loc) · 1.03 KB
/
main.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
import sentry_sdk
import json
import logging
from kafka import KafkaConsumer
from kafka import KafkaProducer
from model.tip import Model
from model.tip import TripValidationError
BOOTSTRAP_SERVER = 'kafka-service.kafka.svc.cluster.local:9092'
TRIPS = 'trips'
TIPS = 'tips'
logger = logging.getLogger('mariotte')
sentry_sdk.init(
dsn = "https://[email protected]/5",
traces_sample_rate = 1.0
)
consumer = KafkaConsumer(
bootstrap_servers = [BOOTSTRAP_SERVER],
value_deserializer = lambda x: json.loads(x)
)
producer = KafkaProducer(
bootstrap_servers = [BOOTSTRAP_SERVER],
value_serializer = lambda x: json.dumps(x).encode('utf-8')
)
model = Model()
if __name__ == "__main__":
consumer.subscribe(TRIPS)
for message in consumer:
trip = message.value
try:
tip = model.predict(trip)
trip['predicted_tip'] = tip
producer.send(TIPS, trip)
except TripValidationError as e:
logger.warning("Skipping invalid record")
continue
except Exception as e:
raise e