-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathpulsarTools.py
78 lines (64 loc) · 1.97 KB
/
pulsarTools.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
"""
pulsarTools.py
"""
import os
import pulsar
from dotenv import load_dotenv
load_dotenv()
try:
STREAMING_TENANT = os.environ['STREAMING_TENANT']
STREAMING_NAMESPACE = os.environ['STREAMING_NAMESPACE']
STREAMING_TOPIC = os.environ['STREAMING_TOPIC']
STREAMING_SERVICE_URL = os.environ['STREAMING_SERVICE_URL']
TRUST_CERTS = os.environ['TRUST_CERTS']
ASTRA_STREAMING_TOKEN = os.environ['ASTRA_STREAMING_TOKEN']
#
client = pulsar.Client(
STREAMING_SERVICE_URL,
authentication=pulsar.AuthenticationToken(ASTRA_STREAMING_TOKEN),
tls_trust_certs_file_path=TRUST_CERTS,
)
except KeyError:
raise KeyError('Environment variables not detected. Perhaps you forgot to '
'prepare the .env file ?')
streamingTopic = 'persistent://{tenant}/{namespace}/{topic}'.format(
tenant=STREAMING_TENANT,
namespace=STREAMING_NAMESPACE,
topic=STREAMING_TOPIC,
)
# Pulsar producer/consumer caches
# (for parsimonious allocation of resources)
consumerCache = {}
cachedProducer = None
def getPulsarClient():
return client
def getConsumer(clientID, puClient):
global consumerCache
#
if clientID not in consumerCache:
pulsarSubscription = f'sub_{clientID}'
consumerCache[clientID] = puClient.subscribe(streamingTopic,
pulsarSubscription)
#
return consumerCache[clientID]
def getProducer(puClient):
global cachedProducer
#
if cachedProducer is None:
cachedProducer = puClient.create_producer(streamingTopic)
#
return cachedProducer
def receiveOrNone(consumer, timeout):
"""
A modified 'receive' function for a Pulsar topic
that handles timeouts so that when the topic is empty
it simply returns None.
"""
try:
msg = consumer.receive(timeout)
return msg
except Exception as e:
if 'timeout' in str(e).lower():
return None
else:
raise e