aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Aggrofile14
-rw-r--r--aggro.py25
-rw-r--r--app/AggroConfig.py18
-rw-r--r--app/EmailAlerter.py47
-rw-r--r--app/PluginManager.py19
-rw-r--r--app/utils.py12
6 files changed, 121 insertions, 14 deletions
diff --git a/Aggrofile b/Aggrofile
index 96569da..6f6864c 100644
--- a/Aggrofile
+++ b/Aggrofile
@@ -1,7 +1,17 @@
{
"db_path": "db.json",
- "server_host": "0.0.0.0",
- "server_port": 8080,
+ "server": {
+ "host": "0.0.0.0",
+ "port": 8080
+ },
+ "email_alerter": {
+ "api_url": "${AGGRO_EMAIL_ALERT_API_URL}",
+ "api_auth": "${AGGRO_EMAIL_ALERT_API_AUTH}",
+ "email_from": "${AGGRO_EMAIL_ALERT_FROM}",
+ "email_to": [
+ "${AGGRO_EMAIL_ALERT_TO}"
+ ]
+ },
"plugins": {
"aaniwalli_scraper": {
"plugin": "ScraperSourcePlugin",
diff --git a/aggro.py b/aggro.py
index 27cd373..0cd7ff6 100644
--- a/aggro.py
+++ b/aggro.py
@@ -7,7 +7,7 @@ import time
from typing import Any
from tinydb import Query
-from app.AggroConfig import AggroConfig
+from app.AggroConfig import AggroConfig, AggroConfigServer, AggroConfigEmailAlerter
from app.MemoryState import memory_state
from app.PluginManager import PluginManager
from app.DatabaseManager import database_manager
@@ -25,7 +25,7 @@ def run_plugin_thread(manager: PluginManager, config: AggroConfig):
def run_server_thread(config: AggroConfig):
print("Server thread starting...")
- run_web_server(config.server_host, config.server_port)
+ run_web_server(config.server.host, config.server.port)
print("Server thread exiting...")
@@ -38,9 +38,26 @@ if __name__ == "__main__":
aggrofile_content = f.read()
aggrofile = json.loads(aggrofile_content)
+ aggrofile_server = get_config_or_default(aggrofile, "server", {})
+ server_config = AggroConfigServer(
+ host=get_config_or_default(aggrofile_server, "server_host", "localhost"),
+ port=get_config_or_default(aggrofile_server, "server_port", 8080),
+ )
+
+ aggrofile_email_alerter = get_config_or_default(aggrofile, "email_alerter", None)
+ if aggrofile_email_alerter:
+ email_alerter_config = AggroConfigEmailAlerter(
+ api_url=get_config(aggrofile_email_alerter, "api_url"),
+ api_auth=get_config(aggrofile_email_alerter, "api_auth"),
+ email_from=get_config(aggrofile_email_alerter, "email_from"),
+ email_to=get_config(aggrofile_email_alerter, "email_to"),
+ )
+ else:
+ email_alerter_config = None
+
aggro_config = AggroConfig(
- server_host=get_config_or_default(aggrofile, "server_host", "localhost"),
- server_port=get_config_or_default(aggrofile, "server_port", 8080),
+ server=server_config,
+ email_alerter=email_alerter_config,
db_path=get_config_or_default(aggrofile, "db_path", "db.json"),
plugins=get_config(aggrofile, "plugins"),
graph=get_config(aggrofile, "graph"),
diff --git a/app/AggroConfig.py b/app/AggroConfig.py
index 789fd6b..01d1230 100644
--- a/app/AggroConfig.py
+++ b/app/AggroConfig.py
@@ -4,9 +4,23 @@ from app.PluginInterface import Params
@dataclass
+class AggroConfigServer:
+ host: str
+ port: int
+
+
+@dataclass
+class AggroConfigEmailAlerter:
+ api_url: str
+ api_auth: str
+ email_from: str
+ email_to: list[str]
+
+
+@dataclass
class AggroConfig:
- server_host: str
- server_port: int
+ server: AggroConfigServer
+ email_alerter: AggroConfigEmailAlerter | None
db_path: str
plugins: dict[str, Params]
graph: dict[str, list[str]]
diff --git a/app/EmailAlerter.py b/app/EmailAlerter.py
new file mode 100644
index 0000000..823cbf1
--- /dev/null
+++ b/app/EmailAlerter.py
@@ -0,0 +1,47 @@
+import requests
+import traceback
+from datetime import datetime
+from app.AggroConfig import AggroConfigEmailAlerter
+from dataclasses import asdict
+
+
+class EmailAlerter:
+ @staticmethod
+ def from_config(config: AggroConfigEmailAlerter) -> "EmailAlerter":
+ return EmailAlerter(**asdict(config))
+
+ def __init__(
+ self, api_url: str, api_auth: str, email_from: str, email_to: list[str]
+ ):
+ self.api_url = api_url
+ self.email_from = email_from
+ self.email_to = email_to
+ api_auth_parts = api_auth.split(":")
+ if len(api_auth_parts) != 2:
+ raise Exception(
+ "[EmailAlerter] supplied api_auth is not of form <key>:<value>"
+ )
+ self.api_auth = (api_auth_parts[0], api_auth_parts[1])
+
+ def send_alert(self, text: str):
+ try:
+ now = datetime.now()
+ now_text = now.strftime("%a, %d %b %Y %H:%M:%S +0000")
+ data = {
+ "from": self.email_from,
+ "to": self.email_to,
+ "subject": f"Aggro alert on {now_text}",
+ "text": text,
+ }
+ r = requests.post(
+ self.api_url,
+ auth=self.api_auth,
+ data=data,
+ )
+ if r.status_code >= 400:
+ raise Exception(
+ f"[EmailAlerter] sending alert email via HTTP returned code {r.status_code} and body:\n{r.text}"
+ )
+
+ except:
+ traceback.print_exc()
diff --git a/app/PluginManager.py b/app/PluginManager.py
index cd810b0..86da22f 100644
--- a/app/PluginManager.py
+++ b/app/PluginManager.py
@@ -1,13 +1,16 @@
import importlib
import schedule
import time
+import sys
+import traceback
from types import ModuleType
from typing import Any
from app.Item import Item
from app.PluginInterface import Params, PluginInterface
-from app.AggroConfig import AggroConfig
+from app.AggroConfig import AggroConfig, AggroConfigEmailAlerter
from app.utils import get_config
from app.MemoryState import memory_state
+from app.EmailAlerter import EmailAlerter
class PluginManager:
@@ -16,6 +19,8 @@ class PluginManager:
self.plugin_instances: dict[str, PluginInterface] = {}
self.config = config
self.scheduled_plugin_ids: list[str] = []
+ if self.config.email_alerter:
+ self.email_alerter = EmailAlerter.from_config(self.config.email_alerter)
def load_plugin(self, plugin_name: str) -> None:
module: ModuleType = importlib.import_module(f"plugins.{plugin_name}")
@@ -36,9 +41,15 @@ class PluginManager:
if not memory_state.running:
return
- plugin: PluginInterface = self.plugin_instances[id]
- ret_items: list[Item] = plugin.process(source_id, items)
- self.propagate(id, ret_items)
+ try:
+ plugin: PluginInterface = self.plugin_instances[id]
+ ret_items: list[Item] = plugin.process(source_id, items)
+ self.propagate(id, ret_items)
+ except Exception as ex:
+ exc = traceback.format_exc()
+ print(exc, file=sys.stderr)
+ if self.email_alerter:
+ self.email_alerter.send_alert(exc)
def build_plugin_instances(self):
for id in self.config.plugins:
diff --git a/app/utils.py b/app/utils.py
index 6deb147..5401c5c 100644
--- a/app/utils.py
+++ b/app/utils.py
@@ -22,7 +22,11 @@ def get_config(config: dict[str, Any], key: str) -> Any:
except KeyError:
raise Exception(f"no {key} field in config: " + str(config))
- return evaluate_env_ref(v)
+ if type(v) == list:
+ mapped = map(evaluate_env_ref, v)
+ return list(mapped)
+ else:
+ return evaluate_env_ref(v)
def get_config_or_default(
@@ -30,7 +34,11 @@ def get_config_or_default(
) -> Any:
v: Any = config.get(key, default)
- return evaluate_env_ref(v)
+ if type(v) == list:
+ mapped = map(evaluate_env_ref, v)
+ return list(mapped)
+ else:
+ return evaluate_env_ref(v)
def item_to_dict(item: Item) -> ItemDict: