fix some pleroma errors with async and 500 errors (#4)

* fix some pleroma errors with async and 500 errors

* add better recovery/handling of HTTP 500

* remove unnecessary else
This commit is contained in:
Joel Beckmeyer 2022-12-28 20:22:24 -05:00 committed by GitHub
parent 766b60c09c
commit 6523a28e11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 4 deletions

View File

@ -21,6 +21,9 @@ def http_session_factory(headers={}):
class BadRequest(Exception): class BadRequest(Exception):
pass pass
class BadResponse(Exception):
pass
class Pleroma: class Pleroma:
def __init__(self, *, api_base_url, access_token): def __init__(self, *, api_base_url, access_token):
self.api_base_url = api_base_url.rstrip('/') self.api_base_url = api_base_url.rstrip('/')
@ -50,6 +53,8 @@ class Pleroma:
async with self._session.request(method, self.api_base_url + path, **kwargs) as resp: async with self._session.request(method, self.api_base_url + path, **kwargs) as resp:
if resp.status == HTTPStatus.BAD_REQUEST: if resp.status == HTTPStatus.BAD_REQUEST:
raise BadRequest((await resp.json())['error']) raise BadRequest((await resp.json())['error'])
if resp.status == HTTPStatus.INTERNAL_SERVER_ERROR:
raise BadResponse((await resp.json()))
#resp.raise_for_status() #resp.raise_for_status()
return await resp.json() return await resp.json()

View File

@ -22,10 +22,22 @@ class ReplyBot:
async for notification in self.pleroma.stream_mentions(): async for notification in self.pleroma.stream_mentions():
await self.process_notification(notification) await self.process_notification(notification)
async def process_notification(self, notification): async def process_notification(self, notification, retry_count=0):
acct = "@" + notification['account']['acct'] # get the account's @ acct = "@" + notification['account']['acct'] # get the account's @
post_id = notification['status']['id'] post_id = notification['status']['id']
context = await self.pleroma.status_context(post_id)
# catch HTTP 500 and backoff on requests
retry_count = retry_count + 1
try:
context = await self.pleroma.status_context(post_id)
except pleroma.BadResponse as exc:
if retry_count < 3:
await anyio.sleep(2**retry_count)
await self.process_notification(notification, retry_count)
else:
# failed too many times in a row, logging
print(f"Received HTTP 500 {retry_count} times in a row, aborting reply attempt.")
return
# check if we've already been participating in this thread # check if we've already been participating in this thread
if self.check_thread_length(context): if self.check_thread_length(context):
@ -69,12 +81,12 @@ class ReplyBot:
await self.pleroma.react(post_id, '') await self.pleroma.react(post_id, '')
async def reply(self, notification): async def reply(self, notification):
toot = utils.make_toot(self.cfg) # generate a toot toot = await utils.make_post(self.cfg) # generate a toot
await self.pleroma.reply(notification['status'], toot, cw=self.cfg['cw']) await self.pleroma.reply(notification['status'], toot, cw=self.cfg['cw'])
@staticmethod @staticmethod
def extract_toot(toot): def extract_toot(toot):
text = utils.extract_toot(toot) text = utils.extract_post_content(toot)
text = re.sub(r"^@\S+\s", r"", text) # remove the initial mention text = re.sub(r"^@\S+\s", r"", text) # remove the initial mention
text = text.lower() # treat text as lowercase for easier keyword matching (if this bot uses it) text = text.lower() # treat text as lowercase for easier keyword matching (if this bot uses it)
return text return text