-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathstream-kafka.py
More file actions
166 lines (138 loc) · 4.95 KB
/
stream-kafka.py
File metadata and controls
166 lines (138 loc) · 4.95 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
import os
import logging
import json
import sys
from pprint import pformat
from tornado.websocket import websocket_connect
from tornado.ioloop import IOLoop
from tornado import gen
from quixstreams import Application
# Configuration
KAFKA_BROKER = os.getenv('KAFKA_BROKER', 'localhost:19092')
KAFKA_TOPIC = os.getenv('KAFKA_TOPIC', 'eu_seismic')
WEBSOCKET_URI = 'wss://www.seismicportal.eu/standing_order/websocket'
PING_INTERVAL = 15
# Setup logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
stream=sys.stdout
)
logger = logging.getLogger(__name__)
class SeismicKafkaProducer:
def __init__(self):
self.app = Application(
broker_address=KAFKA_BROKER,
loglevel="INFO",
producer_extra_config={
"compression.type": "gzip",
"batch.size": 16384,
"linger.ms": 100,
},
)
self.producer = None
def __enter__(self):
self.producer = self.app.get_producer()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
if self.producer:
self.producer.close()
def send_event(self, event_data):
"""Send seismic event to Kafka"""
try:
# Parse the JSON message
data = json.loads(event_data)
# Extract key from the event (using id if available, otherwise generate one)
key = data.get('id', data.get('unid', str(hash(event_data))))
# Send to Kafka
self.producer.produce(
topic=KAFKA_TOPIC,
key=key,
value=json.dumps(data),
)
# Log the event details
if 'data' in data and 'properties' in data['data']:
info = data['data']['properties']
info['action'] = data.get('action', 'unknown')
logger.info(
'>>>> {action:7} event from {auth:7}, unid:{unid}, T0:{time}, Mag:{mag}, Region: {flynn_region}'.format(**info)
)
else:
logger.info(f"Sent event to Kafka: {key}")
except json.JSONDecodeError:
logger.error("Unable to parse JSON message")
except Exception as e:
logger.exception(f"Error processing message: {e}")
class SeismicWebSocketClient:
def __init__(self):
self.ws = None
self.producer = None
@gen.coroutine
def connect(self):
"""Connect to the seismic portal websocket"""
try:
logger.info(f"Connecting to WebSocket: {WEBSOCKET_URI}")
self.ws = yield websocket_connect(
WEBSOCKET_URI,
ping_interval=PING_INTERVAL
)
logger.info("WebSocket connection established")
return True
except Exception as e:
logger.exception(f"Failed to connect to WebSocket: {e}")
return False
@gen.coroutine
def listen(self):
"""Listen for messages from the websocket"""
if not self.ws:
logger.error("WebSocket not connected")
return
logger.info("Listening for seismic events...")
try:
with SeismicKafkaProducer() as producer:
self.producer = producer
while True:
msg = yield self.ws.read_message()
if msg is None:
logger.info("WebSocket connection closed")
break
# Process and send the message to Kafka
self.producer.send_event(msg)
except Exception as e:
logger.exception(f"Error in message processing: {e}")
finally:
self.producer = None
def close(self):
"""Close the websocket connection"""
if self.ws:
self.ws.close()
logger.info("WebSocket connection closed")
@gen.coroutine
def main():
"""Main application function"""
client = SeismicWebSocketClient()
try:
# Connect to websocket
connected = yield client.connect()
if not connected:
return
# Start listening for messages
yield client.listen()
except KeyboardInterrupt:
logger.info("Received interrupt signal")
except Exception as e:
logger.exception(f"Unexpected error: {e}")
finally:
client.close()
if __name__ == '__main__':
logger.info(f"Starting Seismic Kafka Producer")
logger.info(f"Kafka Broker: {KAFKA_BROKER}")
logger.info(f"Kafka Topic: {KAFKA_TOPIC}")
logger.info(f"WebSocket URI: {WEBSOCKET_URI}")
ioloop = IOLoop.instance()
try:
ioloop.run_sync(main)
except KeyboardInterrupt:
logger.info("Shutting down...")
finally:
ioloop.stop()