From 91f926bfe323a272509d33e4eff9eab43f10d345 Mon Sep 17 00:00:00 2001 From: Jeremy Zhang Date: Fri, 22 Sep 2017 01:18:30 +0000 Subject: [PATCH] Delete inactive channel messages and better print stmts --- discordbot/cleanup_db_messages.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/discordbot/cleanup_db_messages.py b/discordbot/cleanup_db_messages.py index f9a28cf..5105ba3 100644 --- a/discordbot/cleanup_db_messages.py +++ b/discordbot/cleanup_db_messages.py @@ -7,6 +7,7 @@ import asyncio import sys import logging import json +import gc from asyncio_extras import threadpool logging.basicConfig(filename='titanbot.log',level=logging.INFO,format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') logging.getLogger('TitanBot') @@ -72,17 +73,25 @@ class Titan(discord.Client): async with threadpool(): with self.database.get_session() as session: guilds = session.query(Guilds).all() + count = 0 for guild in guilds: - print("id-{} snowflake-{} name-{}".format(guild.id, guild.guild_id, guild.name)) + count += 1 + print("[{}] id-{} snowflake-{} name-{}".format(count, guild.id, guild.guild_id, guild.name)) try: channelsjson = json.loads(guild.channels) except: continue + active_channels = [] for channel in channelsjson: chanid = channel["id"] + active_channels.append(chanid) keep_these = session.query(Messages.id).filter(Messages.channel_id == chanid).order_by(Messages.timestamp.desc()).limit(50) - session.query(Messages).filter(Messages.channel_id == chanid, ~Messages.id.in_(keep_these)).delete(synchronize_session=False) + d = session.query(Messages).filter(Messages.channel_id == chanid, ~Messages.id.in_(keep_these)).delete(synchronize_session=False) session.commit() + print(" --{} [{}]".format(channel["name"], d)) + d = session.query(Messages).filter(~Messages.channel_id.in_(active_channels)).delete(synchronize_session=False) + session.commit() + print(" INACTIVE {}".format(d)) print("done!") await self.logout()