-
Notifications
You must be signed in to change notification settings - Fork 82
/
inv_mq_service.py
executable file
·68 lines (54 loc) · 2.16 KB
/
inv_mq_service.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/python
from functools import partial
from confluent_kafka import Consumer as KafkaConsumer
from prometheus_client import start_http_server
from api.cache import init_cache
from app import create_app
from app.environment import RuntimeEnvironment
from app.logging import get_logger
from app.queue.event_producer import EventProducer
from app.queue.host_mq import add_host
from app.queue.host_mq import event_loop
from app.queue.host_mq import handle_message
from app.queue.host_mq import update_system_profile
from lib.handlers import ShutdownHandler
from lib.handlers import register_shutdown
logger = get_logger("host_mq_service")
def main():
application = create_app(RuntimeEnvironment.SERVICE)
config = application.app.config["INVENTORY_CONFIG"]
init_cache(config, application)
start_http_server(config.metrics_port)
topic_to_handler = {config.host_ingress_topic: add_host, config.system_profile_topic: update_system_profile}
consumer = KafkaConsumer(
{
"group.id": config.host_ingress_consumer_group,
"bootstrap.servers": config.bootstrap_servers,
"auto.offset.reset": "earliest",
**config.kafka_consumer,
}
)
consumer.subscribe([config.kafka_consumer_topic])
consumer_shutdown = partial(consumer.close, autocommit=True)
register_shutdown(consumer_shutdown, "Closing consumer")
event_producer = EventProducer(config, config.event_topic)
register_shutdown(event_producer.close, "Closing producer")
notification_event_producer = EventProducer(config, config.notification_topic)
register_shutdown(notification_event_producer.close, "Closing notification producer")
shutdown_handler = ShutdownHandler()
shutdown_handler.register()
message_handler = partial(
handle_message,
message_operation=topic_to_handler[config.kafka_consumer_topic],
notification_event_producer=notification_event_producer,
)
event_loop(
consumer,
application.app,
event_producer,
notification_event_producer,
message_handler,
shutdown_handler.shut_down,
)
if __name__ == "__main__":
main()