ALwrity Backend and Frontend - Stability and Error Handling Improvements
This commit is contained in:
@@ -16,72 +16,98 @@ class DatabaseSetup:
|
||||
|
||||
def setup_essential_tables(self) -> bool:
|
||||
"""Set up essential database tables."""
|
||||
print("📊 Setting up essential database tables...")
|
||||
import os
|
||||
verbose = os.getenv("ALWRITY_VERBOSE", "false").lower() == "true"
|
||||
|
||||
if verbose:
|
||||
print("📊 Setting up essential database tables...")
|
||||
|
||||
try:
|
||||
from services.database import init_database, engine
|
||||
|
||||
# Initialize database connection
|
||||
init_database()
|
||||
print(" ✅ Database connection initialized")
|
||||
if verbose:
|
||||
print(" ✅ Database connection initialized")
|
||||
|
||||
# Create essential tables
|
||||
self._create_monitoring_tables()
|
||||
self._create_subscription_tables()
|
||||
self._create_persona_tables()
|
||||
|
||||
print("✅ Essential database tables created")
|
||||
if verbose:
|
||||
print("✅ Essential database tables created")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"⚠️ Warning: Database setup failed: {e}")
|
||||
if self.production_mode:
|
||||
print(" Continuing in production mode...")
|
||||
return True
|
||||
else:
|
||||
print(" This may affect functionality")
|
||||
return True # Don't fail startup for database issues
|
||||
if verbose:
|
||||
print(f"⚠️ Warning: Database setup failed: {e}")
|
||||
if self.production_mode:
|
||||
print(" Continuing in production mode...")
|
||||
else:
|
||||
print(" This may affect functionality")
|
||||
return True # Don't fail startup for database issues
|
||||
|
||||
def _create_monitoring_tables(self) -> bool:
|
||||
"""Create API monitoring tables."""
|
||||
import os
|
||||
verbose = os.getenv("ALWRITY_VERBOSE", "false").lower() == "true"
|
||||
|
||||
try:
|
||||
from models.api_monitoring import Base as MonitoringBase
|
||||
MonitoringBase.metadata.create_all(bind=engine)
|
||||
print(" ✅ Monitoring tables created")
|
||||
if verbose:
|
||||
print(" ✅ Monitoring tables created")
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f" ⚠️ Monitoring tables failed: {e}")
|
||||
if verbose:
|
||||
print(f" ⚠️ Monitoring tables failed: {e}")
|
||||
return True # Non-critical
|
||||
|
||||
def _create_subscription_tables(self) -> bool:
|
||||
"""Create subscription and billing tables."""
|
||||
import os
|
||||
verbose = os.getenv("ALWRITY_VERBOSE", "false").lower() == "true"
|
||||
|
||||
try:
|
||||
from models.subscription_models import Base as SubscriptionBase
|
||||
SubscriptionBase.metadata.create_all(bind=engine)
|
||||
print(" ✅ Subscription tables created")
|
||||
if verbose:
|
||||
print(" ✅ Subscription tables created")
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f" ⚠️ Subscription tables failed: {e}")
|
||||
if verbose:
|
||||
print(f" ⚠️ Subscription tables failed: {e}")
|
||||
return True # Non-critical
|
||||
|
||||
def _create_persona_tables(self) -> bool:
|
||||
"""Create persona analysis tables."""
|
||||
import os
|
||||
verbose = os.getenv("ALWRITY_VERBOSE", "false").lower() == "true"
|
||||
|
||||
try:
|
||||
from models.persona_models import Base as PersonaBase
|
||||
PersonaBase.metadata.create_all(bind=engine)
|
||||
print(" ✅ Persona tables created")
|
||||
if verbose:
|
||||
print(" ✅ Persona tables created")
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f" ⚠️ Persona tables failed: {e}")
|
||||
if verbose:
|
||||
print(f" ⚠️ Persona tables failed: {e}")
|
||||
return True # Non-critical
|
||||
|
||||
def verify_tables(self) -> bool:
|
||||
"""Verify that essential tables exist."""
|
||||
import os
|
||||
verbose = os.getenv("ALWRITY_VERBOSE", "false").lower() == "true"
|
||||
|
||||
if self.production_mode:
|
||||
print("⚠️ Skipping table verification in production mode")
|
||||
if verbose:
|
||||
print("⚠️ Skipping table verification in production mode")
|
||||
return True
|
||||
|
||||
print("🔍 Verifying database tables...")
|
||||
if verbose:
|
||||
print("🔍 Verifying database tables...")
|
||||
|
||||
try:
|
||||
from services.database import engine
|
||||
@@ -97,11 +123,13 @@ class DatabaseSetup:
|
||||
]
|
||||
|
||||
existing_tables = [table for table in essential_tables if table in tables]
|
||||
print(f" ✅ Found tables: {existing_tables}")
|
||||
if verbose:
|
||||
print(f" ✅ Found tables: {existing_tables}")
|
||||
|
||||
if len(existing_tables) < len(essential_tables):
|
||||
missing = [table for table in essential_tables if table not in existing_tables]
|
||||
print(f" ⚠️ Missing tables: {missing}")
|
||||
if verbose:
|
||||
print(f" ⚠️ Missing tables: {missing}")
|
||||
|
||||
return True
|
||||
|
||||
@@ -124,11 +152,11 @@ class DatabaseSetup:
|
||||
# Set up billing tables
|
||||
self._setup_billing_tables()
|
||||
|
||||
print("✅ Advanced database features configured")
|
||||
logger.debug("✅ Advanced database features configured")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"⚠️ Advanced table setup failed: {e}")
|
||||
logger.warning(f"Advanced table setup failed: {e}")
|
||||
return True # Non-critical
|
||||
|
||||
def _setup_monitoring_tables(self) -> bool:
|
||||
@@ -157,16 +185,16 @@ class DatabaseSetup:
|
||||
|
||||
# Check if tables already exist
|
||||
if check_existing_tables(engine):
|
||||
print(" ✅ Billing tables already exist")
|
||||
logger.debug("✅ Billing tables already exist")
|
||||
return True
|
||||
|
||||
if create_billing_tables():
|
||||
print(" ✅ Billing tables created")
|
||||
logger.debug("✅ Billing tables created")
|
||||
return True
|
||||
else:
|
||||
print(" ⚠️ Billing setup failed")
|
||||
logger.warning("Billing setup failed")
|
||||
return True # Non-critical
|
||||
|
||||
except Exception as e:
|
||||
print(f" ⚠️ Billing setup failed: {e}")
|
||||
logger.warning(f"Billing setup failed: {e}")
|
||||
return True # Non-critical
|
||||
|
||||
@@ -51,40 +51,54 @@ class DependencyManager:
|
||||
|
||||
def check_critical_dependencies(self) -> Tuple[bool, List[str]]:
|
||||
"""Check if critical dependencies are available."""
|
||||
print("🔍 Checking critical dependencies...")
|
||||
import os
|
||||
verbose = os.getenv("ALWRITY_VERBOSE", "false").lower() == "true"
|
||||
|
||||
if verbose:
|
||||
print("🔍 Checking critical dependencies...")
|
||||
|
||||
missing_packages = []
|
||||
|
||||
for package in self.critical_packages:
|
||||
try:
|
||||
__import__(package.replace('-', '_'))
|
||||
print(f" ✅ {package}")
|
||||
if verbose:
|
||||
print(f" ✅ {package}")
|
||||
except ImportError:
|
||||
print(f" ❌ {package} - MISSING")
|
||||
if verbose:
|
||||
print(f" ❌ {package} - MISSING")
|
||||
missing_packages.append(package)
|
||||
|
||||
if missing_packages:
|
||||
print(f"❌ Missing critical packages: {', '.join(missing_packages)}")
|
||||
if verbose:
|
||||
print(f"❌ Missing critical packages: {', '.join(missing_packages)}")
|
||||
return False, missing_packages
|
||||
|
||||
print("✅ All critical dependencies available!")
|
||||
if verbose:
|
||||
print("✅ All critical dependencies available!")
|
||||
return True, []
|
||||
|
||||
def check_optional_dependencies(self) -> Tuple[bool, List[str]]:
|
||||
"""Check if optional dependencies are available."""
|
||||
print("🔍 Checking optional dependencies...")
|
||||
import os
|
||||
verbose = os.getenv("ALWRITY_VERBOSE", "false").lower() == "true"
|
||||
|
||||
if verbose:
|
||||
print("🔍 Checking optional dependencies...")
|
||||
|
||||
missing_packages = []
|
||||
|
||||
for package in self.optional_packages:
|
||||
try:
|
||||
__import__(package.replace('-', '_'))
|
||||
print(f" ✅ {package}")
|
||||
if verbose:
|
||||
print(f" ✅ {package}")
|
||||
except ImportError:
|
||||
print(f" ⚠️ {package} - MISSING (optional)")
|
||||
if verbose:
|
||||
print(f" ⚠️ {package} - MISSING (optional)")
|
||||
missing_packages.append(package)
|
||||
|
||||
if missing_packages:
|
||||
if missing_packages and verbose:
|
||||
print(f"⚠️ Missing optional packages: {', '.join(missing_packages)}")
|
||||
print(" Some features may not be available")
|
||||
|
||||
|
||||
@@ -28,21 +28,29 @@ class EnvironmentSetup:
|
||||
|
||||
def setup_directories(self) -> bool:
|
||||
"""Create necessary directories for ALwrity."""
|
||||
print("📁 Setting up directories...")
|
||||
import os
|
||||
verbose = os.getenv("ALWRITY_VERBOSE", "false").lower() == "true"
|
||||
|
||||
if verbose:
|
||||
print("📁 Setting up directories...")
|
||||
|
||||
if not self.required_directories:
|
||||
print(" ⚠️ Skipping directory creation in production mode")
|
||||
if verbose:
|
||||
print(" ⚠️ Skipping directory creation in production mode")
|
||||
return True
|
||||
|
||||
for directory in self.required_directories:
|
||||
try:
|
||||
Path(directory).mkdir(parents=True, exist_ok=True)
|
||||
print(f" ✅ Created: {directory}")
|
||||
if verbose:
|
||||
print(f" ✅ Created: {directory}")
|
||||
except Exception as e:
|
||||
print(f" ❌ Failed to create {directory}: {e}")
|
||||
if verbose:
|
||||
print(f" ❌ Failed to create {directory}: {e}")
|
||||
return False
|
||||
|
||||
print("✅ All directories created successfully")
|
||||
if verbose:
|
||||
print("✅ All directories created successfully")
|
||||
return True
|
||||
|
||||
def setup_environment_variables(self) -> bool:
|
||||
|
||||
@@ -18,22 +18,31 @@ class RouterManager:
|
||||
|
||||
def include_router_safely(self, router, router_name: str = None) -> bool:
|
||||
"""Include a router safely with error handling."""
|
||||
import os
|
||||
verbose = os.getenv("ALWRITY_VERBOSE", "false").lower() == "true"
|
||||
|
||||
try:
|
||||
self.app.include_router(router)
|
||||
router_name = router_name or getattr(router, 'prefix', 'unknown')
|
||||
self.included_routers.append(router_name)
|
||||
logger.info(f"✅ Router included successfully: {router_name}")
|
||||
if verbose:
|
||||
logger.info(f"✅ Router included successfully: {router_name}")
|
||||
return True
|
||||
except Exception as e:
|
||||
router_name = router_name or 'unknown'
|
||||
self.failed_routers.append({"name": router_name, "error": str(e)})
|
||||
logger.warning(f"❌ Router inclusion failed: {router_name} - {e}")
|
||||
if verbose:
|
||||
logger.warning(f"❌ Router inclusion failed: {router_name} - {e}")
|
||||
return False
|
||||
|
||||
def include_core_routers(self) -> bool:
|
||||
"""Include core application routers."""
|
||||
import os
|
||||
verbose = os.getenv("ALWRITY_VERBOSE", "false").lower() == "true"
|
||||
|
||||
try:
|
||||
logger.info("Including core routers...")
|
||||
if verbose:
|
||||
logger.info("Including core routers...")
|
||||
|
||||
# Component logic router
|
||||
from api.component_logic import router as component_logic_router
|
||||
|
||||
Reference in New Issue
Block a user