Start tracking posting failure

This commit is contained in:
Thomas Sileo 2019-08-07 23:27:57 +02:00
parent 5bc655dfd3
commit 7f22622f9c
3 changed files with 47 additions and 5 deletions

View file

@ -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 ""

View file

@ -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)

31
core/remote.py Normal file
View file

@ -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