2021-06-30 11:41:58 +00:00
|
|
|
import asyncio
|
2021-07-03 12:07:47 +00:00
|
|
|
import collections
|
2020-07-11 15:54:08 +00:00
|
|
|
import logging
|
2021-07-03 12:07:47 +00:00
|
|
|
from typing import Union
|
|
|
|
|
|
|
|
import asyncpg
|
|
|
|
|
2021-03-20 12:42:54 +00:00
|
|
|
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):
|
2021-07-03 12:07:47 +00:00
|
|
|
self.updated: list[tuple[str, tuple[Union[str, int]]]] = []
|
2020-07-11 15:54:08 +00:00
|
|
|
|
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-07-03 12:07:47 +00:00
|
|
|
async def fetch_rows(self, SQLstatement: str, args: Union[str, int]) -> collections.AsyncIterable:
|
|
|
|
async with db.pool().acquire() as connection:
|
|
|
|
async with connection.transaction():
|
|
|
|
async for row in connection.cursor(SQLstatement, *args):
|
|
|
|
yield row
|
|
|
|
|
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:
|
2021-07-03 12:07:47 +00:00
|
|
|
await connection.execute(update[0], *update[1])
|
2021-06-30 11:41:58 +00:00
|
|
|
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:
|
2021-07-03 12:07:47 +00:00
|
|
|
await connection.execute(update[0], *update[1])
|
2021-06-30 11:41:58 +00:00
|
|
|
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()
|