X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osm_common%2Fmsgkafka.py;h=767fff6c30bba3703e27afc093f77f9b10ea94f0;hb=refs%2Fchanges%2F46%2F6746%2F1;hp=2d82f972f0b9bea4c635dcbcb5689ec7547d9bb1;hpb=8657799f1469d2495c06087e88aca2777d425806;p=osm%2Fcommon.git diff --git a/osm_common/msgkafka.py b/osm_common/msgkafka.py index 2d82f97..767fff6 100644 --- a/osm_common/msgkafka.py +++ b/osm_common/msgkafka.py @@ -1,3 +1,18 @@ +# -*- coding: utf-8 -*- + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import logging import asyncio import yaml @@ -5,7 +20,6 @@ from aiokafka import AIOKafkaConsumer from aiokafka import AIOKafkaProducer from aiokafka.errors import KafkaError from osm_common.msgbase import MsgBase, MsgException -# import json __author__ = "Alfonso Tierno , " \ "Guillermo Calvino " @@ -20,6 +34,7 @@ class MsgKafka(MsgBase): self.producer = None self.loop = None self.broker = None + self.group_id = None def connect(self, config): try: @@ -29,6 +44,7 @@ class MsgKafka(MsgBase): self.port = config["port"] self.loop = asyncio.get_event_loop() self.broker = str(self.host) + ":" + str(self.port) + self.group_id = config.get("group_id") except Exception as e: # TODO refine raise MsgException(str(e)) @@ -81,14 +97,15 @@ class MsgKafka(MsgBase): finally: await self.producer.stop() - async def aioread(self, topic, loop=None, callback=None, *args): + async def aioread(self, topic, loop=None, callback=None, aiocallback=None, **kwargs): """ - Asyncio read from one or several topics. It blocks + Asyncio read from one or several topics. It blocks. :param topic: can be str: single topic; or str list: several topics :param loop: asyncio loop - :callback: callback function that will handle the message in kafka bus - :*args: optional arguments for callback function - :return: topic, key, message + :param callback: synchronous callback function that will handle the message in kafka bus + :param aiocallback: async callback function that will handle the message in kafka bus + :param kwargs: optional keyword arguments for callback function + :return: If no callback defined, it returns (topic, key, message) """ if not loop: @@ -99,13 +116,15 @@ class MsgKafka(MsgBase): else: topic_list = (topic,) - self.consumer = AIOKafkaConsumer(loop=loop, bootstrap_servers=self.broker) + self.consumer = AIOKafkaConsumer(loop=loop, bootstrap_servers=self.broker, group_id=self.group_id) await self.consumer.start() self.consumer.subscribe(topic_list) async for message in self.consumer: if callback: - callback(message.topic, yaml.load(message.key), yaml.load(message.value), *args) + callback(message.topic, yaml.load(message.key), yaml.load(message.value), **kwargs) + elif aiocallback: + await aiocallback(message.topic, yaml.load(message.key), yaml.load(message.value), **kwargs) else: return message.topic, yaml.load(message.key), yaml.load(message.value) except KafkaError as e: