Skip to content

Commit

Permalink
Try to handle errors better
Browse files Browse the repository at this point in the history
  • Loading branch information
kdknigga committed Sep 22, 2024
1 parent 1d1d12e commit 91dc868
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
4 changes: 2 additions & 2 deletions rflying_tower_bot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ async def main() -> None:
user_agent=praw_config.client_user_agent,
username=praw_config.username,
password=praw_config.password,
ratelimit_seconds=600,
timeout=30,
ratelimit_seconds=900,
timeout=60,
validate_on_submit=True,
) as reddit:
bot_config = BotConfig(reddit)
Expand Down
4 changes: 3 additions & 1 deletion rflying_tower_bot/inbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ async def watch_inbox(self) -> None:
self.log.warning("Unknown command: %s", message.subject)

except (RequestException, ServerError) as e:
self.log.warning("Server error in post stream watcher: %s", e)
self.log.warning(
"Server error in post stream watcher: %s. Sleeping for a bit.", e
)
# Yes, I know a blocking sleep in async code is bad, but if Reddit is having a problem might as well pause the whole bot
time.sleep(60)
except KeyboardInterrupt:
Expand Down
4 changes: 3 additions & 1 deletion rflying_tower_bot/modlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ async def watch_modlog(self) -> None:
await self.config.update_rules()

except (RequestException, ServerError) as e:
self.log.warning("Server error in post stream watcher: %s", e)
self.log.warning(
"Server error in post stream watcher: %s. Sleeping for a bit.", e
)
# Yes, I know a blocking sleep in async code is bad, but if Reddit is having a problem might as well pause the whole bot
time.sleep(60)
except KeyboardInterrupt:
Expand Down
36 changes: 27 additions & 9 deletions rflying_tower_bot/post_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,30 @@ async def watch_poststream(self) -> None:
self.utilities.format_comment(comment_text)
)
except RedditAPIException as e:
self.log.error(
"API error making comment on %s: %s. Logging the comment as successful to prevent constant retrying.",
post.permalink,
e,
)
await self.config.history.add(
post.permalink, "save_post_body"
)
for sube in e.items:
self.log.error(
"API error making comment on %s: %s.",
post.permalink,
sube,
)

match sube.error_type:
case "RATELIMIT":
self.log.warning(
"Rate limit hit, sleeping for 15 minutes"
)
time.sleep(900)

# case "TOO_LONG":
case _:
self.log.error(
"Marking post as processed and moving on."
)

await self.config.history.add(
post.permalink, "save_post_body"
)

if not c:
self.log.error(
"Making comment on %s seems to have failed", str(post)
Expand All @@ -102,7 +118,9 @@ async def watch_poststream(self) -> None:
)

except (RequestException, ServerError) as e:
self.log.warning("Server error in post stream watcher: %s", e)
self.log.warning(
"Server error in post stream watcher: %s. Sleeping for a bit.", e
)
# Yes, I know a blocking sleep in async code is bad, but if Reddit is having a problem might as well pause the whole bot
time.sleep(60)
except KeyboardInterrupt:
Expand Down

0 comments on commit 91dc868

Please sign in to comment.