From 7f22622f9cc236615278e950df2cd555040ba8cf Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Wed, 7 Aug 2019 23:27:57 +0200 Subject: [PATCH] Start tracking posting failure --- blueprints/tasks.py | 16 +++++++++++----- core/db.py | 5 +++++ core/remote.py | 31 +++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 core/remote.py diff --git a/blueprints/tasks.py b/blueprints/tasks.py index f98ea6e..686d72a 100644 --- a/blueprints/tasks.py +++ b/blueprints/tasks.py @@ -24,6 +24,8 @@ from core.meta import MetaKey from core.meta import _meta from core.notifications import set_inbox_flags from core.outbox import process_outbox +from core.remote import track_failed_send +from core.remote import track_successful_send from core.shared import MY_PERSON from core.shared import _Response from core.shared import back @@ -315,11 +317,6 @@ def task_post_to_remote_inbox() -> _Response: app.logger.info("generating sig") signed_payload = json.loads(payload) - # XXX Disable JSON-LD signature crap for now (as HTTP signatures are enough for most implementations) - # Don't overwrite the signature if we're forwarding an activity - # if "signature" not in signed_payload: - # generate_signature(signed_payload, KEY) - app.logger.info("to=%s", to) resp = requests.post( to, @@ -335,16 +332,25 @@ def task_post_to_remote_inbox() -> _Response: app.logger.info("resp_body=%s", resp.text) resp.raise_for_status() except HTTPError as err: + track_failed_send(to) + app.logger.exception("request failed") if 400 >= err.response.status_code >= 499: app.logger.info("client error, no retry") return "" raise TaskError() from err + except requests.RequestException: + track_failed_send(to) + + app.logger.exception("request failed") + except Exception as err: app.logger.exception("task failed") raise TaskError() from err + track_successful_send(to) + return "" diff --git a/core/db.py b/core/db.py index 59b5429..8d3e5c1 100644 --- a/core/db.py +++ b/core/db.py @@ -13,6 +13,7 @@ _Doc = Optional[Dict[str, Any]] @unique class CollectionName(Enum): ACTIVITIES = "activities" + REMOTE = "remote" def find_one_activity(q: _Q) -> _Doc: @@ -25,3 +26,7 @@ def update_one_activity(q: _Q, update: _Q) -> None: def update_many_activities(q: _Q, update: _Q) -> None: DB[CollectionName.ACTIVITIES.value].update_many(q, update) + + +def update_one_remote(filter_: _Q, update: _Q, upsert: bool = False) -> None: + DB[CollectionName.REMOTE.value].update_one(filter_, update, upsert) diff --git a/core/remote.py b/core/remote.py new file mode 100644 index 0000000..8797c3e --- /dev/null +++ b/core/remote.py @@ -0,0 +1,31 @@ +from urllib.parse import urlparse + +from core.db import _Q +from core.db import update_one_remote +from utils import now + + +def server(url: str) -> str: + return urlparse(url).netloc + + +def _update(url: str, replace: _Q) -> None: + update_one_remote({"server": server(url)}, replace, upsert=True) + + +def track_successful_send(url: str) -> None: + now_ = now() + _update( + url, + { + "$inc": {"successful_send": 1}, + "$set": {"last_successful_contact": now_, "last_contact": now_}, + }, + ) + return None + + +def track_failed_send(url: str) -> None: + now_ = now() + _update(url, {"$inc": {"failed_send": 1}, "$set": {"last_contact": now_}}) + return None