Example #1
0
def main():
    """main"""
    host = getenv('RABBIT_BROKER')
    port = int(getenv('RABBIT_PORT'))
    user = getenv('RABBIT_USER')
    password = getenv('RABBIT_PWD')
    vhost = getenv('RABBIT_VHOST')
    cert = getenv('CERT', 'cert.pem')

    success = False
    context = rabbitmq.RabbitContext(host,
                                     port,
                                     user,
                                     password,
                                     vhost,
                                     cert=cert)

    try:
        LOGGER.info("Connecting...")

        with rabbitmq.RabbitClient(context) as _:
            success = True
            LOGGER.info("Success.")
    except KeyboardInterrupt:
        LOGGER.info("Stopping")
    except Exception as expt:
        LOGGER.info("Error: %r", expt)

    if success:
        LOGGER.info("SUCCESS - connection to the RabbitMQ service verified.")
    else:
        LOGGER.info(
            "FAILED - connection to the RabbitMQ service not verified.")
Example #2
0
    def listen(self):
        """
            A thread, that starts the RabbitMQ message consumption

            Throws:
                No exceptions thrown

            Returns:
                Nothing
        """
        while not SHUTTING_DOWN:
            try:
                LOGGER.info("Connecting to OpenWhisk...")

                with whisk.WhiskInvoker(self.whisk_context) as self.invoker:
                    LOGGER.info("Connecting to RabbitMQ...")

                    with rabbitmq.RabbitClient(
                            self.rabbit_context) as self.rabbit:
                        self.rabbit.start_queue(
                            queue=rabbitmq.RabbitQueue(self.subscribe))

                        LOGGER.info("Waiting on %r...", self.subscribe)

                        #Blocks indefinitely
                        self.rabbit.receive(self.send_to_whisk,
                                            self.timeout_seconds)
                        LOGGER.info("Timed out or interrupted.")
            except Exception as expt:
                LOGGER.error("Listener Exception: %r", expt)
                traceback.print_exc()
                #Brief pause before re-connecting
                time.sleep(2)
def main():
    host = getenv('RABBIT_BROKER')
    port = int(getenv('RABBIT_PORT'))
    user = getenv('RABBIT_USER')
    password = getenv('RABBIT_PWD')
    vhost = getenv('RABBIT_VHOST')
    cert = getenv('CERT', 'cert.pem')
    feed_queue = getenv('PUBLISH_QUEUE')
    reply_queue = getenv('SUBSCRIBE_QUEUE', ' ')

    LOGGER.info("Starting...")

    context = rabbitmq.RabbitContext(host,
                                     port,
                                     user,
                                     password,
                                     vhost,
                                     cert=cert)

    try:
        with rabbitmq.RabbitClient(context) as client:
            client.start(publish=rabbitmq.RabbitQueue(feed_queue, purge=True),
                         subscribe=rabbitmq.RabbitQueue(reply_queue))
            message = {'action': 'Outbound', 'payload': 'some data'}
            LOGGER.info(f'sending {message}')
            client.publish(json.dumps(message))

            with rabbitmq.RabbitClient(context) as server:
                server.start(subscribe=rabbitmq.RabbitQueue(feed_queue))

                mh = ServerMessageHandler()
                server.receive(mh.handler, max_messages=1)

                #And send a reply to the client
                reply = {'action': 'Inbound', 'reply': 'the reply'}
                server.publish(json.dumps(reply),
                               rabbitmq.RabbitQueue(reply_queue))

            #Now catch the reply in the client
            message = client.receive()
            LOGGER.info(f"client got {message}")
    except Exception as err:
        LOGGER.info("Error %r", err)
        raise err
Example #4
0
def main():
    """main"""
    host = getenv('RABBIT_BROKER')
    port = int(getenv('RABBIT_PORT'))
    user = getenv('RABBIT_USER')
    password = getenv('RABBIT_PWD')
    vhost = getenv('RABBIT_VHOST')
    cert = getenv('CERT', 'cert.pem')
    feed_queue = getenv('FEED_QUEUE')
    reply_queue = getenv('REPLY_QUEUE')

    messages = 10
    LOGGER.info("Starting...")

    context = rabbitmq.RabbitContext(host, port, user, password, vhost, cert=cert)

    try:
        start = timeit.default_timer()

        LOGGER.info("Sending requests to: %r", format(feed_queue))

        with rabbitmq.RabbitClient(context) as client:
            client.start_queue(queue=rabbitmq.RabbitQueue(feed_queue))
            message = {"serviceRequest" : "none"}

            for _ in range(0, messages):
                client.publish(json.dumps(message))

        LOGGER.info("Dispatched messages: %r", client.outbound)
        LOGGER.info("Now wait for replies on: %r", reply_queue)

        with rabbitmq.RabbitClient(context) as client:
            client.start_queue(queue=rabbitmq.RabbitQueue(reply_queue))
            client.receive(lambda x: None, max_messages=messages)

        LOGGER.info("Received messages: %r", client.inbound)
        stop = round(timeit.default_timer() - start, 2)
        LOGGER.info("Duration %r", stop)
        LOGGER.info("Done")
    except KeyboardInterrupt:
        LOGGER.info("Stopping")
    except Exception as err:
        LOGGER.info("Error %r", err)
Example #5
0
def main(args):
    activation = os.getenv('__OW_ACTIVATION_ID', None)

    try:
        messages = 0

        cfg = [
            'broker_host', 'broker_vhost', 'broker_port', 'broker_user',
            'broker_password', 'publish_queue'
        ]
        for key in cfg:
            if args.get(key) is None:
                raise Exception('{} is missing'.format(key))

        host = args['broker_host']
        vhost = args['broker_vhost']
        port = args['broker_port']
        user = args['broker_user']
        password = args['broker_password']
        publish_queue = args['publish_queue']

        context = rabbitmq.RabbitContext(host, port, user, password, vhost)

        if 'messages' in args:
            with rabbitmq.RabbitClient(context) as client:
                client.start_queue(queue=rabbitmq.RabbitQueue(publish_queue))

                for msg in args['messages']:
                    messages += 1
                    print(msg)
                    client.publish(json.dumps({'count': messages}))

        result = {'messages': messages}
    except Exception as err:
        result = {'result': str(err)}
        print("Error: %r" % err)

    result.update({"activation": activation})
    print(result)
    return result