Retry transaction until it works

This commit is contained in:
Joscha 2018-08-03 19:47:11 +00:00
parent 1fee49d0e4
commit 6b1348236d

View file

@ -1,16 +1,23 @@
import asyncio import asyncio
import logging
import sqlite3 import sqlite3
from .utils import * from .utils import *
logger = logging.getLogger(__name__)
__all__ = ["Database", "operation"] __all__ = ["Database", "operation"]
def operation(func): def operation(func):
async def wrapper(self, *args, **kwargs): async def wrapper(self, *args, **kwargs):
async with self as db: async with self as db:
while True:
try:
return await asyncify(func, self, db, *args, **kwargs) return await asyncify(func, self, db, *args, **kwargs)
except sqlite3.OperationalError as e:
logger.warn(f"Operational error encountered: {e}")
await asyncio.sleep(5)
return wrapper return wrapper
class Database: class Database: