Catch errors in subreddit

This commit is contained in:
Zeva Rose 2022-06-14 16:35:05 -06:00
parent 3b04182ea2
commit 7f27fab5fb

View file

@ -119,63 +119,67 @@ async def _stream(sub: Sub, bot: Client, reddit: Reddit) -> None:
await sub.load() await sub.load()
running.append(sub.display_name) running.append(sub.display_name)
logger.debug(f"Streaming subreddit {sub.display_name}") logger.debug(f"Streaming subreddit {sub.display_name}")
async for post in sub.stream.submissions(): try:
if not post: async for post in sub.stream.submissions():
logger.debug(f"Got None for post in {sub.display_name}") if not post:
continue logger.debug(f"Got None for post in {sub.display_name}")
if post.created_utc < now.timestamp():
continue
logger.debug(f"Got new post in {sub.display_name}")
follows = SubredditFollow.find(q(display_name=sub.display_name))
num_follows = 0
async for follow in follows:
num_follows += 1
guild = await bot.fetch_guild(follow.guild)
if not guild:
logger.warning(f"Follow {follow.id}'s guild no longer exists, deleting")
await follow.delete()
num_follows -= 1
continue continue
if post.created_utc < now.timestamp():
channel = await bot.fetch_channel(follow.channel)
if not channel:
logger.warning(f"Follow {follow.id}'s channel no longer exists, deleting")
await follow.delete()
num_follows -= 1
continue continue
logger.debug(f"Got new post in {sub.display_name}")
follows = SubredditFollow.find(q(display_name=sub.display_name))
num_follows = 0
embeds = await post_embeds(sub, post, reddit) async for follow in follows:
timestamp = int(post.created_utc) num_follows += 1
try: guild = await bot.fetch_guild(follow.guild)
await channel.send( if not guild:
f"`r/{sub.display_name}` was posted to at <t:{timestamp}:f>", logger.warning(f"Follow {follow.id}'s guild no longer exists, deleting")
embeds=embeds, await follow.delete()
) num_follows -= 1
count = reddit_count.labels( continue
guild_id=guild.id, guild_name=guild.name, subreddit_name=sub.display_name
)
count.inc()
except DNotFound:
logger.warning(f"Follow {follow.id}'s channel no longer exists, deleting")
await follow.delete()
num_follows -= 1
continue
except Exception:
logger.error(
f"Failed to send message to {channel.id} in {channel.guild.name}", exc_info=True
)
gauge = reddit_gauge.labels(subreddit_name=sub.display_name) channel = await bot.fetch_channel(follow.channel)
gauge.set(num_follows) if not channel:
logger.warning(f"Follow {follow.id}'s channel no longer exists, deleting")
await follow.delete()
num_follows -= 1
continue
if num_follows == 0: embeds = await post_embeds(sub, post, reddit)
s = await Subreddit.find_one(q(display_name=sub.display_name)) timestamp = int(post.created_utc)
if s:
await s.delete() try:
break await channel.send(
f"`r/{sub.display_name}` was posted to at <t:{timestamp}:f>",
embeds=embeds,
)
count = reddit_count.labels(
guild_id=guild.id, guild_name=guild.name, subreddit_name=sub.display_name
)
count.inc()
except DNotFound:
logger.warning(f"Follow {follow.id}'s channel no longer exists, deleting")
await follow.delete()
num_follows -= 1
continue
except Exception:
logger.error(
f"Failed to send message to {channel.id} in {channel.guild.name}",
exc_info=True,
)
gauge = reddit_gauge.labels(subreddit_name=sub.display_name)
gauge.set(num_follows)
if num_follows == 0:
s = await Subreddit.find_one(q(display_name=sub.display_name))
if s:
await s.delete()
break
except Exception:
logger.error(f"Subreddit stream {sub.display_name} failed", exc_info=True)
running.remove(sub.display_name) running.remove(sub.display_name)