10
10
import discord
11
11
import praw
12
12
from praw import models
13
+ import prawcore
13
14
14
15
# local imports
15
16
from src .common import common
16
17
from src .common import globals
18
+ from src .common import inspector
17
19
18
20
19
21
class Bot :
20
22
def __init__ (self , ** kwargs ):
21
23
self .STOP_SIGNAL = False
22
24
self .DEGRADED = False
25
+ self .DEGRADED_REASONS = []
23
26
24
27
# threads
25
28
self .bot_thread = threading .Thread (target = lambda : None )
@@ -70,6 +73,8 @@ def validate_env(self) -> bool:
70
73
if env not in os .environ :
71
74
sys .stderr .write (f"Environment variable ``{ env } `` must be defined\n " )
72
75
self .DEGRADED = True
76
+ reason = inspector .current_name ()
77
+ self .DEGRADED_REASONS .append (reason ) if reason not in self .DEGRADED_REASONS else None
73
78
return False
74
79
return True
75
80
@@ -167,6 +172,8 @@ def discord(self, submission: models.Submission):
167
172
redditor = self .reddit .redditor (name = submission .author )
168
173
except Exception :
169
174
self .DEGRADED = True
175
+ reason = inspector .current_name ()
176
+ self .DEGRADED_REASONS .append (reason ) if reason not in self .DEGRADED_REASONS else None
170
177
return
171
178
172
179
# create the discord embed
@@ -237,21 +244,49 @@ def slash_commands(self, comment: models.Comment):
237
244
238
245
def _comment_loop (self , test : bool = False ):
239
246
# 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 :
242
249
if self .STOP_SIGNAL :
243
250
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 )
246
267
247
268
def _submission_loop (self , test : bool = False ):
248
269
# 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 :
251
272
if self .STOP_SIGNAL :
252
273
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 )
255
290
256
291
def start (self ):
257
292
# start comment and submission loops in separate threads
@@ -269,12 +304,16 @@ def start_threaded(self):
269
304
except KeyboardInterrupt :
270
305
print ("Keyboard Interrupt Detected" )
271
306
self .DEGRADED = True
307
+ reason = inspector .current_name ()
308
+ self .DEGRADED_REASONS .append (reason ) if reason not in self .DEGRADED_REASONS else None
272
309
self .stop ()
273
310
274
311
def stop (self ):
275
312
print ("Attempting to stop reddit bot" )
276
313
self .STOP_SIGNAL = True
277
314
self .DEGRADED = True
315
+ reason = inspector .current_name ()
316
+ self .DEGRADED_REASONS .append (reason ) if reason not in self .DEGRADED_REASONS else None
278
317
if self .bot_thread is not None and self .bot_thread .is_alive ():
279
318
self .comment_thread .join ()
280
319
self .submission_thread .join ()
0 commit comments