reuse same cache everywhere
This commit is contained in:
parent
6687e7a92a
commit
a3b13e3796
5 changed files with 15 additions and 12 deletions
|
@ -1,7 +1,7 @@
|
||||||
import datetime
|
import datetime
|
||||||
from typing import Dict, List
|
from typing import Dict, List
|
||||||
|
|
||||||
import diskcache
|
from diskcache import Cache
|
||||||
import pytimeparse
|
import pytimeparse
|
||||||
import requests
|
import requests
|
||||||
from requests import RequestException
|
from requests import RequestException
|
||||||
|
@ -15,10 +15,9 @@ from matrix_alertbot.errors import (
|
||||||
|
|
||||||
|
|
||||||
class AlertmanagerClient:
|
class AlertmanagerClient:
|
||||||
def __init__(self, config: Config) -> None:
|
def __init__(self, url: str, cache: Cache) -> None:
|
||||||
url = config.alertmanager_url
|
|
||||||
self.api_url = f"{url}/api/v2"
|
self.api_url = f"{url}/api/v2"
|
||||||
self.cache = diskcache.Cache(config.cache_dir)
|
self.cache = cache
|
||||||
|
|
||||||
def get_alerts(self) -> List[Dict]:
|
def get_alerts(self) -> List[Dict]:
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -25,6 +25,7 @@ class Callbacks:
|
||||||
self,
|
self,
|
||||||
client: AsyncClient,
|
client: AsyncClient,
|
||||||
alertmanager: AlertmanagerClient,
|
alertmanager: AlertmanagerClient,
|
||||||
|
cache: Cache,
|
||||||
config: Config,
|
config: Config,
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
|
@ -38,7 +39,7 @@ class Callbacks:
|
||||||
config: Bot configuration parameters.
|
config: Bot configuration parameters.
|
||||||
"""
|
"""
|
||||||
self.client = client
|
self.client = client
|
||||||
self.cache = Cache(config.cache_dir)
|
self.cache = cache
|
||||||
self.alertmanager = alertmanager
|
self.alertmanager = alertmanager
|
||||||
self.config = config
|
self.config = config
|
||||||
self.command_prefix = config.command_prefix
|
self.command_prefix = config.command_prefix
|
||||||
|
|
|
@ -88,8 +88,11 @@ 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 = AlertmanagerClient(config.alertmanager_url, cache)
|
||||||
|
|
||||||
# Configuration options for the AsyncClient
|
# Configuration options for the AsyncClient
|
||||||
client_config = AsyncClientConfig(
|
client_config = AsyncClientConfig(
|
||||||
|
@ -113,7 +116,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, config)
|
callbacks = Callbacks(client, alertmanager, cache, 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,)
|
||||||
|
@ -121,7 +124,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, config)
|
webhook_server = Webhook(client, cache, config)
|
||||||
|
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
loop.create_task(webhook_server.start())
|
loop.create_task(webhook_server.start())
|
||||||
|
|
|
@ -48,9 +48,7 @@ async def create_alert(request: web_request.Request) -> web.Response:
|
||||||
|
|
||||||
|
|
||||||
class Webhook:
|
class Webhook:
|
||||||
def __init__(self, client: AsyncClient, config: Config) -> None:
|
def __init__(self, client: AsyncClient, cache: Cache, 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
|
||||||
|
|
|
@ -2,6 +2,7 @@ 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
|
||||||
|
@ -15,13 +16,14 @@ 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_alertmanager, self.fake_config
|
self.fake_client, self.fake_cache, self.fake_alertmanager, self.fake_config
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_invite(self) -> None:
|
def test_invite(self) -> None:
|
||||||
|
|
Loading…
Reference in a new issue