Code Coverage

Cobertura Coverage Report > osm_common >

msgkafka.py

Trend

File Coverage summary

NameClassesLinesConditionals
msgkafka.py
0%
0/1
0%
0/78
100%
0/0

Coverage Breakdown by Class

NameLinesConditionals
msgkafka.py
0%
0/78
N/A

Source

osm_common/msgkafka.py
1 # -*- coding: utf-8 -*-
2
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #    http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12 # implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 0 import asyncio
17 0 import logging
18
19 0 from aiokafka import AIOKafkaConsumer
20 0 from aiokafka import AIOKafkaProducer
21 0 from aiokafka.errors import KafkaError
22 0 from osm_common.msgbase import MsgBase, MsgException
23 0 import yaml
24
25 0 __author__ = (
26     "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>, "
27     "Guillermo Calvino <guillermo.calvinosanchez@altran.com>"
28 )
29
30
31 0 class MsgKafka(MsgBase):
32 0     def __init__(self, logger_name="msg", lock=False):
33 0         super().__init__(logger_name, lock)
34 0         self.host = None
35 0         self.port = None
36 0         self.consumer = None
37 0         self.producer = None
38 0         self.broker = None
39 0         self.group_id = None
40
41 0     def connect(self, config):
42 0         try:
43 0             if "logger_name" in config:
44 0                 self.logger = logging.getLogger(config["logger_name"])
45 0             self.host = config["host"]
46 0             self.port = config["port"]
47 0             self.broker = str(self.host) + ":" + str(self.port)
48 0             self.group_id = config.get("group_id")
49
50 0         except Exception as e:  # TODO refine
51 0             raise MsgException(str(e))
52
53 0     def disconnect(self):
54 0         try:
55 0             pass
56 0         except Exception as e:  # TODO refine
57 0             raise MsgException(str(e))
58
59 0     def write(self, topic, key, msg):
60         """
61         Write a message at kafka bus
62         :param topic: message topic, must be string
63         :param key: message key, must be string
64         :param msg: message content, can be string or dictionary
65         :return: None or raises MsgException on failing
66         """
67 0         retry = 2  # Try two times
68 0         while retry:
69 0             try:
70 0                 asyncio.run(self.aiowrite(topic=topic, key=key, msg=msg))
71 0                 break
72 0             except Exception as e:
73 0                 retry -= 1
74 0                 if retry == 0:
75 0                     raise MsgException(
76                         "Error writing {} topic: {}".format(topic, str(e))
77                     )
78
79 0     def read(self, topic):
80         """
81         Read from one or several topics.
82         :param topic: can be str: single topic; or str list: several topics
83         :return: topic, key, message; or None
84         """
85 0         try:
86 0             return asyncio.run(self.aioread(topic))
87 0         except MsgException:
88 0             raise
89 0         except Exception as e:
90 0             raise MsgException("Error reading {} topic: {}".format(topic, str(e)))
91
92 0     async def aiowrite(self, topic, key, msg):
93         """
94         Asyncio write
95         :param topic: str kafka topic
96         :param key: str kafka key
97         :param msg: str or dictionary  kafka message
98         :return: None
99         """
100 0         try:
101 0             self.producer = AIOKafkaProducer(
102                 key_serializer=str.encode,
103                 value_serializer=str.encode,
104                 bootstrap_servers=self.broker,
105             )
106 0             await self.producer.start()
107 0             await self.producer.send(
108                 topic=topic, key=key, value=yaml.safe_dump(msg, default_flow_style=True)
109             )
110 0         except Exception as e:
111 0             raise MsgException(
112                 "Error publishing topic '{}', key '{}': {}".format(topic, key, e)
113             )
114         finally:
115 0             await self.producer.stop()
116
117 0     async def aioread(
118         self,
119         topic,
120         callback=None,
121         aiocallback=None,
122         group_id=None,
123         from_beginning=None,
124         **kwargs
125     ):
126         """
127         Asyncio read from one or several topics.
128         :param topic: can be str: single topic; or str list: several topics
129         :param callback: synchronous callback function that will handle the message in kafka bus
130         :param aiocallback: async callback function that will handle the message in kafka bus
131         :param group_id: kafka group_id to use. Can be False (set group_id to None), None (use general group_id provided
132                          at connect inside config), or a group_id string
133         :param from_beginning: if True, messages will be obtained from beginning instead of only new ones.
134                                If group_id is supplied, only the not processed messages by other worker are obtained.
135                                If group_id is None, all messages stored at kafka are obtained.
136         :param kwargs: optional keyword arguments for callback function
137         :return: If no callback defined, it returns (topic, key, message)
138         """
139 0         if group_id is False:
140 0             group_id = None
141 0         elif group_id is None:
142 0             group_id = self.group_id
143 0         try:
144 0             if isinstance(topic, (list, tuple)):
145 0                 topic_list = topic
146             else:
147 0                 topic_list = (topic,)
148 0             self.consumer = AIOKafkaConsumer(
149                 bootstrap_servers=self.broker,
150                 group_id=group_id,
151                 auto_offset_reset="earliest" if from_beginning else "latest",
152             )
153 0             await self.consumer.start()
154 0             self.consumer.subscribe(topic_list)
155
156 0             async for message in self.consumer:
157 0                 if callback:
158 0                     callback(
159                         message.topic,
160                         yaml.safe_load(message.key),
161                         yaml.safe_load(message.value),
162                         **kwargs
163                     )
164 0                 elif aiocallback:
165 0                     await aiocallback(
166                         message.topic,
167                         yaml.safe_load(message.key),
168                         yaml.safe_load(message.value),
169                         **kwargs
170                     )
171                 else:
172 0                     return (
173                         message.topic,
174                         yaml.safe_load(message.key),
175                         yaml.safe_load(message.value),
176                     )
177 0         except KafkaError as e:
178 0             raise MsgException(str(e))
179         finally:
180 0             await self.consumer.stop()