moving config storage to mongo
This commit is contained in:
1 parent
9bc34d4dc9
commit
7d070dec40
2 files changed
+43
-30
No files matched your search
+29
-19
@@ -1,11 +1,16 @@
|
||||
"""Config loading and mode helpers shared across monitors and the bot."""
|
||||
|
||||
import json
|
||||
import logging
|
||||
import pathlib
|
||||
from enum import Enum
|
||||
|
||||
from monitor_utils.constants import LOGGERS
|
||||
from monitor_utils.database import get_collection
|
||||
|
||||
CONFIGS_COLLECTION_NAME = "configs"
|
||||
|
||||
|
||||
class ConfigNotFoundError(Exception):
|
||||
"""Raised when a monitor has no config document in Mongo."""
|
||||
|
||||
|
||||
class DevelopmentMode(str, Enum):
|
||||
@@ -21,29 +26,34 @@ def is_production(development_mode: DevelopmentMode) -> bool:
|
||||
return development_mode == DevelopmentMode.PRODUCTION
|
||||
|
||||
|
||||
def get_config(
|
||||
monitor_name: str,
|
||||
config_dir: str | pathlib.Path = "configs",
|
||||
test_config_dir: str | pathlib.Path = "tests/configs",
|
||||
use_production_path: bool = False,
|
||||
) -> tuple[dict, pathlib.Path]:
|
||||
"""Load a monitor's JSON config from the production or test directory.
|
||||
async def get_config(monitor_name: str, use_production: bool = False) -> dict:
|
||||
"""Load a monitor's config document from Mongo.
|
||||
|
||||
Reads the configs collection in production and configs_testing in
|
||||
development, so a dev run never reads production routing.
|
||||
|
||||
Args:
|
||||
monitor_name: Monitor name with or without a .json suffix.
|
||||
config_dir: Directory holding production configs.
|
||||
test_config_dir: Directory holding test configs.
|
||||
use_production_path: Load from config_dir instead of test_config_dir.
|
||||
monitor_name: Monitor identifier (the config document _id).
|
||||
use_production: Read the production configs collection instead of
|
||||
configs_testing.
|
||||
|
||||
Returns:
|
||||
Tuple of the parsed config dict and the resolved config path.
|
||||
The config document, including its categories and channels.
|
||||
|
||||
Raises:
|
||||
ConfigNotFoundError: When no document exists for the monitor.
|
||||
"""
|
||||
|
||||
monitor_name = monitor_name.replace(".json", "")
|
||||
base = config_dir if use_production_path else test_config_dir
|
||||
config_path = pathlib.Path(base) / f"{monitor_name}.json"
|
||||
with open(config_path) as f:
|
||||
return json.load(f), config_path
|
||||
collection_name = (
|
||||
CONFIGS_COLLECTION_NAME
|
||||
if use_production
|
||||
else f"{CONFIGS_COLLECTION_NAME}_testing"
|
||||
)
|
||||
config = await get_collection(collection_name).find_one({"_id": monitor_name})
|
||||
if config is None:
|
||||
raise ConfigNotFoundError(monitor_name)
|
||||
|
||||
return config
|
||||
|
||||
|
||||
def set_logger_levels(loggers: list[str] = LOGGERS, level: int = logging.WARNING):
|
||||
|
||||
+14
-11
@@ -28,18 +28,20 @@ from monitor_utils.sessions import get_base_session, get_tls_session
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def init_monitor(
|
||||
async def init_monitor(
|
||||
monitor_name: str,
|
||||
development_mode: DevelopmentMode,
|
||||
production_proxy_path: str = "proxies.txt",
|
||||
testing_proxy_path: str = "configs/proxies.txt",
|
||||
logging_level=LOGGING_LEVEL,
|
||||
) -> tuple[list[str], AsyncIOMotorCollection, AioProxies]:
|
||||
) -> tuple[list[str] | dict[str, str], AsyncIOMotorCollection, AioProxies]:
|
||||
"""Run shared monitor startup and return categories, collection, and proxies.
|
||||
|
||||
Configures JSON logging under the monitor's name, applies logger levels,
|
||||
loads the monitor config and Mongo collection, and loads proxies from the
|
||||
production or testing path based on development_mode.
|
||||
production or testing path based on development_mode. Async because the
|
||||
config now lives in Mongo; call it from inside the monitor's event loop
|
||||
(in main), not at module import.
|
||||
|
||||
Args:
|
||||
monitor_name: Monitor identifier used for logging and config lookup.
|
||||
@@ -61,7 +63,7 @@ def init_monitor(
|
||||
output="json",
|
||||
)
|
||||
set_logger_levels()
|
||||
category_names, _, collection = setup_config(
|
||||
category_names, _, collection = await setup_config(
|
||||
monitor_name=monitor_name, development_mode=development_mode
|
||||
)
|
||||
proxy_path = production_proxy_path if use_production else testing_proxy_path
|
||||
@@ -69,17 +71,18 @@ def init_monitor(
|
||||
return category_names, collection, proxies
|
||||
|
||||
|
||||
def setup_config(
|
||||
async def setup_config(
|
||||
monitor_name: str, development_mode: DevelopmentMode
|
||||
) -> tuple[list[str], dict[int], AsyncIOMotorCollection]:
|
||||
) -> tuple[list[str] | dict[str, str], dict[str, str | list[str]], AsyncIOMotorCollection]:
|
||||
"""Load monitor config and resolve the Mongo collection for the mode.
|
||||
|
||||
Testing mode appends a _testing suffix to the collection name so
|
||||
production data is never touched during development.
|
||||
Testing mode reads the configs_testing collection and appends a _testing
|
||||
suffix to the product collection name so production data is never touched
|
||||
during development.
|
||||
|
||||
Args:
|
||||
monitor_name: Monitor identifier used for config and collection lookup.
|
||||
development_mode: Selects the production or testing config path and
|
||||
development_mode: Selects the production or testing config and
|
||||
collection.
|
||||
|
||||
Returns:
|
||||
@@ -87,9 +90,9 @@ def setup_config(
|
||||
"""
|
||||
|
||||
use_production = is_production(development_mode)
|
||||
config, _ = get_config(
|
||||
config = await get_config(
|
||||
monitor_name=monitor_name,
|
||||
use_production_path=use_production,
|
||||
use_production=use_production,
|
||||
)
|
||||
|
||||
category_names = config["categories"]
|
||||
|
||||
Reference in new issue
Block a user