-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
204 lines (168 loc) · 7.86 KB
/
main.py
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env python3
import os
import signal
import sys
import logging
import time
import threading
from typing import Optional, Any
from dotenv import load_dotenv
from src.config.logging_config import setup_logging
from src.config.settings import (
SHUTDOWN_TIMEOUT_ORDER,
SHUTDOWN_TIMEOUT_PRICE,
SHUTDOWN_TIMEOUT_STATE,
MONITOR_LOOP_INTERVAL
)
from src.core.price_manager import PriceManager
from src.core.order_manager import OrderManager
from src.core.state_manager import StateManager
from src.db.operations import initialize_database
class Application:
def __init__(self):
self.price_manager: Optional[PriceManager] = None
self.order_manager: Optional[OrderManager] = None
self.state_manager: Optional[StateManager] = None
self.logger = logging.getLogger(__name__)
self.running = True
self.shutting_down = False # Track shutdown state
def initialize(self):
"""Initialize all components according to design specification."""
try:
# Load environment variables
load_dotenv()
# Setup logging
setup_logging()
self.logger.info("Starting Binance Swing Trading Automation")
# Initialize database
initialize_database()
self.logger.info("Database initialized")
# Initialize components in correct order according to design:
# 1. Price Manager (WebSocket connections)
self.price_manager = PriceManager()
self.logger.info("Price Manager initialized")
# 2. Order Manager (depends on Price Manager)
self.order_manager = OrderManager(
price_manager=self.price_manager,
state_manager=None # Will be set after StateManager initialization
)
self.logger.info("Order Manager initialized")
# 3. State Manager (requires both Price and Order managers)
self.state_manager = StateManager(
price_manager=self.price_manager,
order_manager=self.order_manager
)
# Update Order Manager with State Manager reference
self.order_manager.state_manager = self.state_manager
self.logger.info("State Manager initialized")
# Register callbacks
self.price_manager.register_price_callback(self.order_manager.handle_price_update)
self.price_manager.register_order_callback(self.order_manager.handle_order_update)
self.logger.info("Callbacks registered")
# Start components in order according to design:
# 1. State Manager (load and reconcile state)
self.state_manager.start()
self.logger.info("State Manager started - state loaded and reconciled")
# 2. Price Manager (establish WebSocket connections)
self.price_manager.start()
self.logger.info("Price Manager started - WebSocket connections established")
# 3. Order Manager (begin monitoring)
self.order_manager.start()
self.logger.info("Order Manager started - monitoring orders")
self.logger.info("All components initialized and started")
except Exception as e:
self.logger.error("Failed to initialize application", exc_info=True)
self.shutdown()
sys.exit(1)
def handle_signal(self, signum: int, frame: Any) -> None:
"""Simple, direct signal handler that either initiates shutdown or forces exit."""
self.logger.info("Caught signal %s, initiating shutdown...", signum)
if self.shutting_down:
self.logger.warning("Forced exit requested, calling os._exit(1)", exc_info=True)
# Ensure logs are flushed before forced exit
for handler in logging.getLogger().handlers:
handler.flush()
sys.stdout.flush()
sys.stderr.flush()
os._exit(1)
self.shutdown()
def shutdown(self) -> None:
"""Ordered shutdown of components with proper state tracking."""
try:
self.shutting_down = True
self.running = False
# 1. Stop OrderManager (depends on others)
if self.order_manager:
self.logger.info("Stopping Order Manager...")
self.order_manager.stop()
if not self.order_manager.join(timeout=SHUTDOWN_TIMEOUT_ORDER):
self.logger.error("OrderManager failed to stop in time")
# 2. Stop PriceManager (provides data)
if self.price_manager:
self.logger.info("Stopping Price Manager...")
self.price_manager.stop()
if not self.price_manager.join(timeout=SHUTDOWN_TIMEOUT_PRICE):
self.logger.error("PriceManager failed to stop in time")
# 3. Stop StateManager (handles final state)
if self.state_manager:
self.logger.info("Stopping State Manager...")
# Update final state before stopping
self.state_manager.update_state(
websocket_status="STOPPED",
last_error=None,
reconnection_attempts=0
)
self.state_manager.stop()
if not self.state_manager.join(timeout=SHUTDOWN_TIMEOUT_STATE):
self.logger.error("StateManager failed to stop in time")
# Final state verification
self.verify_final_state()
except Exception as e:
self.logger.error("Error during shutdown", exc_info=True)
raise
finally:
# Ensure all logs are flushed
for handler in logging.getLogger().handlers:
handler.flush()
sys.stdout.flush()
sys.stderr.flush()
sys.exit(0) # Clean exit after resources are freed
def verify_final_state(self):
"""Verify database state and resource cleanup."""
try:
if self.state_manager:
# Verify DB state
assert self.state_manager.db.verify_integrity()
# Check for active threads
active_threads = threading.enumerate()
assert len(active_threads) == 1 # Only main thread
# Verify WebSocket closure
if self.price_manager:
assert all(ws.closed for ws in self.price_manager.websockets)
# Verify DB connections are closed
assert self.state_manager.db.pool.size() == 0
except AssertionError as e:
self.logger.error("State verification failed", exc_info=True)
raise
def run(self) -> None:
"""Run the application."""
try:
# Register signal handler only once
signal.signal(signal.SIGINT, self.handle_signal)
signal.signal(signal.SIGTERM, self.handle_signal)
self.initialize()
self.logger.info("Application initialized, entering main loop")
# Main loop with shorter sleep interval
while self.running:
time.sleep(MONITOR_LOOP_INTERVAL)
self.logger.debug("Main loop ended, shutdown in progress")
except Exception as e:
self.logger.error("Error in main loop", exc_info=True)
finally:
if not self.shutting_down: # Only call shutdown if not already shutting down
self.shutdown()
def main():
app = Application()
app.run()
if __name__ == "__main__":
main()