Revert "reuse same cache everywhere"

This reverts commit a3b13e3796.
This commit is contained in:
HgO 2022-07-06 01:19:44 +02:00
parent fa0cc9bfca
commit baf73ea5a2
5 changed files with 12 additions and 15 deletions

View file

@ -1,7 +1,7 @@
import datetime import datetime
from typing import Dict, List from typing import Dict, List
from diskcache import Cache import diskcache
import pytimeparse import pytimeparse
import requests import requests
from requests import RequestException from requests import RequestException
@ -15,9 +15,10 @@ from matrix_alertbot.errors import (
class AlertmanagerClient: 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.api_url = f"{url}/api/v2"
self.cache = cache self.cache = diskcache.Cache(config.cache_dir)
def get_alerts(self) -> List[Dict]: def get_alerts(self) -> List[Dict]:
try: try:

View file

@ -25,7 +25,6 @@ class Callbacks:
self, self,
client: AsyncClient, client: AsyncClient,
alertmanager: AlertmanagerClient, alertmanager: AlertmanagerClient,
cache: Cache,
config: Config, config: Config,
): ):
""" """
@ -39,7 +38,7 @@ class Callbacks:
config: Bot configuration parameters. config: Bot configuration parameters.
""" """
self.client = client self.client = client
self.cache = cache self.cache = Cache(config.cache_dir)
self.alertmanager = alertmanager self.alertmanager = alertmanager
self.config = config self.config = config
self.command_prefix = config.command_prefix self.command_prefix = config.command_prefix

View file

@ -88,11 +88,8 @@ def main() -> None:
# Read the parsed config file and create a Config object # Read the parsed config file and create a Config object
config = Config(config_path) config = Config(config_path)
# Configure the cache
cache = diskcache.Cache(config.cache_dir)
# Configure Alertmanager client # Configure Alertmanager client
alertmanager = AlertmanagerClient(config.alertmanager_url, cache) alertmanager = AlertmanagerClient(config)
# Configuration options for the AsyncClient # Configuration options for the AsyncClient
client_config = AsyncClientConfig( client_config = AsyncClientConfig(
@ -116,7 +113,7 @@ def main() -> None:
client.user_id = config.user_id client.user_id = config.user_id
# Set up event callbacks # 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.message, (RoomMessageText,))
client.add_event_callback( client.add_event_callback(
callbacks.invite_event_filtered_callback, (InviteMemberEvent,) 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.decryption_failure, (MegolmEvent,))
client.add_event_callback(callbacks.unknown, (UnknownEvent,)) client.add_event_callback(callbacks.unknown, (UnknownEvent,))
webhook_server = Webhook(client, cache, config) webhook_server = Webhook(client, config)
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
loop.create_task(webhook_server.start()) loop.create_task(webhook_server.start())

View file

@ -48,7 +48,9 @@ async def create_alert(request: web_request.Request) -> web.Response:
class Webhook: 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 = web.Application(logger=logger)
self.app["client"] = client self.app["client"] = client
self.app["config"] = config self.app["config"] = config

View file

@ -2,7 +2,6 @@ import unittest
from unittest.mock import Mock from unittest.mock import Mock
import nio import nio
from diskcache import Cache
from matrix_alertbot.alertmanager import AlertmanagerClient from matrix_alertbot.alertmanager import AlertmanagerClient
from matrix_alertbot.callbacks import Callbacks from matrix_alertbot.callbacks import Callbacks
@ -16,14 +15,13 @@ class CallbacksTestCase(unittest.TestCase):
self.fake_client = Mock(spec=nio.AsyncClient) self.fake_client = Mock(spec=nio.AsyncClient)
self.fake_client.user = "@fake_user:example.com" self.fake_client.user = "@fake_user:example.com"
self.fake_cache = Mock(spec=Cache)
self.fake_alertmanager = Mock(spec=AlertmanagerClient) self.fake_alertmanager = Mock(spec=AlertmanagerClient)
# We don't spec config, as it doesn't currently have well defined attributes # We don't spec config, as it doesn't currently have well defined attributes
self.fake_config = Mock() self.fake_config = Mock()
self.callbacks = Callbacks( 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: def test_invite(self) -> None: