]> jfr.im git - erebus.git/commitdiff
Fixed "db has gone away"
authorzonidjan <redacted>
Tue, 4 Mar 2014 13:30:16 +0000 (07:30 -0600)
committerzonidjan <redacted>
Tue, 4 Mar 2014 13:30:16 +0000 (07:30 -0600)
Bot will now try to reconnect and try query again.
If there's a second failure, it will exit.

bot.py
erebus.py

diff --git a/bot.py b/bot.py
index 10be7f6a4908207091535cc1d05f4021c53eddd5..1c8aa02a4b26c8bf68355e24e75859c2f270b4d0 100644 (file)
--- a/bot.py
+++ b/bot.py
@@ -61,8 +61,7 @@ class Bot(object):
                        qt = pieces[3]
                        nick = pieces[4]
                        auth = pieces[5]
-                       if auth != '0':
-                               self.parent.user(nick).authed(auth)
+                       self.parent.user(nick).authed(auth)
 
                elif pieces[1] == "JOIN":
                        nick = pieces[0].split('!')[0][1:]
index fb4239a97306f9c46963733e3aa7068e5bb4921d..d0d41a713a6222b1f17d489ce6ef97750e4aa55d 100644 (file)
--- a/erebus.py
+++ b/erebus.py
@@ -29,8 +29,8 @@ class Erebus(object):
                        return self.auth is not None
 
                def authed(self, auth):
-                       if auth == '0': auth = None
-                       self.auth = auth.lower()
+                       if auth == '0': self.auth = None
+                       else: self.auth = auth.lower()
                        self.checklevel()
 
                def checklevel(self):
@@ -198,6 +198,24 @@ class Erebus(object):
 
 
 
+class MyCursor(MySQLdb.cursors.DictCursor):
+       def execute(self, *args, **kwargs):
+               print "[SQL] [#] MyCursor.execute(self, %s, %s)" % (', '.join([repr(i) for i in args]), ', '.join([str(key)+"="+repr(kwargs[key]) for key in kwargs]))
+               try:
+                       super(self.__class__, self).execute(*args, **kwargs)
+               except MySQLdb.MySQLError as e:
+                       print "[SQL] [!] MySQL error! %r" % (e)
+                       try:
+                               dbsetup()
+                               super(self.__class__, self).execute(*args, **kwargs)
+                       except MySQLdb.MySQLError as e:
+                               print "[SQL] [!!!] DOUBLE MySQL error! %r" % (e)
+                               print "* [!!!] Giving up."
+                               sys.exit()
+
+
+def dbsetup():
+       main.db = MySQLdb.connect(host=cfg.dbhost, user=cfg.dbuser, passwd=cfg.dbpass, db=cfg.dbname, cursorclass=MyCursor)
 
 def setup():
        global cfg, main
@@ -210,7 +228,7 @@ def setup():
                print "Loading %s" % (mod)
                ctlmod.load(main, mod)
 
-       main.db = MySQLdb.connect(host=cfg.dbhost, user=cfg.dbuser, passwd=cfg.dbpass, db=cfg.dbname, cursorclass=MySQLdb.cursors.DictCursor)
+       dbsetup()
        c = main.db.cursor()
        c.execute("SELECT nick, user, bind FROM bots WHERE active = 1")
        rows = c.fetchall()