import asyncio import sys import os from sqlalchemy import text # Add backend to path sys.path.append(os.path.join(os.path.dirname(__file__), '..')) from app.core.database import get_async_session from app.services.cache_service import cache_service from app.services.google_calendar import get_google_calendar_service from app.worker import celery_app from app.core.config import settings async def check_db(): print("\nšŸ” Checking Database...") try: async for session in get_async_session(): result = await session.execute(text("SELECT 1")) print(f"āœ… Database connected. Result: {result.scalar()}") # Check pgvector extension try: await session.execute(text("CREATE EXTENSION IF NOT EXISTS vector")) print("āœ… pgvector extension verified.") except Exception as e: print(f"āš ļø pgvector check warning: {e}") break except Exception as e: print(f"āŒ Database connection failed: {e}") async def check_redis(): print("\nšŸ” Checking Redis Cache...") try: await cache_service.set("health_check", "ok", ttl=10) val = await cache_service.get("health_check") if val == "ok": mode = "Redis" if cache_service.use_redis else "Memory Fallback" print(f"āœ… Cache working. Mode: {mode}") else: print("āŒ Cache set/get mismatch.") except Exception as e: print(f"āŒ Cache check failed: {e}") def check_celery(): print("\nšŸ” Checking Celery Config...") try: print(f"āœ… Broker URL: {celery_app.conf.broker_url}") print(f"āœ… Registered Tasks: {list(celery_app.tasks.keys())}") except Exception as e: print(f"āŒ Celery check failed: {e}") def check_google_calendar(): print("\nšŸ” Checking Google Calendar Service...") try: service = get_google_calendar_service() # Mock tenant ID from uuid import uuid4 url = service.get_auth_url(uuid4()) if "accounts.google.com" in url: print("āœ… Auth URL generation working.") else: print("āŒ Auth URL unexpected format.") except Exception as e: print(f"āŒ Google Calendar check failed: {e}") async def main(): print("šŸ„ STARTING SYSTEM HEALTH CHECK") await check_db() await check_redis() check_celery() check_google_calendar() print("\nšŸ Health Check Complete.") if __name__ == "__main__": if sys.platform == 'win32': asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) asyncio.run(main())