forked from NSLS2/lsdc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkafka_producer.py
More file actions
35 lines (29 loc) · 1.28 KB
/
Copy pathkafka_producer.py
File metadata and controls
35 lines (29 loc) · 1.28 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
from confluent_kafka import Producer
import os
import sys
import json
from time import sleep
import certifi
conf = {'bootstrap.servers':os.environ["KAFKA_SERVERS"],
'security.protocol': 'SSL',
'ssl.ca.location': certifi.where()}
p = Producer(**conf)
def delivery_callback(err, msg):
if err:
sys.stderr.write('%% Message failed delivery: %s\n' % err)
sys.exit(1)
else:
sys.stderr.write('%% Message delivered to %s [%d] @ %d\n' %
(msg.topic(), msg.partition(), msg.offset()))
def send_kafka_message(topic, event, uuid, protocol, **kwargs):
try:
if protocol in ("standard", "vector") or (protocol == "raster" and event == "stop"):
message = {"event":event, "uuid":uuid, "protocol":protocol}
elif protocol == "raster" and event == "event":
message = {"event":event, "uuid":uuid, "protocol":protocol, "row":kwargs["row"], "proc_flag":kwargs["proc_flag"]}
else:
raise Exception(f'Unhandled protocol/event combination: protocol={protocol} event={event}')
p.produce(topic, json.dumps(message), callback=delivery_callback)
except BufferError:
sys.stderr.write('%% Local producer queue is full(%d messages awaiting delivery): try again\n' %
len(p))