def main(): application = create_app(RuntimeEnvironment.SERVICE) start_http_server(9126) config = application.config["INVENTORY_CONFIG"] consumer = KafkaConsumer( config.host_ingress_topic, group_id=config.host_ingress_consumer_group, bootstrap_servers=config.bootstrap_servers, api_version=(0, 10, 1), value_deserializer=lambda m: m.decode(), **config.kafka_consumer, ) event_producer = EventProducer(config) try: event_loop(consumer, application, event_producer, handle_message, ShutdownHandler()) finally: logger.info("Closing consumer") consumer.close(autocommit=True) logger.info("Closing producer") event_producer.close()
def main(): application = create_app(RuntimeEnvironment.SERVICE) config = application.config["INVENTORY_CONFIG"] start_http_server(config.metrics_port) topic_to_handler = { config.host_ingress_topic: add_host, config.system_profile_topic: update_system_profile } consumer = KafkaConsumer( config.kafka_consumer_topic, group_id=config.host_ingress_consumer_group, bootstrap_servers=config.bootstrap_servers, api_version=(0, 10, 1), value_deserializer=lambda m: m.decode(), **config.kafka_consumer, ) consumer_shutdown = partial(consumer.close, autocommit=True) register_shutdown(consumer_shutdown, "Closing consumer") event_producer = EventProducer(config) register_shutdown(event_producer.close, "Closing producer") shutdown_handler = ShutdownHandler() shutdown_handler.register() message_handler = partial( handle_message, message_operation=topic_to_handler[config.kafka_consumer_topic]) event_loop(consumer, application, event_producer, message_handler, shutdown_handler.shut_down)
def test_shutdown_handler(mocker, flask_app): fake_consumer = mocker.Mock() fake_consumer.poll.return_value = {"poll1": [mocker.Mock(), mocker.Mock()]} fake_event_producer = None handle_message_mock = mocker.Mock(side_effect=[None, None]) event_loop( fake_consumer, flask_app, fake_event_producer, handler=handle_message_mock, interrupt=mocker.Mock(side_effect=(False, True)), ) fake_consumer.poll.assert_called_once() assert handle_message_mock.call_count == 2
def test_event_loop_exception_handling(mocker, flask_app): """ Test to ensure that an exception in message handler method does not cause the event loop to stop processing messages """ fake_consumer = mocker.Mock() fake_consumer.poll.return_value = {"poll1": [mocker.Mock(), mocker.Mock(), mocker.Mock()]} fake_event_producer = None handle_message_mock = mocker.Mock(side_effect=[None, KeyError("blah"), None]) event_loop( fake_consumer, flask_app, fake_event_producer, handler=handle_message_mock, interrupt=mocker.Mock(side_effect=(False, True)), ) assert handle_message_mock.call_count == 3