jkhsjdhjs
e03f5d0a43
This only works on the second database interaction, since psycopg2 only notices that the connection is gone, when a query is executed. So in the common case reconnect works as follows: - some bot method calls a cursor function like .execute(), .fetchone(), etc. - this raises an error if the connection is broken - if following code then requests a new cursor, this will also fail since psycopg2 now knows that the connection is gone - the error is caught in storage.DBConn.cursor(), a new connection will be set up of which a new cursor is yielded If the error happens in connection.commit() or .rollback() instead we can instantly reconnect since these methods are wrapped. So why not wrap the cursor methods as well? Consider the following example: A query is the last thing that was executed on a cursor. The database connection is lost. Now .fetchone() is called on the cursor. We could wrap .fetchone() and reconnect, but we'd have to use a new cursor since cursors are linked to connections. And on this new cursor .fetchone() wouldn't make any sense, since we haven't executed a query on this cursor.
76 lines
1.8 KiB
Python
76 lines
1.8 KiB
Python
# -*- coding: utf-8 -*-
|
|
import os
|
|
import json
|
|
import logging
|
|
from collections import OrderedDict
|
|
|
|
import irc3
|
|
from dotenv import load_dotenv
|
|
|
|
MODULE = __name__
|
|
|
|
|
|
# TODO: ddg
|
|
class Bot(irc3.IrcBot):
|
|
DEV = 'BOT_DEV' in os.environ
|
|
REPO_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
CONFIG_DIR = REPO_DIR if DEV else os.path.dirname(REPO_DIR)
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
self.cfg_file = None
|
|
self.env_file = None
|
|
|
|
def load_config(self):
|
|
with open(self.cfg_file, 'r') as fp:
|
|
return json.load(fp, object_pairs_hook=OrderedDict)
|
|
|
|
def save_config(self, data):
|
|
with open(self.cfg_file, 'w') as fp:
|
|
json.dump(data, fp, indent=2)
|
|
|
|
@classmethod
|
|
def from_json(cls, cfg_file: str = 'config.json', env_file: str = '.env'):
|
|
cfg_file = os.path.join(cls.CONFIG_DIR, cfg_file)
|
|
env_file = os.path.join(cls.CONFIG_DIR, env_file)
|
|
|
|
load_dotenv(env_file)
|
|
|
|
with open(cfg_file, 'r') as fp:
|
|
conf = json.load(fp)
|
|
|
|
if cls.DEV:
|
|
conf['debug'] = True
|
|
|
|
bot = cls.from_config(conf)
|
|
bot.cfg_file = cfg_file
|
|
bot.env_file = env_file
|
|
return bot
|
|
|
|
|
|
@irc3.plugin
|
|
class BasePlugin:
|
|
requires = ['irc3.plugins.command']
|
|
|
|
def __init__(self, bot: Bot):
|
|
self.bot = bot
|
|
self.log = logging.getLogger('irc3.{}'.format(self.__class__.__name__.lower()))
|
|
|
|
|
|
class Plugin(BasePlugin):
|
|
@classmethod
|
|
def reload(cls, old: BasePlugin):
|
|
return cls(old.bot)
|
|
|
|
|
|
from .storage import Storage # noqa
|
|
|
|
|
|
class DatabasePlugin(Plugin):
|
|
requires = Plugin.requires + ['bot.storage']
|
|
|
|
def __init__(self, bot: Bot):
|
|
super().__init__(bot)
|
|
self.db = bot.get_plugin(Storage)
|
|
self.con = self.db.con
|