-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
172 lines (133 loc) · 5.02 KB
/
main.py
File metadata and controls
172 lines (133 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
"""
NASDAQ Stock Agent - Main Application Entry Point
"""
import asyncio
import logging
import uvicorn
from typing import Optional
from src.api.app import create_app
from src.core.config_manager import config_manager
from src.core.dependencies import service_container
from src.nest.config import NESTConfig
from src.nest.adapter import StockAgentNEST
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# Global NEST adapter instance
_nest_adapter: Optional[StockAgentNEST] = None
def main():
"""Main application entry point"""
try:
# Load configuration
config = config_manager.load_configuration()
app_config = config.get("application", {})
# Create FastAPI application
app = create_app()
logger.info(f"Starting {app_config.get('name', 'NASDAQ Stock Agent')} v{app_config.get('version', '1.0.0')}")
return app
except Exception as e:
logger.error(f"Failed to start application: {e}")
raise
async def startup_check():
"""Perform startup health check"""
try:
logger.info("Performing startup health check...")
# Initialize services
await service_container.initialize()
# Get system status
status = await service_container.get_system_status()
if status.get('status') in ['healthy', 'degraded']:
logger.info("Startup health check passed")
return True
else:
logger.error(f"Startup health check failed: {status}")
return False
except Exception as e:
logger.error(f"Startup health check error: {e}")
return False
async def initialize_nest():
"""
Initialize NEST integration if enabled.
Returns:
Optional[StockAgentNEST]: NEST adapter instance if enabled, None otherwise
"""
global _nest_adapter
try:
# Load NEST configuration
nest_config = NESTConfig.from_env()
# Check if NEST should be enabled
if not nest_config.should_enable_nest():
logger.info("NEST integration is disabled - running in standalone mode")
return None
logger.info("NEST integration is enabled - initializing NEST adapter")
# Validate configuration
is_valid, errors = nest_config.validate()
if not is_valid:
logger.error(f"NEST configuration validation failed: {', '.join(errors)}")
logger.warning("Falling back to standalone mode")
return None
# Create NEST adapter
_nest_adapter = StockAgentNEST(config=nest_config)
# Start NEST adapter
await _nest_adapter.start_async(register=True)
logger.info(
f"NEST adapter started successfully "
f"(agent_id: {nest_config.agent_id}, port: {nest_config.nest_port})"
)
return _nest_adapter
except ImportError as e:
logger.warning(
f"NEST integration requires python-a2a package: {e}. "
"Running in standalone mode. Install with: pip install python-a2a"
)
return None
except Exception as e:
logger.error(f"Failed to initialize NEST integration: {e}", exc_info=True)
logger.warning("Continuing in standalone mode")
return None
async def shutdown_nest():
"""Shutdown NEST integration if running."""
global _nest_adapter
if _nest_adapter and _nest_adapter.is_running():
try:
logger.info("Shutting down NEST adapter...")
await _nest_adapter.stop_async()
logger.info("NEST adapter stopped successfully")
except Exception as e:
logger.error(f"Error shutting down NEST adapter: {e}", exc_info=True)
finally:
_nest_adapter = None
def get_nest_adapter() -> Optional[StockAgentNEST]:
"""
Get the global NEST adapter instance.
Returns:
Optional[StockAgentNEST]: NEST adapter if initialized, None otherwise
"""
return _nest_adapter
if __name__ == "__main__":
try:
# Load configuration
config = config_manager.load_configuration()
app_config = config.get("application", {})
# Get server configuration
host = app_config.get("host", "0.0.0.0")
port = app_config.get("port", 8000)
debug = app_config.get("debug", False)
logger.info(f"Starting server on {host}:{port} (debug={debug})")
# Run server
uvicorn.run(
"main:main",
host=host,
port=port,
reload=debug,
factory=True,
log_level="info" if not debug else "debug"
)
except KeyboardInterrupt:
logger.info("Application stopped by user")
except Exception as e:
logger.error(f"Application startup failed: {e}")
exit(1)