RcGcDb/src/queue_handler.py

39 lines
961 B
Python
Raw Normal View History

2021-06-30 11:41:58 +00:00
import asyncio
2020-07-11 15:54:08 +00:00
import logging
from src.database import db
2020-07-11 15:54:08 +00:00
logger = logging.getLogger("rcgcdb.queue_handler")
2020-07-28 12:39:32 +00:00
class UpdateDB:
2020-07-11 15:54:08 +00:00
def __init__(self):
self.updated = []
2021-06-30 11:41:58 +00:00
def add(self, sql_expression):
self.updated.append(sql_expression)
2020-07-11 15:54:08 +00:00
def clear_list(self):
self.updated.clear()
2021-03-18 16:00:01 +00:00
async def update_db(self):
2021-06-30 11:41:58 +00:00
try:
while True:
if self.updated:
async with db.pool().acquire() as connection:
async with connection.transaction():
for update in self.updated:
await connection.execute(update)
self.clear_list()
await asyncio.sleep(10.0)
except asyncio.CancelledError:
logger.info("Shutting down after updating DB with {} more entries...".format(len(self.updated)))
async with db.pool().acquire() as connection:
async with connection.transaction():
for update in self.updated:
await connection.execute(update)
self.clear_list()
await db.shutdown_connection()
2020-07-18 12:12:00 +00:00
2020-07-21 12:15:40 +00:00
DBHandler = UpdateDB()