diff --git a/backend/scripts/sync_all_zones.py b/backend/scripts/sync_all_zones.py index 5070188..cf506e3 100644 --- a/backend/scripts/sync_all_zones.py +++ b/backend/scripts/sync_all_zones.py @@ -62,16 +62,22 @@ SWITCH_CONFIG = { } } -# Setup logging -logging.basicConfig( - level=logging.INFO, - format='%(asctime)s [%(levelname)s] %(message)s', - handlers=[ - logging.StreamHandler(), - logging.FileHandler(LOG_FILE) if LOG_FILE.parent.exists() else logging.StreamHandler() - ] -) -logger = logging.getLogger(__name__) +# Setup logging (avoid duplicate handlers) +logger = logging.getLogger("zone_sync") +if not logger.handlers: + logger.setLevel(logging.INFO) + formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(message)s') + + # Console handler + console_handler = logging.StreamHandler() + console_handler.setFormatter(formatter) + logger.addHandler(console_handler) + + # File handler (if directory exists) + if LOG_FILE.parent.exists(): + file_handler = logging.FileHandler(LOG_FILE) + file_handler.setFormatter(formatter) + logger.addHandler(file_handler) class ZoneSyncResult: @@ -86,33 +92,10 @@ class ZoneSyncResult: async def get_db_session(): - """Create async database session - ROBUST VERSION for standalone script""" - # Read DATABASE_URL directly from .env (avoids import issues when running standalone) - env_file = Path("/home/user/pounce/backend/.env") - if not env_file.exists(): - env_file = Path(__file__).parent.parent / ".env" + """Create async database session""" + from app.config import settings - db_url = None - if env_file.exists(): - for line in env_file.read_text().splitlines(): - if line.startswith("DATABASE_URL="): - db_url = line.split("=", 1)[1].strip().strip('"').strip("'") - break - - # Default to SQLite if not found - if not db_url: - db_url = "sqlite:///./domainwatch.db" - logger.warning(f"DATABASE_URL not found in .env, using default: {db_url}") - - # Convert to async driver - if "sqlite://" in db_url and "aiosqlite" not in db_url: - db_url = db_url.replace("sqlite://", "sqlite+aiosqlite://") - elif "postgresql://" in db_url and "asyncpg" not in db_url: - db_url = db_url.replace("postgresql://", "postgresql+asyncpg://") - - logger.info(f"Database URL: {db_url[:50]}...") - - engine = create_async_engine(db_url, echo=False) + engine = create_async_engine(settings.database_url.replace("sqlite://", "sqlite+aiosqlite://")) async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False) return async_session()