From baf73ea5a238a840b86d31b77f197fec0e559a07 Mon Sep 17 00:00:00 2001 From: HgO Date: Wed, 6 Jul 2022 01:19:44 +0200 Subject: [PATCH] Revert "reuse same cache everywhere" This reverts commit a3b13e3796a17fa434036a52bf1da7d1f2f33231. --- matrix_alertbot/alertmanager.py | 7 ++++--- matrix_alertbot/callbacks.py | 3 +-- matrix_alertbot/main.py | 9 +++------ matrix_alertbot/webhook.py | 4 +++- tests/test_callbacks.py | 4 +--- 5 files changed, 12 insertions(+), 15 deletions(-) diff --git a/matrix_alertbot/alertmanager.py b/matrix_alertbot/alertmanager.py index 1bdc7c4..bab8a0c 100644 --- a/matrix_alertbot/alertmanager.py +++ b/matrix_alertbot/alertmanager.py @@ -1,7 +1,7 @@ import datetime from typing import Dict, List -from diskcache import Cache +import diskcache import pytimeparse import requests from requests import RequestException @@ -15,9 +15,10 @@ from matrix_alertbot.errors import ( class AlertmanagerClient: - def __init__(self, url: str, cache: Cache) -> None: + def __init__(self, config: Config) -> None: + url = config.alertmanager_url self.api_url = f"{url}/api/v2" - self.cache = cache + self.cache = diskcache.Cache(config.cache_dir) def get_alerts(self) -> List[Dict]: try: diff --git a/matrix_alertbot/callbacks.py b/matrix_alertbot/callbacks.py index 0833726..8f82d52 100644 --- a/matrix_alertbot/callbacks.py +++ b/matrix_alertbot/callbacks.py @@ -25,7 +25,6 @@ class Callbacks: self, client: AsyncClient, alertmanager: AlertmanagerClient, - cache: Cache, config: Config, ): """ @@ -39,7 +38,7 @@ class Callbacks: config: Bot configuration parameters. """ self.client = client - self.cache = cache + self.cache = Cache(config.cache_dir) self.alertmanager = alertmanager self.config = config self.command_prefix = config.command_prefix diff --git a/matrix_alertbot/main.py b/matrix_alertbot/main.py index 8092b7d..920fc97 100644 --- a/matrix_alertbot/main.py +++ b/matrix_alertbot/main.py @@ -88,11 +88,8 @@ def main() -> None: # Read the parsed config file and create a Config object config = Config(config_path) - # Configure the cache - cache = diskcache.Cache(config.cache_dir) - # Configure Alertmanager client - alertmanager = AlertmanagerClient(config.alertmanager_url, cache) + alertmanager = AlertmanagerClient(config) # Configuration options for the AsyncClient client_config = AsyncClientConfig( @@ -116,7 +113,7 @@ def main() -> None: client.user_id = config.user_id # Set up event callbacks - callbacks = Callbacks(client, alertmanager, cache, config) + callbacks = Callbacks(client, alertmanager, config) client.add_event_callback(callbacks.message, (RoomMessageText,)) client.add_event_callback( callbacks.invite_event_filtered_callback, (InviteMemberEvent,) @@ -124,7 +121,7 @@ def main() -> None: client.add_event_callback(callbacks.decryption_failure, (MegolmEvent,)) client.add_event_callback(callbacks.unknown, (UnknownEvent,)) - webhook_server = Webhook(client, cache, config) + webhook_server = Webhook(client, config) loop = asyncio.get_event_loop() loop.create_task(webhook_server.start()) diff --git a/matrix_alertbot/webhook.py b/matrix_alertbot/webhook.py index 80b45c3..fd81788 100644 --- a/matrix_alertbot/webhook.py +++ b/matrix_alertbot/webhook.py @@ -48,7 +48,9 @@ async def create_alert(request: web_request.Request) -> web.Response: class Webhook: - def __init__(self, client: AsyncClient, cache: Cache, config: Config) -> None: + def __init__(self, client: AsyncClient, config: Config) -> None: + cache = Cache(config.cache_dir) + self.app = web.Application(logger=logger) self.app["client"] = client self.app["config"] = config diff --git a/tests/test_callbacks.py b/tests/test_callbacks.py index 4c1fa11..41e6733 100644 --- a/tests/test_callbacks.py +++ b/tests/test_callbacks.py @@ -2,7 +2,6 @@ import unittest from unittest.mock import Mock import nio -from diskcache import Cache from matrix_alertbot.alertmanager import AlertmanagerClient from matrix_alertbot.callbacks import Callbacks @@ -16,14 +15,13 @@ class CallbacksTestCase(unittest.TestCase): self.fake_client = Mock(spec=nio.AsyncClient) self.fake_client.user = "@fake_user:example.com" - self.fake_cache = Mock(spec=Cache) self.fake_alertmanager = Mock(spec=AlertmanagerClient) # We don't spec config, as it doesn't currently have well defined attributes self.fake_config = Mock() self.callbacks = Callbacks( - self.fake_client, self.fake_cache, self.fake_alertmanager, self.fake_config + self.fake_client, self.fake_alertmanager, self.fake_config ) def test_invite(self) -> None: