Skip to content

Commit 68c098f

Browse files
committed
refactor: move env var reads to lazy initialization to improve testability
1 parent 17b431f commit 68c098f

2 files changed

Lines changed: 22 additions & 7 deletions

File tree

src/api/services/document/job_queue.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,13 @@ def __init__(self):
6161
self.fallback_queue: Dict[str, Dict[str, Any]] = {}
6262

6363
async def initialize(self) -> None:
64-
"""Initialize Redis connection."""
64+
"""Initialize Redis connection (lazy initialization - only reads env when called)."""
6565
if not REDIS_AVAILABLE:
6666
logger.warning("Redis not available, using in-memory fallback")
6767
return
6868

6969
try:
70+
# Read environment variables only when initialize() is called, not at import time
7071
redis_host = os.getenv("REDIS_HOST", "localhost")
7172
redis_port = int(os.getenv("REDIS_PORT", "6379"))
7273
redis_password = os.getenv("REDIS_PASSWORD")

src/retrieval/structured/sql_retriever.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,26 @@
3636
@dataclass
3737
class DatabaseConfig:
3838
"""Database configuration for warehouse operations."""
39-
host: str = os.getenv("PGHOST", "localhost")
40-
port: int = int(os.getenv("PGPORT", "5435"))
41-
database: str = os.getenv("POSTGRES_DB", "warehouse")
42-
user: str = os.getenv("POSTGRES_USER", "warehouse")
43-
password: str = os.getenv("POSTGRES_PASSWORD", "")
39+
host: str = "localhost"
40+
port: int = 5435
41+
database: str = "warehouse"
42+
user: str = "warehouse"
43+
password: str = ""
4444
min_size: int = 1
4545
max_size: int = 10
46+
47+
@classmethod
48+
def from_env(cls) -> "DatabaseConfig":
49+
"""Create DatabaseConfig from environment variables (lazy initialization)."""
50+
return cls(
51+
host=os.getenv("PGHOST", "localhost"),
52+
port=int(os.getenv("PGPORT", "5435")),
53+
database=os.getenv("POSTGRES_DB", "warehouse"),
54+
user=os.getenv("POSTGRES_USER", "warehouse"),
55+
password=os.getenv("POSTGRES_PASSWORD", ""),
56+
min_size=1,
57+
max_size=10,
58+
)
4659

4760
class SQLRetriever:
4861
"""
@@ -62,7 +75,8 @@ def __new__(cls, config: Optional[DatabaseConfig] = None):
6275

6376
def __init__(self, config: Optional[DatabaseConfig] = None):
6477
if not self._initialized:
65-
self.config = config or DatabaseConfig()
78+
# Lazy initialization: only read env vars when actually creating config
79+
self.config = config or DatabaseConfig.from_env()
6680
self._pool: Optional[asyncpg.Pool] = None
6781
self._initialized = True
6882

0 commit comments

Comments
 (0)