Added handling for temporary connection loss

This commit is contained in:
Collin R
2020-02-09 22:09:10 -08:00
parent de01671e98
commit 582c2c5a39

View File

@@ -19,7 +19,11 @@ MOD_SOLVED_PAT = re.compile('/[Ss]olved')
def run(): def run():
cfg = config.load() cfg = config.load()
levels = cfg.levels levels = cfg.levels
db = database.Database(cfg.database_path)
# Run indefinitely, reconnecting any time a connection is lost
while True:
try:
reddit = praw.Reddit(client_id=cfg.client_id, reddit = praw.Reddit(client_id=cfg.client_id,
client_secret=cfg.client_secret, client_secret=cfg.client_secret,
username=cfg.username, username=cfg.username,
@@ -33,11 +37,18 @@ def run():
is_mod = bool(subreddit.moderator(redditor=reddit.user.me())) is_mod = bool(subreddit.moderator(redditor=reddit.user.me()))
print_level(1, f'Is mod? {is_mod}') print_level(1, f'Is mod? {is_mod}')
db = database.Database(cfg.database_path) monitor_comments(subreddit, db)
# Ignoring other potential exceptions for now, since we may not be able
# to recover from them as well as from this one
except prawcore.exceptions.ServerError as e:
print('Lost connection to Reddit; attempting to reconnect....')
# Monitor new comments for confirmed solutions
# Passing pause_after=0 will bypass the internal exponential delay; instead, def monitor_comments(subreddit, db):
# have to check if any comments are returned with each query """Monitor new comments in the subreddit, looking for confirmed solutions.
"""
# Passing pause_after=0 will bypass the internal exponential delay, but have
# to check if any comments are returned with each query
for comm in subreddit.stream.comments(skip_existing=True, pause_after=0): for comm in subreddit.stream.comments(skip_existing=True, pause_after=0):
if comm is None: if comm is None:
continue continue