Send alerts and reset
This commit is contained in:
+16
-2
@@ -1,4 +1,6 @@
|
||||
import time
|
||||
import asyncio
|
||||
import inspect
|
||||
|
||||
class AlertManager:
|
||||
def __init__(self):
|
||||
@@ -8,10 +10,22 @@ class AlertManager:
|
||||
def set_on_alert_callback(self, callback):
|
||||
self.on_alert_callback = callback
|
||||
|
||||
def call_on_alert_callback(self, alert):
|
||||
if self.on_alert_callback:
|
||||
if inspect.iscoroutinefunction(self.on_alert_callback):
|
||||
# Schedule coroutine on the running loop without blocking
|
||||
try:
|
||||
loop = asyncio.get_running_loop()
|
||||
loop.create_task(self.on_alert_callback(alert))
|
||||
except RuntimeError:
|
||||
# Fallback if called outside an active event loop
|
||||
asyncio.run(self.on_alert_callback(alert))
|
||||
else:
|
||||
self.on_alert_callback(alert)
|
||||
|
||||
def add_alert(self, alert):
|
||||
self.alerts.append(alert)
|
||||
if self.on_alert_callback:
|
||||
self.on_alert_callback(alert)
|
||||
self.call_on_alert_callback(alert)
|
||||
|
||||
def get_alerts(self):
|
||||
return self.alerts
|
||||
|
||||
Reference in New Issue
Block a user