mirror of
https://github.com/TitanEmbeds/Titan.git
synced 2024-11-14 18:11:23 +01:00
Fix database connection, better logging
This commit is contained in:
parent
abf34dd438
commit
3ce4a8fe83
@ -8,6 +8,7 @@ import sys
|
|||||||
import logging
|
import logging
|
||||||
import json
|
import json
|
||||||
import gc
|
import gc
|
||||||
|
import random
|
||||||
from asyncio_extras import threadpool
|
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.basicConfig(filename='titanbot.log',level=logging.INFO,format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
|
||||||
logging.getLogger('TitanBot')
|
logging.getLogger('TitanBot')
|
||||||
@ -27,6 +28,18 @@ class Titan(discord.AutoShardedClient):
|
|||||||
self.http.user_agent += ' TitanEmbeds-Bot'
|
self.http.user_agent += ' TitanEmbeds-Bot'
|
||||||
self.database = DatabaseInterface(self)
|
self.database = DatabaseInterface(self)
|
||||||
self.command = Commands(self, self.database)
|
self.command = Commands(self, self.database)
|
||||||
|
self.logger = logging.getLogger("titan_cleanupdb")
|
||||||
|
self.logger.setLevel(logging.DEBUG)
|
||||||
|
fh = logging.FileHandler("titan_cleanupdb.log")
|
||||||
|
fh.setLevel(logging.DEBUG)
|
||||||
|
session_id = str(random.randrange(100))
|
||||||
|
formatter = logging.Formatter("%(asctime)s - {0} - %(levelname)s - %(message)s".format(session_id))
|
||||||
|
fh.setFormatter(formatter)
|
||||||
|
self.logger.addHandler(fh)
|
||||||
|
consoleHandler = logging.StreamHandler()
|
||||||
|
consoleHandler.setFormatter(formatter)
|
||||||
|
self.logger.addHandler(consoleHandler)
|
||||||
|
self.logger.info("Initialized Database Cleaning Class with session id of " + session_id)
|
||||||
|
|
||||||
def _cleanup(self):
|
def _cleanup(self):
|
||||||
try:
|
try:
|
||||||
@ -65,7 +78,7 @@ class Titan(discord.AutoShardedClient):
|
|||||||
await self.change_presence(status=discord.Status.do_not_disturb, activity=game)
|
await self.change_presence(status=discord.Status.do_not_disturb, activity=game)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await self.database.connect(config["database-uri"])
|
self.database.connect(config["database-uri"])
|
||||||
except Exception:
|
except Exception:
|
||||||
self.logger.error("Unable to connect to specified database!")
|
self.logger.error("Unable to connect to specified database!")
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
@ -73,30 +86,31 @@ class Titan(discord.AutoShardedClient):
|
|||||||
return
|
return
|
||||||
|
|
||||||
print("working on this...")
|
print("working on this...")
|
||||||
async with threadpool():
|
self.loop.run_in_executor(None, self.start_cleanup)
|
||||||
with self.database.get_session() as session:
|
|
||||||
guilds = session.query(Guilds).all()
|
def start_cleanup(self):
|
||||||
count = 0
|
with self.database.get_session() as session:
|
||||||
for guild in guilds:
|
guilds = session.query(Guilds).all()
|
||||||
count += 1
|
count = 0
|
||||||
print("[{}] snowflake-{} name-{}".format(count, guild.guild_id, guild.name))
|
for guild in guilds:
|
||||||
try:
|
count += 1
|
||||||
channelsjson = json.loads(guild.channels)
|
self.logger.info("[{}] snowflake-{} name-{}".format(count, guild.guild_id, guild.name))
|
||||||
except:
|
try:
|
||||||
continue
|
channelsjson = json.loads(guild.channels)
|
||||||
active_channels = []
|
except:
|
||||||
for channel in channelsjson:
|
continue
|
||||||
chanid = channel["id"]
|
active_channels = []
|
||||||
active_channels.append(chanid)
|
for channel in channelsjson:
|
||||||
keep_these = session.query(Messages.message_id).filter(Messages.channel_id == chanid).order_by(Messages.timestamp.desc()).limit(50)
|
chanid = channel["id"]
|
||||||
d = session.query(Messages).filter(Messages.channel_id == chanid, ~Messages.message_id.in_(keep_these)).delete(synchronize_session=False)
|
active_channels.append(chanid)
|
||||||
session.commit()
|
keep_these = session.query(Messages.message_id).filter(Messages.channel_id == chanid).order_by(Messages.timestamp.desc()).limit(50)
|
||||||
print(" --{} [{}]".format(channel["name"], d))
|
d = session.query(Messages).filter(Messages.channel_id == chanid, ~Messages.message_id.in_(keep_these)).delete(synchronize_session=False)
|
||||||
d = session.query(Messages).filter(Messages.guild_id == guild.guild_id, ~Messages.channel_id.in_(active_channels)).delete(synchronize_session=False)
|
|
||||||
session.commit()
|
session.commit()
|
||||||
print(" INACTIVE {}".format(d))
|
self.logger.info(" --{} [{}]".format(channel["name"], d))
|
||||||
print("done!")
|
d = session.query(Messages).filter(Messages.guild_id == guild.guild_id, ~Messages.channel_id.in_(active_channels)).delete(synchronize_session=False)
|
||||||
await self.logout()
|
session.commit()
|
||||||
|
self.logger.info(" INACTIVE {}".format(d))
|
||||||
|
self.logger.info("done!")
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
print("Starting...")
|
print("Starting...")
|
||||||
|
Loading…
Reference in New Issue
Block a user