fix silence ID caching

This commit is contained in:
HgO 2022-07-17 00:21:44 +02:00
parent fba263afe8
commit 073cde37db
3 changed files with 14 additions and 9 deletions

View file

@ -104,7 +104,7 @@ class Callbacks:
alert_event_id, alert_event_id,
) )
except TypeError as e: except TypeError as e:
logging.error(f"Unable to process the command '{cmd}': {e}") logging.error(f"Unable to create the command '{cmd}': {e}")
return return
await command.process() await command.process()
@ -215,7 +215,7 @@ class Callbacks:
alert_event_id, alert_event_id,
) )
except TypeError as e: except TypeError as e:
logging.error(f"Unable to process the command '{cmd}': {e}") logging.error(f"Unable to create the command '{cmd}': {e}")
return return
await command.process() await command.process()
@ -233,7 +233,10 @@ class Callbacks:
logger.warning(f"Error removing silence from reaction {event.redacts}") logger.warning(f"Error removing silence from reaction {event.redacts}")
return return
alert_event_id = self.cache[event.redacts] logger.debug(
f"Read alert event ID for redacted event {event.redacts} from cache"
)
alert_event_id: str = self.cache[event.redacts]
try: try:
command = CommandFactory.create( command = CommandFactory.create(
@ -248,7 +251,7 @@ class Callbacks:
alert_event_id, alert_event_id,
) )
except TypeError as e: except TypeError as e:
logging.error(f"Unable to process the command 'unack': {e}") logging.error(f"Unable to create the command 'unack': {e}")
return return
await command.process() await command.process()

View file

@ -1,5 +1,5 @@
import logging import logging
from typing import List, Optional from typing import List, Optional, Tuple
import pytimeparse2 import pytimeparse2
from diskcache import Cache from diskcache import Cache
@ -129,7 +129,7 @@ class AckAlertCommand(BaseAlertCommand):
) )
return return
alert_fingerprints = self.cache[self.alert_event_id] alert_fingerprints: Tuple[str] = self.cache[self.alert_event_id]
logger.debug(f"Found {len(alert_fingerprints)} in cache") logger.debug(f"Found {len(alert_fingerprints)} in cache")
count_alert_not_found = 0 count_alert_not_found = 0
@ -153,7 +153,9 @@ class AckAlertCommand(BaseAlertCommand):
except AlertmanagerError as e: except AlertmanagerError as e:
logger.exception(f"Unable to create silence: {e}", exc_info=e) logger.exception(f"Unable to create silence: {e}", exc_info=e)
self.cache.set(self.event_id, tuple(created_silences), expire=duration_seconds) matchers_id = "".join(sorted(str(matcher) for matcher in matchers))
ack_id = "".join(alert_fingerprints) + str(duration_seconds) + matchers_id
self.cache.set(ack_id, tuple(created_silences), expire=duration_seconds)
if count_alert_not_found > 0: if count_alert_not_found > 0:
await send_text_to_room( await send_text_to_room(
@ -195,7 +197,7 @@ class UnackAlertCommand(BaseAlertCommand):
) )
return return
alert_fingerprints = self.cache[self.alert_event_id] alert_fingerprints: Tuple[str] = self.cache[self.alert_event_id]
logger.debug(f"Found {len(alert_fingerprints)} in cache") logger.debug(f"Found {len(alert_fingerprints)} in cache")
count_alert_not_found = 0 count_alert_not_found = 0

View file

@ -64,7 +64,7 @@ async def create_alert(request: web_request.Request) -> web.Response:
status=500, body="An error occured when sending alerts to Matrix room." status=500, body="An error occured when sending alerts to Matrix room."
) )
fingerprints = tuple(alert["fingerprint"] for alert in data["alerts"]) fingerprints = tuple(sorted(alert["fingerprint"] for alert in data["alerts"]))
cache.set( cache.set(
event.event_id, fingerprints, expire=config.cache_expire_time, tag="event" event.event_id, fingerprints, expire=config.cache_expire_time, tag="event"
) )