Skip to content

Commit

Permalink
Work on #21.
Browse files Browse the repository at this point in the history
For some reason this seems to catch almost all errors, even methods that
directly use dbpool
  • Loading branch information
d_dd committed Jul 21, 2015
1 parent 7ca4a74 commit 7569c60
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions database.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from twisted.internet import defer
from twisted.internet import defer, reactor, task
from twisted.enterprise import adbapi
from tools import clog
from sqlite3 import OperationalError
sys = 'database'
_USERIDSQL = '(SELECT userId FROM CyUser WHERE nameLower=? AND registered=?)'

Expand All @@ -10,11 +11,33 @@ def turnOnFK(txn):
class NoRowException(Exception):
pass

def operate(sql, binds):
return dbpool.runOperation(sql, binds)
def operate(sql, binds, attempt=0):
d = task.deferLater(reactor, attempt * 1, dbpool.runOperation, sql, binds)
#d = task.deferLater(reactor, attempt * 1, raise_error, sql, binds)
attempt += 1
d.addErrback(retryDatabase, 'operate', sql, binds, attempt)
return d

def query(sql, binds, attempt=0):
d = task.deferLater(reactor, attempt * 1, dbpool.runQuery, sql, binds)
attempt += 1
d.addErrback(retryDatabase, 'query', sql, binds, attempt)
return d

def raise_error(*args, **kwargs):
raise OperationalError('teto')

def retryDatabase(error, operation, sql, binds, attempt):
if attempt >= 5:
clog.error(error.getBriefTraceback(),
'Reached max attempts: %s' % attempt)
return
clog.warning(error.getBriefTraceback(), 'retrying attempt: %s' % attempt)
if operation == 'operate':
return operate(sql, binds, attempt)
elif operation == 'query':
return query(sql, binds, attempt)

def query(sql, binds):
return dbpool.runQuery(sql, binds)

def dbQuery(columns, table, **kwargs):
"""
Expand Down Expand Up @@ -87,7 +110,8 @@ def _bulkLogChat(txn, table, chatList):

def insertChat(*args):
sql = 'INSERT INTO CyChat VALUES(?, ?, ?, ?, ?, ?, ?)'
return dbpool.runOperation(sql, args)
#return dbpool.runOperation(sql, args)
return operate(sql, args)

def bulkLogMedia(playlist):
return dbpool.runInteraction(_bulkLogMedia, playlist)
Expand Down

0 comments on commit 7569c60

Please sign in to comment.