From cac30237b347fbff0a865d08f091d95631a13a3c Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Sun, 15 Dec 2024 20:55:58 -0500 Subject: [PATCH] feat(webapp): add more deprecated status triggers --- src/common/inspector.py | 14 ++++++++ src/reddit/bot.py | 55 ++++++++++++++++++++++++----- tests/unit/common/test_inspector.py | 6 ++++ 3 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 src/common/inspector.py create mode 100644 tests/unit/common/test_inspector.py diff --git a/src/common/inspector.py b/src/common/inspector.py new file mode 100644 index 0000000..0f48346 --- /dev/null +++ b/src/common/inspector.py @@ -0,0 +1,14 @@ +# standard imports +import inspect + + +def current_name() -> str: + """ + Get the name of the function that called this function + + Returns + ------- + str + the name of the function that called this function + """ + return inspect.currentframe().f_back.f_code.co_name diff --git a/src/reddit/bot.py b/src/reddit/bot.py index d8178b4..b11a9f5 100644 --- a/src/reddit/bot.py +++ b/src/reddit/bot.py @@ -10,16 +10,19 @@ import discord import praw from praw import models +import prawcore # local imports from src.common import common from src.common import globals +from src.common import inspector class Bot: def __init__(self, **kwargs): self.STOP_SIGNAL = False self.DEGRADED = False + self.DEGRADED_REASONS = [] # threads self.bot_thread = threading.Thread(target=lambda: None) @@ -70,6 +73,8 @@ def validate_env(self) -> bool: if env not in os.environ: sys.stderr.write(f"Environment variable ``{env}`` must be defined\n") self.DEGRADED = True + reason = inspector.current_name() + self.DEGRADED_REASONS.append(reason) if reason not in self.DEGRADED_REASONS else None return False return True @@ -167,6 +172,8 @@ def discord(self, submission: models.Submission): redditor = self.reddit.redditor(name=submission.author) except Exception: self.DEGRADED = True + reason = inspector.current_name() + self.DEGRADED_REASONS.append(reason) if reason not in self.DEGRADED_REASONS else None return # create the discord embed @@ -237,21 +244,49 @@ def slash_commands(self, comment: models.Comment): def _comment_loop(self, test: bool = False): # process comments and then keep monitoring - for comment in self.subreddit.stream.comments(): - self.process_comment(comment=comment) + reason = inspector.current_name() + while True: if self.STOP_SIGNAL: break - if test: - return comment + + if self.DEGRADED and reason in self.DEGRADED_REASONS and len(self.DEGRADED_REASONS) == 1: + self.DEGRADED = False + + try: + for comment in self.subreddit.stream.comments(): + self.process_comment(comment=comment) + if self.STOP_SIGNAL: + break + if test: + return comment + except prawcore.exceptions.ServerError as e: + print(f"Server Error: {e}") + self.DEGRADED = True + self.DEGRADED_REASONS.append(reason) if reason not in self.DEGRADED_REASONS else None + time.sleep(60) def _submission_loop(self, test: bool = False): # process submissions and then keep monitoring - for submission in self.subreddit.stream.submissions(): - self.process_submission(submission=submission) + reason = inspector.current_name() + while True: if self.STOP_SIGNAL: break - if test: - return submission + + if self.DEGRADED and reason in self.DEGRADED_REASONS and len(self.DEGRADED_REASONS) == 1: + self.DEGRADED = False + + try: + for submission in self.subreddit.stream.submissions(): + self.process_submission(submission=submission) + if self.STOP_SIGNAL: + break + if test: + return submission + except prawcore.exceptions.ServerError as e: + print(f"Server Error: {e}") + self.DEGRADED = True + self.DEGRADED_REASONS.append(reason) if reason not in self.DEGRADED_REASONS else None + time.sleep(60) def start(self): # start comment and submission loops in separate threads @@ -269,12 +304,16 @@ def start_threaded(self): except KeyboardInterrupt: print("Keyboard Interrupt Detected") self.DEGRADED = True + reason = inspector.current_name() + self.DEGRADED_REASONS.append(reason) if reason not in self.DEGRADED_REASONS else None self.stop() def stop(self): print("Attempting to stop reddit bot") self.STOP_SIGNAL = True self.DEGRADED = True + reason = inspector.current_name() + self.DEGRADED_REASONS.append(reason) if reason not in self.DEGRADED_REASONS else None if self.bot_thread is not None and self.bot_thread.is_alive(): self.comment_thread.join() self.submission_thread.join() diff --git a/tests/unit/common/test_inspector.py b/tests/unit/common/test_inspector.py new file mode 100644 index 0000000..a770202 --- /dev/null +++ b/tests/unit/common/test_inspector.py @@ -0,0 +1,6 @@ +# local imports +from src.common import inspector + + +def test_current_name(): + assert inspector.current_name() == 'test_current_name'