diff --git a/firebase_website/__init__.py b/firebase_website/__init__.py index 14a0c0d..1edc591 100644 --- a/firebase_website/__init__.py +++ b/firebase_website/__init__.py @@ -4,50 +4,28 @@ Requires GOOGLE_APPLICATION_CREDENTIALS env var pointing to a service-account JS Usage from main.py: await bot.load_extension("firebase_website") - -Provides: - get_db() → google.cloud.firestore.AsyncClient (shared, lazy-init) """ ###################################################################### import logging -import os from discord.ext import commands -from google.cloud.firestore import AsyncClient +from .config import get_db # noqa: re‑export for other modules from .sysinfo_sync import SysInfoSync log = logging.getLogger(__name__) -_db: AsyncClient | None = None _sync: SysInfoSync | None = None -def get_db() -> AsyncClient: - """Return the shared Async Firestore client, creating it on first call.""" - global _db - if _db is None: - creds_path = os.getenv("GOOGLE_APPLICATION_CREDENTIALS") - if creds_path is None: - raise RuntimeError( - "GOOGLE_APPLICATION_CREDENTIALS environment variable is not set. " - "Point it to your service-account JSON file." - ) - _db = AsyncClient() - log.info("Firestore Admin SDK initialised (project %s)", _db.project) - return _db - - async def setup(bot: commands.Bot) -> None: - """Start the sysinfo sync loop.""" global _sync - get_db() # early init — fails fast if credentials are missing + get_db() # fail fast if credentials are missing _sync = SysInfoSync() _sync.start() async def teardown(bot: commands.Bot) -> None: - """Stop the sysinfo sync loop.""" global _sync if _sync is not None: _sync.stop() diff --git a/firebase_website/config.py b/firebase_website/config.py index 9a6e8a6..b209e67 100644 --- a/firebase_website/config.py +++ b/firebase_website/config.py @@ -1,7 +1,28 @@ -"""Firebase project config — shared by all website integrations.""" +"""Firebase project config + shared Admin SDK client.""" ###################################################################### +import logging +import os + +from google.cloud.firestore import AsyncClient + +log = logging.getLogger(__name__) PROJECT_ID = "esef-514bf" +SYSINFO_INTERVAL = 10 # seconds between pushes -# How often system stats are pushed (seconds) -SYSINFO_INTERVAL = 10 +_db: AsyncClient | None = None + + +def get_db() -> AsyncClient: + """Return the shared Firestore AsyncClient, creating it on first call.""" + global _db + if _db is None: + creds_path = os.getenv("GOOGLE_APPLICATION_CREDENTIALS") + if creds_path is None: + raise RuntimeError( + "GOOGLE_APPLICATION_CREDENTIALS is not set. " + "Point it to your service-account JSON file." + ) + _db = AsyncClient() + log.info("Firestore Admin SDK initialised (project %s)", _db.project) + return _db diff --git a/firebase_website/sysinfo_sync.py b/firebase_website/sysinfo_sync.py index bc6be5e..207bb93 100644 --- a/firebase_website/sysinfo_sync.py +++ b/firebase_website/sysinfo_sync.py @@ -7,8 +7,7 @@ import time import psutil -from . import get_db -from .config import SYSINFO_INTERVAL +from .config import get_db, SYSINFO_INTERVAL log = logging.getLogger(__name__)