mirror of
https://gitlab.com/chicken-riders/RcGcDb.git
synced 2025-02-23 00:54:09 +00:00
Database setup
This commit is contained in:
parent
e327c90544
commit
08710b32b9
|
@ -30,6 +30,7 @@ class Client:
|
|||
A client for interacting with RcGcDw when creating formatters or hooks.
|
||||
"""
|
||||
def __init__(self, hooks, wiki):
|
||||
URLS = src.misc.get_paths(wiki.script_url,)
|
||||
self._formatters = hooks
|
||||
self.__recent_changes: Wiki = wiki
|
||||
self.WIKI_API_PATH: str = src.misc.WIKI_API_PATH
|
||||
|
|
|
@ -12,9 +12,9 @@ from contextlib import asynccontextmanager
|
|||
from src.discord.queue import messagequeue
|
||||
from src.argparser import command_line_args
|
||||
from src.config import settings
|
||||
from src.database import db_connection
|
||||
from src.database import db
|
||||
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_ratelimiter import RateLimiter
|
||||
from src.domain_manager import domains
|
||||
|
@ -33,8 +33,6 @@ all_wikis: dict = {}
|
|||
|
||||
main_tasks: dict = {}
|
||||
|
||||
db = db_connection()
|
||||
|
||||
# First populate the all_wikis list with every wiki
|
||||
# Reasons for this: 1. we require amount of wikis to calculate the cooldown between requests
|
||||
# 2. Easier to code
|
||||
|
@ -242,7 +240,7 @@ async def main_loop():
|
|||
# loop.set_exception_handler(global_exception_handler)
|
||||
try:
|
||||
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["database_updates_shield"] = asyncio.shield(main_tasks["database_updates"])
|
||||
await asyncio.gather(main_tasks["message_sender"], main_tasks["database_updates"])
|
||||
|
|
|
@ -46,3 +46,5 @@ class db_connection:
|
|||
# async with self.connection.acquire() as connection:
|
||||
# async with connection.transaction():
|
||||
# return connection.cursor(string, *arg)
|
||||
|
||||
db = db_connection()
|
|
@ -4,7 +4,7 @@ from urllib.parse import urlparse, urlunparse
|
|||
import logging
|
||||
import asyncpg
|
||||
|
||||
from exceptions import NoDomain
|
||||
from src.exceptions import NoDomain
|
||||
from src.config import settings
|
||||
from src.domain import Domain
|
||||
from src.irc_feed import AioIRCCat
|
||||
|
|
|
@ -2,6 +2,7 @@ import asyncio
|
|||
import collections
|
||||
import logging
|
||||
from typing import Union, Optional
|
||||
from src.database import db
|
||||
|
||||
import asyncpg
|
||||
|
||||
|
@ -11,7 +12,6 @@ logger = logging.getLogger("rcgcdb.queue_handler")
|
|||
class UpdateDB:
|
||||
def __init__(self):
|
||||
self.updated: list[tuple[str, tuple[Union[str, int]]]] = []
|
||||
self.db: Optional[] = None
|
||||
|
||||
def add(self, sql_expression):
|
||||
self.updated.append(sql_expression)
|
||||
|
@ -20,7 +20,7 @@ class UpdateDB:
|
|||
self.updated.clear()
|
||||
|
||||
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 for row in connection.cursor(SQLstatement, *args):
|
||||
yield row
|
||||
|
@ -29,7 +29,7 @@ class UpdateDB:
|
|||
try:
|
||||
while True:
|
||||
if self.updated:
|
||||
async with self.db.pool().acquire() as connection:
|
||||
async with db.pool().acquire() as connection:
|
||||
async with connection.transaction():
|
||||
for update in self.updated:
|
||||
await connection.execute(update[0], *update[1])
|
||||
|
@ -37,12 +37,12 @@ class UpdateDB:
|
|||
await asyncio.sleep(10.0)
|
||||
except asyncio.CancelledError:
|
||||
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():
|
||||
for update in self.updated:
|
||||
await connection.execute(update[0], *update[1])
|
||||
self.clear_list()
|
||||
await self.db.shutdown_connection()
|
||||
await db.shutdown_connection()
|
||||
|
||||
|
||||
dbmanager = UpdateDB()
|
||||
|
|
|
@ -12,7 +12,7 @@ from api.util import default_message
|
|||
from src.discord.queue import messagequeue, QueueEntry
|
||||
from mw_messages import MWMessages
|
||||
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.client import Client
|
||||
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"""
|
||||
Settings = namedtuple("Settings", ["lang", "display"])
|
||||
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"])
|
||||
self.targets = target_settings
|
||||
|
||||
|
@ -281,11 +281,11 @@ class Wiki:
|
|||
if self.rc_id in (0, None, -1):
|
||||
if len(recent_changes) > 0:
|
||||
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)))
|
||||
else:
|
||||
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?
|
||||
categorize_events = {}
|
||||
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
|
||||
# local_wiki.mw_messages = key
|
||||
|
||||
|
||||
async def essential_feeds(change: dict, comment_pages: dict, db_wiki, target: tuple) -> DiscordMessage:
|
||||
"""Prepares essential information for both embed and compact message format."""
|
||||
appearance_mode = feeds_embed_formatter if target[0][1] > 0 else feeds_compact_formatter
|
||||
|
|
Loading…
Reference in a new issue