Spaces:
Sleeping
Sleeping
Commit
·
8f9ef28
1
Parent(s):
9ac46cc
feat: Introduce logging, error handling, and Windows asyncio policy for app startup.
Browse files
app.py
CHANGED
|
@@ -1,17 +1,41 @@
|
|
| 1 |
import os
|
| 2 |
import sys
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
# Ensure the root directory is in the python path
|
| 5 |
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
from src.alert_mcp.db import init_db
|
| 8 |
from src.alert_mcp_server.app import create_demo
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
demo
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
if __name__ == "__main__":
|
| 17 |
-
|
|
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
+
import logging
|
| 4 |
+
import asyncio
|
| 5 |
+
|
| 6 |
+
# Configure logging
|
| 7 |
+
logging.basicConfig(
|
| 8 |
+
level=logging.INFO,
|
| 9 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
| 10 |
+
handlers=[logging.StreamHandler(sys.stdout)]
|
| 11 |
+
)
|
| 12 |
+
logger = logging.getLogger(__name__)
|
| 13 |
|
| 14 |
# Ensure the root directory is in the python path
|
| 15 |
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
| 16 |
|
| 17 |
+
# Windows-specific event loop policy to prevent "Event loop is closed" errors
|
| 18 |
+
if sys.platform == 'win32':
|
| 19 |
+
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
|
| 20 |
+
|
| 21 |
from src.alert_mcp.db import init_db
|
| 22 |
from src.alert_mcp_server.app import create_demo
|
| 23 |
|
| 24 |
+
def main():
|
| 25 |
+
try:
|
| 26 |
+
# Initialize the database
|
| 27 |
+
logger.info("Initializing database...")
|
| 28 |
+
init_db()
|
| 29 |
+
|
| 30 |
+
# Create and launch the demo
|
| 31 |
+
logger.info("Creating Gradio app...")
|
| 32 |
+
demo = create_demo()
|
| 33 |
|
| 34 |
+
logger.info("Launching server...")
|
| 35 |
+
demo.launch(mcp_server=True)
|
| 36 |
+
except Exception as e:
|
| 37 |
+
logger.error(f"Failed to launch app: {e}", exc_info=True)
|
| 38 |
+
sys.exit(1)
|
| 39 |
|
| 40 |
if __name__ == "__main__":
|
| 41 |
+
main()
|