Spaces:
Sleeping
Sleeping
Claude Code Claude Code commited on
Commit ·
420f732
1
Parent(s): 8756346
Claude Code: Add defensive .env initialization to brain_minimal.py
Browse files- Add try/except block for .env file initialization at startup
- Fallback to copying .env.example if .env is missing
- Create empty .env as last resort to prevent crash
- Add warning log when fallback mode is active
- Prevents FileNotFoundError on subprocess startup
Co-Authored-By: Claude Code <noreply@anthropic.com>
- brain_minimal.py +39 -0
brain_minimal.py
CHANGED
|
@@ -9,6 +9,7 @@ Features:
|
|
| 9 |
- Graceful shutdown on SIGTERM
|
| 10 |
- Heartbeat file for health monitoring
|
| 11 |
- Persistent polling loop
|
|
|
|
| 12 |
"""
|
| 13 |
import os
|
| 14 |
import sys
|
|
@@ -21,6 +22,40 @@ from pathlib import Path
|
|
| 21 |
from datetime import datetime
|
| 22 |
from threading import Thread, Event
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
# Configure logging
|
| 25 |
logging.basicConfig(
|
| 26 |
level=logging.INFO,
|
|
@@ -29,6 +64,10 @@ logging.basicConfig(
|
|
| 29 |
)
|
| 30 |
logger = logging.getLogger(__name__)
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
# Global shutdown flag
|
| 33 |
_shutdown_event = Event()
|
| 34 |
|
|
|
|
| 9 |
- Graceful shutdown on SIGTERM
|
| 10 |
- Heartbeat file for health monitoring
|
| 11 |
- Persistent polling loop
|
| 12 |
+
- Defensive .env initialization to prevent startup crashes
|
| 13 |
"""
|
| 14 |
import os
|
| 15 |
import sys
|
|
|
|
| 22 |
from datetime import datetime
|
| 23 |
from threading import Thread, Event
|
| 24 |
|
| 25 |
+
# CRITICAL: Defensive .env initialization before any other imports
|
| 26 |
+
# This prevents FileNotFoundError if brain_minimal.py runs before entrypoint.sh
|
| 27 |
+
_fallback_mode = False
|
| 28 |
+
try:
|
| 29 |
+
# Ensure /app is in path for utils import
|
| 30 |
+
if "/app" not in sys.path:
|
| 31 |
+
sys.path.insert(0, "/app")
|
| 32 |
+
|
| 33 |
+
# Try to import and run env_init
|
| 34 |
+
try:
|
| 35 |
+
from utils.env_init import init_env
|
| 36 |
+
init_env()
|
| 37 |
+
except ImportError:
|
| 38 |
+
# If utils.env_init is not available, create .env directly
|
| 39 |
+
env_file = Path("/app/.env")
|
| 40 |
+
if not env_file.exists():
|
| 41 |
+
env_example = Path("/app/.env.example")
|
| 42 |
+
if env_example.exists():
|
| 43 |
+
import shutil
|
| 44 |
+
shutil.copy(env_example, env_file)
|
| 45 |
+
logging.basicConfig(level=logging.INFO)
|
| 46 |
+
logging.warning("[BRAIN_MINIMAL] .env not found - copied from .env.example (fallback mode)")
|
| 47 |
+
_fallback_mode = True
|
| 48 |
+
else:
|
| 49 |
+
env_file.touch()
|
| 50 |
+
logging.basicConfig(level=logging.INFO)
|
| 51 |
+
logging.warning("[BRAIN_MINIMAL] .env.example not found - created empty .env (fallback mode)")
|
| 52 |
+
_fallback_mode = True
|
| 53 |
+
except Exception as e:
|
| 54 |
+
# Don't crash on .env init errors - log warning and continue
|
| 55 |
+
logging.basicConfig(level=logging.INFO)
|
| 56 |
+
logging.warning(f"[BRAIN_MINIMAL] .env initialization failed (using safe defaults): {e}")
|
| 57 |
+
_fallback_mode = True
|
| 58 |
+
|
| 59 |
# Configure logging
|
| 60 |
logging.basicConfig(
|
| 61 |
level=logging.INFO,
|
|
|
|
| 64 |
)
|
| 65 |
logger = logging.getLogger(__name__)
|
| 66 |
|
| 67 |
+
# Log if fallback mode is active
|
| 68 |
+
if _fallback_mode:
|
| 69 |
+
logger.warning("[BRAIN_MINIMAL] FALLBACK MODE ACTIVE - .env handling may be incomplete")
|
| 70 |
+
|
| 71 |
# Global shutdown flag
|
| 72 |
_shutdown_event = Event()
|
| 73 |
|