-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer.py
89 lines (68 loc) · 2.72 KB
/
consumer.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
import threading
from typing import Dict
from time import sleep
from .connection import Connection
from .sync_queue import SyncQueue
from .topic import Topic
class TopicConsumer:
FETCH_FREQUENCY = 10
def __init__(self, topic: Topic, connection: Connection):
self.topic = topic
self.connection = connection
self._stop_thread = False
res = self.connection.post_readonly('/consumer/register', params=topic.dict())
if not res.ok:
raise Exception('Error while registering topic')
self._cons_id = res.json()
self._queue = SyncQueue()
self._thread = threading.Thread(target=self._worker)
self._thread.start()
def _fetch_next(self) -> None:
res = self.connection.get('/consumer/consume', params={**self.topic.dict(), 'consumer_id': self._cons_id})
if not res.ok:
raise Exception('Error while getting next message', res.json())
self._queue.put(res.json()['message'])
def _worker(self, ) -> None:
while not self._stop_thread:
try:
self._fetch_next()
finally:
sleep(1 / self.FETCH_FREQUENCY)
def get_size(self) -> int:
size_in_buffer = len(self._queue)
res = self.connection.get('/consumer/size', params={**self.topic.dict(), 'consumer_id': self._cons_id})
if not res.ok:
raise Exception('Error while getting size')
return res.json()['size'] + size_in_buffer
def get_next(self) -> str:
if self._queue.empty:
self._fetch_next()
return self._queue.get()
def stop_worker(self):
self._stop_thread = True
if hasattr(self, '_thread') and self._thread.is_alive():
self._thread.join()
def __del__(self):
self.stop_worker()
class Consumer:
FETCH_FREQUENCY = 10
def __init__(self, topics: list[Topic], connection: Connection):
self._consumers: Dict[Topic, TopicConsumer] = dict()
self.connection = connection
for topic in topics:
self.register_topic(topic)
def register_topic(self, topic: Topic) -> None:
self._consumers[topic] = TopicConsumer(topic, self.connection)
def get_next(self, topic: Topic) -> str:
if topic not in self._consumers:
self.register_topic(topic)
return self._consumers[topic].get_next()
def get_size(self, topic: Topic) -> int:
if topic not in self._consumers:
raise Exception('Topic not registered')
return self._consumers[topic].get_size()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
for consumer in self._consumers.values():
consumer.stop_worker()