Exemplo n.º 1
0
    def add_concurrency(self, message_type: str, concurrency_count: int):
        """
        To achieve concurrency for a message type.

        Parameters:
        message_type         This is essentially equivalent to queue/topic name.
                             For e.g. "Alert"
        concurrency_count    Integer to achieve concurrency among consumers
        """
        client_id = self._get_conf('client_id')
        MessageBus.add_concurrency(client_id, message_type,\
            concurrency_count)
Exemplo n.º 2
0
class MessageBusClient:
    """ common infrastructure for producer and consumer """

    def __init__(self, client_type: str, **client_conf: dict):
        self._message_bus = MessageBus() if 'message_bus' not in \
            client_conf.keys() or client_conf['message_bus'] is None else \
            client_conf['message_bus']
        self._message_bus.init_client(client_type, **client_conf)
        self._client_conf = client_conf
        Log.debug(f"MessageBusClient: initialized with arguments" \
            f" client_type: {client_type}, kwargs: {client_conf}")

    def _get_conf(self, key: str):
        """ To get the client configurations """
        if key not in self._client_conf.keys():
            Log.error(f"MessageBusError: {errno.ENOENT}. Could not" \
                f" find key {key} in conf file {self._message_bus.conf_file}")
            raise MessageBusError(errno.ENOENT, "Could not find key %s in " +\
                "conf file %s", key, self._message_bus.conf_file)
        return self._client_conf[key]

    def list_message_types(self) -> list:
        """ Returns list of available message types """
        client_id = self._get_conf('client_id')
        return self._message_bus.list_message_types(client_id)

    def register_message_type(self, message_types: list, partitions: int):
        """
        Registers a list of message types

        Parameters:
        message_types    This is essentially equivalent to the list of queue
                         topic name. For e.g. ["Alert"]
        partitions       Integer that represents number of partitions to be
                         created.
        """
        client_id = self._get_conf('client_id')
        self._message_bus.register_message_type(client_id, message_types, \
            partitions)

    def deregister_message_type(self, message_types: list):
        """
        Deregisters a list of message types

        Parameters:
        message_types    This is essentially equivalent to the list of queue
                         topic name. For e.g. ["Alert"]
        """
        client_id = self._get_conf('client_id')
        self._message_bus.deregister_message_type(client_id, message_types)

    def add_concurrency(self, message_type: str, concurrency_count: int):
        """
        To achieve concurrency for a message type

        Parameters:
        message_type         This is essentially equivalent to queue/topic name.
                             For e.g. "Alert"
        concurrency_count    Integer to achieve concurrency among consumers
        """
        client_id = self._get_conf('client_id')
        self._message_bus.add_concurrency(client_id, message_type, \
            concurrency_count)

    @staticmethod
    def _get_str_message_list(messages: list) -> list:
        """ Convert the format of message to string """
        from cortx.utils.kv_store import KvPayload

        message_list = []
        for message in messages:
            if isinstance(message, KvPayload):
                message = message.json

            if not isinstance(message, str):
                raise MessageBusError(errno.EINVAL, "Invalid message format, \
                    not of type KvPayload or str. %s", message)
            message_list.append(message)
        return message_list

    def send(self, messages: list):
        """
        Sends list of messages to the Message Bus

        Parameters:
        messages     A list of messages sent to Message Bus
        """
        message_type = self._get_conf('message_type')
        method = self._get_conf('method')
        client_id = self._get_conf('client_id')
        messages = self._get_str_message_list(messages)
        self._message_bus.send(client_id, message_type, method, messages)

    def delete(self):
        """ Deletes the messages """
        message_type = self._get_conf('message_type')
        client_id = self._get_conf('client_id')
        return self._message_bus.delete(client_id, message_type)

    def receive(self, timeout: float = None) -> list:
        """
        Receives messages from the Message Bus

        Parameters:
        timeout     Time in seconds to wait for the message.
        """
        client_id = self._get_conf('client_id')
        return self._message_bus.receive(client_id, timeout)

    def ack(self):
        """ Provides acknowledgement on offset """
        client_id = self._get_conf('client_id')
        self._message_bus.ack(client_id)
Exemplo n.º 3
0
class MessageBusClient:
    """ common infrastructure for producer and consumer """
    def __init__(self, client_type: str, **client_conf: dict):
        self._message_bus = MessageBus() if 'message_bus' not in \
            client_conf.keys() or client_conf['message_bus'] is None else \
            client_conf['message_bus']
        self._message_bus.init_client(client_type, **client_conf)
        self._client_conf = client_conf

    def _get_conf(self, key: str):
        """ To get the client configurations """
        if key not in self._client_conf.keys():
            raise MessageBusError(errno.EINVAL, "Invalid entry %s", key)
        return self._client_conf[key]

    def list_message_types(self) -> list:
        """ Returns list of available message types """
        client_id = self._get_conf('client_id')
        return self._message_bus.list_message_types(client_id)

    def register_message_type(self, message_types: list, partitions: int):
        """
        Registers a list of message types

        Parameters:
        message_types    This is essentially equivalent to the list of queue
                         topic name. For e.g. ["Alert"]
        partitions       Integer that represents number of partitions to be
                         created.
        """
        client_id = self._get_conf('client_id')
        self._message_bus.register_message_type(client_id, message_types, \
            partitions)

    def deregister_message_type(self, message_types: list):
        """
        Deregisters a list of message types

        Parameters:
        message_types    This is essentially equivalent to the list of queue
                         topic name. For e.g. ["Alert"]
        """
        client_id = self._get_conf('client_id')
        self._message_bus.deregister_message_type(client_id, message_types)

    def add_concurrency(self, message_type: str, concurrency_count: int):
        """
        To achieve concurrency for a message type

        Parameters:
        message_type         This is essentially equivalent to queue/topic name.
                             For e.g. "Alert"
        concurrency_count    Integer to achieve concurrency among consumers
        """
        client_id = self._get_conf('client_id')
        self._message_bus.add_concurrency(client_id, message_type, \
            concurrency_count)

    def send(self, messages: list):
        """
        Sends list of messages to the Message Bus

        Parameters:
        messages     A list of messages sent to Message Bus
        """
        message_type = self._get_conf('message_type')
        method = self._get_conf('method')
        client_id = self._get_conf('client_id')
        self._message_bus.send(client_id, message_type, method, messages)

    def delete(self):
        """ Deletes the messages """
        message_type = self._get_conf('message_type')
        client_id = self._get_conf('client_id')
        self._message_bus.delete(client_id, message_type)

    def receive(self, timeout: float = None) -> list:
        """
        Receives messages from the Message Bus

        Parameters:
        timeout     Time in seconds to wait for the message.
        """
        client_id = self._get_conf('client_id')
        return self._message_bus.receive(client_id, timeout)

    def ack(self):
        """ Provides acknowledgement on offset """
        client_id = self._get_conf('client_id')
        self._message_bus.ack(client_id)