Example #1
0
def send_event_to_store(channel_name, client_id, kube_add):
    sender = Sender(kube_add)
    event = Event(metadata="EventMetaData",
                  body=("Event Created on time %s" %
                        datetime.datetime.utcnow()).encode('UTF-8'),
                  store=True,
                  channel=channel_name,
                  client_id="EventSenderStore")
    event.tags = [
        ('key', 'value'),
        ('key2', 'value2'),
    ]
    sender.send_event(event)
def send_single_event(queue_name, client_id, kube_add):
    sender = Sender(kube_add)
    event = Event(metadata="EventMetaData",
                  body=("Event Created on time %s" %
                        datetime.datetime.utcnow()).encode('UTF-8'),
                  store=False,
                  channel="MyTestChannelName",
                  client_id="EventSender")
    event.tags = [
        ('key', 'value'),
        ('key2', 'value2'),
    ]
    sender.send_event(event)
import datetime
import json

from kubemq.events.lowlevel.event import Event
from kubemq.events.lowlevel.sender import Sender

if __name__ == "__main__":
    try:
        publisher = Sender("localhost:50000")
        f = open('bookmark_events.json')
        data = json.load(f)
        for event in data:
            event = Event(metadata="Bookmark event metadata",
                          body=json.dumps(event).encode('utf-8'),
                          store=False,
                          channel="bookmark_event_channel",
                          client_id="bookmark_events_subscriber")
            print(event)
            res = publisher.send_event(event)
            print(res)
        f.close()
    except Exception as err:
        print("'Error when sending event:'%s'" % (err))
Example #4
0
import datetime
import sys

sys.path.append(".")
from kubemq.events.lowlevel.event import Event
from kubemq.events.lowlevel.sender import Sender

if __name__ == "__main__":
    print("Sending event using sender example")
    sender = Sender("localhost:50000")
    event = Event(metadata="EventMetaData",
                  body=("Event Created on time %s" %
                        datetime.datetime.utcnow()).encode('UTF-8'),
                  store=False,
                  channel="MyTestChannelName",
                  client_id="EventSender")
    event.tags = [
        ('key', 'value'),
        ('key2', 'value2'),
    ]
    try:
        sender.send_event(event)
    except Exception as err:
        print('error, error:%s' % (err))
Example #5
0
class Channel:
    """Sender with a set of predefined parameters"""
    sender = None

    def __init__(self,
                 params=None,
                 channel_name=None,
                 client_id=None,
                 store=None,
                 kubemq_address=None):
        """
        Initializes a new instance of the Events.Channel class using params OR "Manual" Parameters.

        :param params: ChannelParameters to use instead of the other params
        :param channel_name: Represents The channel name to send to using the KubeMQ.
        :param client_id: Represents the sender ID that the messages will be send under.
        :param store: Represents if the events should be send to persistence.
        :param kubemq_address: The address the of the KubeMQ including the GRPC Port ,Example: "LocalHost:50000".
        """

        if params:
            self.channel_name = params.channel_name
            self.client_id = params.client_id
            self.store = params.store
            self.kubemq_address = params.kubemq_address
        else:
            self.channel_name = channel_name
            self.client_id = client_id
            self.store = store
            self.kubemq_address = kubemq_address

        if not self.channel_name:
            raise ValueError("channel_name parameter is mandatory")

        self.sender = Sender(kubemq_address)

    def __repr__(self):
        return "<Channel channel_name:%s client_id:%s store:%s kubemq_address:%s>" % (
            self.channel_name, self.client_id, self.store, self.kubemq_address)

    def send_event(self, event):
        """Sending a new Event using KubeMQ."""
        low_level_event = self.create_low_level_event(event)
        return self.sender.send_event(low_level_event)

    def stream_event(self, stream, handler=None):
        """Sending a new Event using KubeMQ."""
        def stream_handler():
            for event in stream:
                yield self.create_low_level_event(event)

        return self.sender.stream_event(stream_handler(), handler)

    def create_low_level_event(self, notification):
        return Event(
            channel=self.channel_name,
            client_id=self.client_id,
            store=self.store,
            event_id=notification.event_id,
            body=notification.body,
            metadata=notification.metadata,
        )