Database setup

This commit is contained in:
Frisk 2022-09-29 23:10:36 +02:00
parent e327c90544
commit 08710b32b9
No known key found for this signature in database
GPG key ID: 213F7C15068AF8AC
6 changed files with 18 additions and 16 deletions

View file

@ -30,6 +30,7 @@ class Client:
A client for interacting with RcGcDw when creating formatters or hooks. A client for interacting with RcGcDw when creating formatters or hooks.
""" """
def __init__(self, hooks, wiki): def __init__(self, hooks, wiki):
URLS = src.misc.get_paths(wiki.script_url,)
self._formatters = hooks self._formatters = hooks
self.__recent_changes: Wiki = wiki self.__recent_changes: Wiki = wiki
self.WIKI_API_PATH: str = src.misc.WIKI_API_PATH self.WIKI_API_PATH: str = src.misc.WIKI_API_PATH

View file

@ -12,9 +12,9 @@ from contextlib import asynccontextmanager
from src.discord.queue import messagequeue from src.discord.queue import messagequeue
from src.argparser import command_line_args from src.argparser import command_line_args
from src.config import settings from src.config import settings
from src.database import db_connection from src.database import db
from src.exceptions import * from src.exceptions import *
from src.queue_handler import UpdateDB from src.queue_handler import dbmanager
from src.wiki import Wiki, process_cats, essential_feeds from src.wiki import Wiki, process_cats, essential_feeds
from src.wiki_ratelimiter import RateLimiter from src.wiki_ratelimiter import RateLimiter
from src.domain_manager import domains from src.domain_manager import domains
@ -33,8 +33,6 @@ all_wikis: dict = {}
main_tasks: dict = {} main_tasks: dict = {}
db = db_connection()
# First populate the all_wikis list with every wiki # First populate the all_wikis list with every wiki
# Reasons for this: 1. we require amount of wikis to calculate the cooldown between requests # Reasons for this: 1. we require amount of wikis to calculate the cooldown between requests
# 2. Easier to code # 2. Easier to code
@ -242,7 +240,7 @@ async def main_loop():
# loop.set_exception_handler(global_exception_handler) # loop.set_exception_handler(global_exception_handler)
try: try:
main_tasks = {"message_sender": asyncio.create_task(message_sender()), main_tasks = {"message_sender": asyncio.create_task(message_sender()),
"database_updates": asyncio.create_task(DBHandler.update_db())} # "discussion_handler": asyncio.create_task(discussion_handler()), "database_updates": asyncio.create_task(dbmanager.update_db())} # "discussion_handler": asyncio.create_task(discussion_handler()),
main_tasks["msg_queue_shield"] = asyncio.shield(main_tasks["message_sender"]) main_tasks["msg_queue_shield"] = asyncio.shield(main_tasks["message_sender"])
main_tasks["database_updates_shield"] = asyncio.shield(main_tasks["database_updates"]) main_tasks["database_updates_shield"] = asyncio.shield(main_tasks["database_updates"])
await asyncio.gather(main_tasks["message_sender"], main_tasks["database_updates"]) await asyncio.gather(main_tasks["message_sender"], main_tasks["database_updates"])

View file

@ -46,3 +46,5 @@ class db_connection:
# async with self.connection.acquire() as connection: # async with self.connection.acquire() as connection:
# async with connection.transaction(): # async with connection.transaction():
# return connection.cursor(string, *arg) # return connection.cursor(string, *arg)
db = db_connection()

View file

@ -4,7 +4,7 @@ from urllib.parse import urlparse, urlunparse
import logging import logging
import asyncpg import asyncpg
from exceptions import NoDomain from src.exceptions import NoDomain
from src.config import settings from src.config import settings
from src.domain import Domain from src.domain import Domain
from src.irc_feed import AioIRCCat from src.irc_feed import AioIRCCat

View file

@ -2,6 +2,7 @@ import asyncio
import collections import collections
import logging import logging
from typing import Union, Optional from typing import Union, Optional
from src.database import db
import asyncpg import asyncpg
@ -11,7 +12,6 @@ logger = logging.getLogger("rcgcdb.queue_handler")
class UpdateDB: class UpdateDB:
def __init__(self): def __init__(self):
self.updated: list[tuple[str, tuple[Union[str, int]]]] = [] self.updated: list[tuple[str, tuple[Union[str, int]]]] = []
self.db: Optional[] = None
def add(self, sql_expression): def add(self, sql_expression):
self.updated.append(sql_expression) self.updated.append(sql_expression)
@ -20,7 +20,7 @@ class UpdateDB:
self.updated.clear() self.updated.clear()
async def fetch_rows(self, SQLstatement: str, args: Union[str, int]) -> collections.AsyncIterable: async def fetch_rows(self, SQLstatement: str, args: Union[str, int]) -> collections.AsyncIterable:
async with self.db.pool().acquire() as connection: async with db.pool().acquire() as connection:
async with connection.transaction(): async with connection.transaction():
async for row in connection.cursor(SQLstatement, *args): async for row in connection.cursor(SQLstatement, *args):
yield row yield row
@ -29,7 +29,7 @@ class UpdateDB:
try: try:
while True: while True:
if self.updated: if self.updated:
async with self.db.pool().acquire() as connection: async with db.pool().acquire() as connection:
async with connection.transaction(): async with connection.transaction():
for update in self.updated: for update in self.updated:
await connection.execute(update[0], *update[1]) await connection.execute(update[0], *update[1])
@ -37,12 +37,12 @@ class UpdateDB:
await asyncio.sleep(10.0) await asyncio.sleep(10.0)
except asyncio.CancelledError: except asyncio.CancelledError:
logger.info("Shutting down after updating DB with {} more entries...".format(len(self.updated))) logger.info("Shutting down after updating DB with {} more entries...".format(len(self.updated)))
async with self.db.pool().acquire() as connection: async with db.pool().acquire() as connection:
async with connection.transaction(): async with connection.transaction():
for update in self.updated: for update in self.updated:
await connection.execute(update[0], *update[1]) await connection.execute(update[0], *update[1])
self.clear_list() self.clear_list()
await self.db.shutdown_connection() await db.shutdown_connection()
dbmanager = UpdateDB() dbmanager = UpdateDB()

View file

@ -12,7 +12,7 @@ from api.util import default_message
from src.discord.queue import messagequeue, QueueEntry from src.discord.queue import messagequeue, QueueEntry
from mw_messages import MWMessages from mw_messages import MWMessages
from src.exceptions import * from src.exceptions import *
from src.queue_handler import UpdateDB from src.queue_handler import dbmanager
from src.api.hooks import formatter_hooks from src.api.hooks import formatter_hooks
from src.api.client import Client from src.api.client import Client
from src.api.context import Context from src.api.context import Context
@ -150,7 +150,7 @@ class Wiki:
:returns defaultdict[namedtuple, list[str]] - where namedtuple is a named tuple with settings for given webhooks in list""" :returns defaultdict[namedtuple, list[str]] - where namedtuple is a named tuple with settings for given webhooks in list"""
Settings = namedtuple("Settings", ["lang", "display"]) Settings = namedtuple("Settings", ["lang", "display"])
target_settings: defaultdict[Settings, list[str]] = defaultdict(list) target_settings: defaultdict[Settings, list[str]] = defaultdict(list)
async for webhook in DBHandler.fetch_rows("SELECT webhook, lang, display FROM rcgcdw WHERE wiki = $1 AND (rcid != -1 OR rcid IS NULL)", self.script_url): async for webhook in dbmanager.fetch_rows("SELECT webhook, lang, display FROM rcgcdw WHERE wiki = $1 AND (rcid != -1 OR rcid IS NULL)", self.script_url):
target_settings[Settings(webhook["lang"], webhook["display"])].append(webhook["webhook"]) target_settings[Settings(webhook["lang"], webhook["display"])].append(webhook["webhook"])
self.targets = target_settings self.targets = target_settings
@ -281,11 +281,11 @@ class Wiki:
if self.rc_id in (0, None, -1): if self.rc_id in (0, None, -1):
if len(recent_changes) > 0: if len(recent_changes) > 0:
self.statistics.last_action = recent_changes[-1]["rcid"] self.statistics.last_action = recent_changes[-1]["rcid"]
DBHandler.add(("UPDATE rcgcdw SET rcid = $1 WHERE wiki = $2 AND ( rcid != -1 OR rcid IS NULL )", dbmanager.add(("UPDATE rcgcdw SET rcid = $1 WHERE wiki = $2 AND ( rcid != -1 OR rcid IS NULL )",
(recent_changes[-1]["rcid"], self.script_url))) (recent_changes[-1]["rcid"], self.script_url)))
else: else:
self.statistics.last_action = 0 self.statistics.last_action = 0
DBHandler.add(("UPDATE rcgcdw SET rcid = 0 WHERE wiki = $1 AND ( rcid != -1 OR rcid IS NULL )", (self.script_url))) dbmanager.add(("UPDATE rcgcdw SET rcid = 0 WHERE wiki = $1 AND ( rcid != -1 OR rcid IS NULL )", (self.script_url)))
return # TODO Add a log entry? return # TODO Add a log entry?
categorize_events = {} categorize_events = {}
new_events = 0 new_events = 0
@ -474,6 +474,7 @@ async def process_cats(event: dict, local_wiki: Wiki, categorize_events: dict):
# mw_msgs[key] = msgs # it may be a little bit messy for sure, however I don't expect any reason to remove mw_msgs entries by one # mw_msgs[key] = msgs # it may be a little bit messy for sure, however I don't expect any reason to remove mw_msgs entries by one
# local_wiki.mw_messages = key # local_wiki.mw_messages = key
async def essential_feeds(change: dict, comment_pages: dict, db_wiki, target: tuple) -> DiscordMessage: async def essential_feeds(change: dict, comment_pages: dict, db_wiki, target: tuple) -> DiscordMessage:
"""Prepares essential information for both embed and compact message format.""" """Prepares essential information for both embed and compact message format."""
appearance_mode = feeds_embed_formatter if target[0][1] > 0 else feeds_compact_formatter appearance_mode = feeds_embed_formatter if target[0][1] > 0 else feeds_compact_formatter