Skip to content

Commit

Permalink
feat(webapp): add more deprecated status triggers (#378)
Browse files Browse the repository at this point in the history
  • Loading branch information
ReenigneArcher authored Dec 16, 2024
1 parent 544a386 commit 09e0a23
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 8 deletions.
14 changes: 14 additions & 0 deletions src/common/inspector.py
Original file line number Diff line number Diff line change
@@ -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
55 changes: 47 additions & 8 deletions src/reddit/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/common/test_inspector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# local imports
from src.common import inspector


def test_current_name():
assert inspector.current_name() == 'test_current_name'

0 comments on commit 09e0a23

Please sign in to comment.