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:
parent
f7cc00e443
commit
e2a18f8888
|
@ -14,6 +14,9 @@ class BadRequest(Exception):
|
||||||
class LoginFailed(Exception):
|
class LoginFailed(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('/')
|
||||||
|
@ -43,6 +46,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()
|
||||||
|
|
||||||
|
|
18
reply.py
18
reply.py
|
@ -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']
|
||||||
|
|
||||||
|
# catch HTTP 500 and backoff on requests
|
||||||
|
retry_count = retry_count + 1
|
||||||
|
try:
|
||||||
context = await self.pleroma.status_context(post_id)
|
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
|
||||||
|
|
Loading…
Reference in New Issue