Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(webapp): add more deprecated status triggers #378

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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

Check warning on line 77 in src/reddit/bot.py

View check run for this annotation

Codecov / codecov/patch

src/reddit/bot.py#L76-L77

Added lines #L76 - L77 were not covered by tests
return False
return True

Expand Down Expand Up @@ -167,6 +172,8 @@
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

Check warning on line 176 in src/reddit/bot.py

View check run for this annotation

Codecov / codecov/patch

src/reddit/bot.py#L175-L176

Added lines #L175 - L176 were not covered by tests
return

# create the discord embed
Expand Down Expand Up @@ -237,21 +244,49 @@

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

Check warning on line 253 in src/reddit/bot.py

View check run for this annotation

Codecov / codecov/patch

src/reddit/bot.py#L253

Added line #L253 was not covered by tests

try:
for comment in self.subreddit.stream.comments():
self.process_comment(comment=comment)
if self.STOP_SIGNAL:
break

Check warning on line 259 in src/reddit/bot.py

View check run for this annotation

Codecov / codecov/patch

src/reddit/bot.py#L259

Added line #L259 was not covered by tests
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)

Check warning on line 266 in src/reddit/bot.py

View check run for this annotation

Codecov / codecov/patch

src/reddit/bot.py#L262-L266

Added lines #L262 - L266 were not covered by tests

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

Check warning on line 276 in src/reddit/bot.py

View check run for this annotation

Codecov / codecov/patch

src/reddit/bot.py#L276

Added line #L276 was not covered by tests

try:
for submission in self.subreddit.stream.submissions():
self.process_submission(submission=submission)
if self.STOP_SIGNAL:
break

Check warning on line 282 in src/reddit/bot.py

View check run for this annotation

Codecov / codecov/patch

src/reddit/bot.py#L282

Added line #L282 was not covered by tests
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)

Check warning on line 289 in src/reddit/bot.py

View check run for this annotation

Codecov / codecov/patch

src/reddit/bot.py#L285-L289

Added lines #L285 - L289 were not covered by tests

def start(self):
# start comment and submission loops in separate threads
Expand All @@ -269,12 +304,16 @@
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

Check warning on line 308 in src/reddit/bot.py

View check run for this annotation

Codecov / codecov/patch

src/reddit/bot.py#L307-L308

Added lines #L307 - L308 were not covered by tests
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

Check warning on line 316 in src/reddit/bot.py

View check run for this annotation

Codecov / codecov/patch

src/reddit/bot.py#L315-L316

Added lines #L315 - L316 were not covered by tests
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'
Loading