matrix-alertbot/matrix_alertbot/main.py

57 lines
1.7 KiB
Python
Raw Normal View History

2019-09-25 14:26:29 +02:00
#!/usr/bin/env python3
2024-01-22 11:35:13 +01:00
from __future__ import annotations
2019-09-25 14:26:29 +02:00
import asyncio
2020-08-10 00:02:07 +02:00
import logging
import sys
2020-08-10 00:02:07 +02:00
from diskcache import Cache
2020-08-10 00:02:07 +02:00
from matrix_alertbot.alertmanager import AlertmanagerClient
2022-06-13 20:55:01 +02:00
from matrix_alertbot.config import Config
2024-01-22 11:35:13 +01:00
from matrix_alertbot.matrix import MatrixClientPool
from matrix_alertbot.webhook import Webhook
2019-09-25 14:26:29 +02:00
logger = logging.getLogger(__name__)
def main() -> None:
"""The first function that is run when starting the bot"""
# Read user-configured options from a config file.
# A different config file path can be specified as the first command line argument
if len(sys.argv) > 1:
config_path = sys.argv[1]
else:
config_path = "config.yaml"
# Read the parsed config file and create a Config object
config = Config(config_path)
# Configure the cache
cache = Cache(config.cache_dir)
2022-07-08 23:04:04 +02:00
# Configure Alertmanager client
2022-08-08 00:28:36 +02:00
alertmanager_client = AlertmanagerClient(config.alertmanager_url, cache)
2022-07-08 23:04:04 +02:00
2024-01-22 11:35:13 +01:00
# Create matrix clients
matrix_client_pool = MatrixClientPool(alertmanager_client, cache, config)
2022-07-08 23:04:04 +02:00
# Configure webhook server
2024-01-22 11:35:13 +01:00
webhook_server = Webhook(matrix_client_pool, alertmanager_client, cache, config)
2022-07-08 23:04:04 +02:00
loop = asyncio.get_event_loop()
loop.create_task(alertmanager_client.start())
2022-07-09 12:31:05 +02:00
loop.create_task(webhook_server.start())
2024-01-22 11:35:13 +01:00
for account in config.accounts:
loop.create_task(matrix_client_pool.start(account, config))
try:
loop.run_forever()
except Exception as e:
logger.error(e)
2022-07-08 23:04:04 +02:00
finally:
loop.run_until_complete(webhook_server.close())
2022-08-08 00:28:36 +02:00
loop.run_until_complete(alertmanager_client.close())
2024-01-22 11:35:13 +01:00
loop.run_until_complete(matrix_client_pool.close())
2022-07-10 18:09:25 +02:00
cache.close()