Пример #1
0
def run():
    pipeline = Queue()
    connection = Connection("amqp://*****:*****@localhost:5672//",
                            "paramore.brightside.exchange",
                            is_durable=True)
    configuration = BrightsideConsumerConfiguration(
        pipeline,
        "examples_long_running_queue",
        "long_running",
        is_long_running_handler=True)
    consumer = ConsumerConfiguration(connection, configuration,
                                     consumer_factory,
                                     command_processor_factory,
                                     map_my_command_to_request)
    dispatcher = Dispatcher({"HelloWorldCommand": consumer})

    dispatcher.receive()

    # poll for keyboard input to allow the user to quit monitoring
    while True:
        try:
            # just sleep unless we receive an interrupt i.e. CTRL+C
            time.sleep(KEYBOARD_INTERRUPT_SLEEP)
        except KeyboardInterrupt:
            dispatcher.end()
            sys.exit(1)
Пример #2
0
def run():
    pipeline = Queue()
    amqp_uri = os.getenv('BROKER')
    connection = Connection(amqp_uri,
                            "future.stack.exchange",
                            is_durable=False)
    configuration = BrightsideConsumerConfiguration(pipeline,
                                                    "taskcreated.event",
                                                    "taskcreated.event")
    consumer = ConsumerConfiguration(connection, configuration,
                                     consumer_factory,
                                     command_processor_factory,
                                     map_my_command_to_request)
    dispatcher = Dispatcher({"ToDoCreatedEvent": consumer})

    dispatcher.receive()

    # poll for keyboard input to allow the user to quit monitoring
    while True:
        try:
            # just sleep unless we receive an interrupt i.e. CTRL+C
            time.sleep(KEYBOARD_INTERRUPT_SLEEP)
        except KeyboardInterrupt:
            dispatcher.end()
            sys.exit(1)
Пример #3
0
def run():
    pipeline = Queue()
    connection = Connection(amqp_uri,
                            "paramore.brighter.exchange",
                            is_durable=False)
    configuration = BrightsideConsumerConfiguration(pipeline,
                                                    "greetings_queue",
                                                    "greeting.event")
    consumer = ConsumerConfiguration(connection, configuration,
                                     consumer_factory,
                                     command_processor_factory,
                                     map_my_command_to_request)
    dispatcher = Dispatcher({"HelloWorldCommand": consumer})

    dispatcher.receive()

    # poll for keyboard input to allow the user to quit monitoring
    while True:
        try:
            # just sleep unless we receive an interrupt i.e. CTRL+C
            time.sleep(KEYBOARD_INTERRUPT_SLEEP)
        except KeyboardInterrupt:
            dispatcher.end()
            sys.exit(1)
Пример #4
0
    def test_stop_consumer(self):
        """Given that I have a dispatcher
            When I stop a consumer
            Then the performer should terminate
        """
        request = MyCommand()
        pipeline = Queue()
        connection = Connection(config.broker_uri,
                                "examples.perfomer.exchange")
        configuration = BrightsideConsumerConfiguration(
            pipeline, "dispatcher.test.queue", "examples.tests.mycommand")
        consumer = ConsumerConfiguration(connection, configuration,
                                         mock_consumer_factory,
                                         mock_command_processor_factory,
                                         map_my_command_to_request)
        dispatcher = Dispatcher({"MyCommand": consumer})

        header = BrightsideMessageHeader(uuid4(), request.__class__.__name__,
                                         BrightsideMessageType.MT_COMMAND)
        body = BrightsideMessageBody(
            JsonRequestSerializer(request=request).serialize_to_json(),
            BrightsideMessageBodyType.application_json)
        message = BrightsideMessage(header, body)

        pipeline.put(message)

        self.assertEqual(dispatcher.state, DispatcherState.ds_awaiting)

        dispatcher.receive()

        time.sleep(1)

        dispatcher.end()

        self.assertEqual(dispatcher.state, DispatcherState.ds_stopped)
        self.assertTrue(pipeline.empty())
Пример #5
0
    def test_restart_consumer(self):
        """Given that I have a dispatcher with all consumers stopped
            When I restart a consumer
            Then the dispatcher should have one running consumer
        """
        connection = Connection(config.broker_uri,
                                "examples.perfomer.exchange")

        # First consumer
        request = MyCommand()
        pipeline_one = Queue()
        configuration_one = BrightsideConsumerConfiguration(
            pipeline_one, "restart_command.test.queue",
            "examples.tests.mycommand")
        consumer_one = ConsumerConfiguration(connection, configuration_one,
                                             mock_consumer_factory,
                                             mock_command_processor_factory,
                                             map_my_command_to_request)

        header_one = BrightsideMessageHeader(uuid4(),
                                             request.__class__.__name__,
                                             BrightsideMessageType.MT_COMMAND)
        body_one = BrightsideMessageBody(
            JsonRequestSerializer(request=request).serialize_to_json(),
            BrightsideMessageBodyType.application_json)
        message_one = BrightsideMessage(header_one, body_one)

        pipeline_one.put(message_one)

        # Second consumer
        event = MyEvent()
        pipeline_two = Queue()
        configuration_two = BrightsideConsumerConfiguration(
            pipeline_two, "restart_event.test.queue", "examples.tests.myevent")
        consumer_two = ConsumerConfiguration(connection, configuration_two,
                                             mock_consumer_factory,
                                             mock_command_processor_factory,
                                             map_my_event_to_request)

        header_two = BrightsideMessageHeader(uuid4(), event.__class__.__name__,
                                             BrightsideMessageType.MT_EVENT)
        body_two = BrightsideMessageBody(
            JsonRequestSerializer(request=event).serialize_to_json(),
            BrightsideMessageBodyType.application_json)
        message_two = BrightsideMessage(header_two, body_two)

        pipeline_two.put_nowait(message_two)

        # Dispatcher
        dispatcher = Dispatcher({
            "consumer_one": consumer_one,
            "consumer_two": consumer_two
        })

        # Consume the messages and stop
        self.assertEqual(dispatcher.state, DispatcherState.ds_awaiting)

        dispatcher.receive()

        time.sleep(1)

        dispatcher.end()

        self.assertEqual(dispatcher.state, DispatcherState.ds_stopped)
        self.assertTrue(pipeline_one.empty())

        #Now add a new message, restart a consumer, and eat
        event_three = MyEvent()
        header_three = BrightsideMessageHeader(uuid4(),
                                               event.__class__.__name__,
                                               BrightsideMessageType.MT_EVENT)
        body_three = BrightsideMessageBody(
            JsonRequestSerializer(request=event_three).serialize_to_json(),
            BrightsideMessageBodyType.application_json)
        message_three = BrightsideMessage(header_three, body_three)

        pipeline_two.put_nowait(message_three)

        dispatcher.open("consumer_two")

        time.sleep(1)

        dispatcher.end()

        self.assertEqual(dispatcher.state, DispatcherState.ds_stopped)
        self.assertTrue(pipeline_two.empty())