fix: resolve 13 important issues from code review
I1: Add graceful shutdown (SIGTERM/SIGINT) to all 5 background services I2: Fix Dockerfile healthcheck to use curl on /metrics endpoint I3: Fix StreamConsumer.ensure_group() to only catch BUSYGROUP errors I4: Fix SimulatedBroker to reject orders with insufficient cash/shares I5: Move ORM attribute access inside DB session context in trades routes I6: Add Redis-based rate limiting (10 req/min/IP) on all auth endpoints I8: Prevent backtest background task garbage collection I9: Use Numeric(16,6) instead of Float for financial columns in migration I10: Add index on trades.created_at for time-range queries I11: Bind infrastructure ports to 127.0.0.1 in docker-compose I12: Add migrations init service; all app services depend on it I13: Fix user enumeration in login_begin (return options for non-existent users)
This commit is contained in:
parent
2a56727267
commit
5a6b20c8f1
13 changed files with 355 additions and 188 deletions
|
|
@ -10,6 +10,7 @@ from __future__ import annotations
|
|||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
import signal
|
||||
from datetime import datetime, timezone
|
||||
from uuid import UUID
|
||||
|
||||
|
|
@ -275,24 +276,36 @@ async def run(config: LearningEngineConfig | None = None) -> None:
|
|||
|
||||
logger.info("Consuming from trades:executed")
|
||||
|
||||
# Graceful shutdown on SIGTERM/SIGINT
|
||||
shutdown_event = asyncio.Event()
|
||||
loop = asyncio.get_running_loop()
|
||||
for sig in (signal.SIGTERM, signal.SIGINT):
|
||||
loop.add_signal_handler(sig, shutdown_event.set)
|
||||
|
||||
# --- Consume loop ---
|
||||
async for _msg_id, data in consumer.consume():
|
||||
try:
|
||||
trade = TradeExecution.model_validate(data)
|
||||
try:
|
||||
async for _msg_id, data in consumer.consume():
|
||||
if shutdown_event.is_set():
|
||||
break
|
||||
try:
|
||||
trade = TradeExecution.model_validate(data)
|
||||
|
||||
if trade.status.value != "FILLED":
|
||||
logger.debug("Skipping non-filled trade: %s", trade.status.value)
|
||||
continue
|
||||
if trade.status.value != "FILLED":
|
||||
logger.debug("Skipping non-filled trade: %s", trade.status.value)
|
||||
continue
|
||||
|
||||
adjustments = await process_trade(trade, redis, evaluator, adjuster, counters)
|
||||
if adjustments:
|
||||
logger.info(
|
||||
"Made %d weight adjustment(s) for %s",
|
||||
len(adjustments),
|
||||
trade.ticker,
|
||||
)
|
||||
except Exception:
|
||||
logger.exception("Error processing trade execution: %s", data)
|
||||
adjustments = await process_trade(trade, redis, evaluator, adjuster, counters)
|
||||
if adjustments:
|
||||
logger.info(
|
||||
"Made %d weight adjustment(s) for %s",
|
||||
len(adjustments),
|
||||
trade.ticker,
|
||||
)
|
||||
except Exception:
|
||||
logger.exception("Error processing trade execution: %s", data)
|
||||
finally:
|
||||
await redis.aclose()
|
||||
logger.info("Learning engine stopped gracefully")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue