Skip to content

Commit 09e0a23

Browse files
feat(webapp): add more deprecated status triggers (#378)
1 parent 544a386 commit 09e0a23

File tree

3 files changed

+67
-8
lines changed

3 files changed

+67
-8
lines changed

src/common/inspector.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# standard imports
2+
import inspect
3+
4+
5+
def current_name() -> str:
6+
"""
7+
Get the name of the function that called this function
8+
9+
Returns
10+
-------
11+
str
12+
the name of the function that called this function
13+
"""
14+
return inspect.currentframe().f_back.f_code.co_name

src/reddit/bot.py

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,19 @@
1010
import discord
1111
import praw
1212
from praw import models
13+
import prawcore
1314

1415
# local imports
1516
from src.common import common
1617
from src.common import globals
18+
from src.common import inspector
1719

1820

1921
class Bot:
2022
def __init__(self, **kwargs):
2123
self.STOP_SIGNAL = False
2224
self.DEGRADED = False
25+
self.DEGRADED_REASONS = []
2326

2427
# threads
2528
self.bot_thread = threading.Thread(target=lambda: None)
@@ -70,6 +73,8 @@ def validate_env(self) -> bool:
7073
if env not in os.environ:
7174
sys.stderr.write(f"Environment variable ``{env}`` must be defined\n")
7275
self.DEGRADED = True
76+
reason = inspector.current_name()
77+
self.DEGRADED_REASONS.append(reason) if reason not in self.DEGRADED_REASONS else None
7378
return False
7479
return True
7580

@@ -167,6 +172,8 @@ def discord(self, submission: models.Submission):
167172
redditor = self.reddit.redditor(name=submission.author)
168173
except Exception:
169174
self.DEGRADED = True
175+
reason = inspector.current_name()
176+
self.DEGRADED_REASONS.append(reason) if reason not in self.DEGRADED_REASONS else None
170177
return
171178

172179
# create the discord embed
@@ -237,21 +244,49 @@ def slash_commands(self, comment: models.Comment):
237244

238245
def _comment_loop(self, test: bool = False):
239246
# process comments and then keep monitoring
240-
for comment in self.subreddit.stream.comments():
241-
self.process_comment(comment=comment)
247+
reason = inspector.current_name()
248+
while True:
242249
if self.STOP_SIGNAL:
243250
break
244-
if test:
245-
return comment
251+
252+
if self.DEGRADED and reason in self.DEGRADED_REASONS and len(self.DEGRADED_REASONS) == 1:
253+
self.DEGRADED = False
254+
255+
try:
256+
for comment in self.subreddit.stream.comments():
257+
self.process_comment(comment=comment)
258+
if self.STOP_SIGNAL:
259+
break
260+
if test:
261+
return comment
262+
except prawcore.exceptions.ServerError as e:
263+
print(f"Server Error: {e}")
264+
self.DEGRADED = True
265+
self.DEGRADED_REASONS.append(reason) if reason not in self.DEGRADED_REASONS else None
266+
time.sleep(60)
246267

247268
def _submission_loop(self, test: bool = False):
248269
# process submissions and then keep monitoring
249-
for submission in self.subreddit.stream.submissions():
250-
self.process_submission(submission=submission)
270+
reason = inspector.current_name()
271+
while True:
251272
if self.STOP_SIGNAL:
252273
break
253-
if test:
254-
return submission
274+
275+
if self.DEGRADED and reason in self.DEGRADED_REASONS and len(self.DEGRADED_REASONS) == 1:
276+
self.DEGRADED = False
277+
278+
try:
279+
for submission in self.subreddit.stream.submissions():
280+
self.process_submission(submission=submission)
281+
if self.STOP_SIGNAL:
282+
break
283+
if test:
284+
return submission
285+
except prawcore.exceptions.ServerError as e:
286+
print(f"Server Error: {e}")
287+
self.DEGRADED = True
288+
self.DEGRADED_REASONS.append(reason) if reason not in self.DEGRADED_REASONS else None
289+
time.sleep(60)
255290

256291
def start(self):
257292
# start comment and submission loops in separate threads
@@ -269,12 +304,16 @@ def start_threaded(self):
269304
except KeyboardInterrupt:
270305
print("Keyboard Interrupt Detected")
271306
self.DEGRADED = True
307+
reason = inspector.current_name()
308+
self.DEGRADED_REASONS.append(reason) if reason not in self.DEGRADED_REASONS else None
272309
self.stop()
273310

274311
def stop(self):
275312
print("Attempting to stop reddit bot")
276313
self.STOP_SIGNAL = True
277314
self.DEGRADED = True
315+
reason = inspector.current_name()
316+
self.DEGRADED_REASONS.append(reason) if reason not in self.DEGRADED_REASONS else None
278317
if self.bot_thread is not None and self.bot_thread.is_alive():
279318
self.comment_thread.join()
280319
self.submission_thread.join()

tests/unit/common/test_inspector.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# local imports
2+
from src.common import inspector
3+
4+
5+
def test_current_name():
6+
assert inspector.current_name() == 'test_current_name'

0 commit comments

Comments
 (0)