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
|
|
@ -7,6 +7,7 @@ to the ``news:raw`` Redis Stream.
|
|||
|
||||
import asyncio
|
||||
import logging
|
||||
import signal
|
||||
|
||||
from redis.asyncio import Redis
|
||||
|
||||
|
|
@ -53,9 +54,10 @@ async def _poll_rss(
|
|||
publisher: StreamPublisher,
|
||||
articles_fetched_counter,
|
||||
fetch_errors_counter,
|
||||
shutdown_event: asyncio.Event,
|
||||
) -> None:
|
||||
"""Continuously poll RSS feeds at *interval* seconds."""
|
||||
while True:
|
||||
while not shutdown_event.is_set():
|
||||
try:
|
||||
logger.info("Polling RSS feeds …")
|
||||
articles = await source.fetch()
|
||||
|
|
@ -66,7 +68,11 @@ async def _poll_rss(
|
|||
except Exception:
|
||||
logger.exception("RSS poll cycle failed")
|
||||
fetch_errors_counter.add(1)
|
||||
await asyncio.sleep(interval)
|
||||
try:
|
||||
await asyncio.wait_for(shutdown_event.wait(), timeout=interval)
|
||||
return # Shutdown signaled
|
||||
except asyncio.TimeoutError:
|
||||
pass # Normal timeout — continue polling
|
||||
|
||||
|
||||
async def _poll_reddit(
|
||||
|
|
@ -76,9 +82,10 @@ async def _poll_reddit(
|
|||
publisher: StreamPublisher,
|
||||
articles_fetched_counter,
|
||||
fetch_errors_counter,
|
||||
shutdown_event: asyncio.Event,
|
||||
) -> None:
|
||||
"""Continuously poll Reddit at *interval* seconds."""
|
||||
while True:
|
||||
while not shutdown_event.is_set():
|
||||
try:
|
||||
logger.info("Polling Reddit …")
|
||||
articles = await source.fetch()
|
||||
|
|
@ -89,7 +96,11 @@ async def _poll_reddit(
|
|||
except Exception:
|
||||
logger.exception("Reddit poll cycle failed")
|
||||
fetch_errors_counter.add(1)
|
||||
await asyncio.sleep(interval)
|
||||
try:
|
||||
await asyncio.wait_for(shutdown_event.wait(), timeout=interval)
|
||||
return # Shutdown signaled
|
||||
except asyncio.TimeoutError:
|
||||
pass # Normal timeout — continue polling
|
||||
|
||||
|
||||
async def run() -> None:
|
||||
|
|
@ -124,28 +135,40 @@ async def run() -> None:
|
|||
min_score=config.reddit_min_score,
|
||||
)
|
||||
|
||||
# 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)
|
||||
|
||||
# Run pollers concurrently
|
||||
async with asyncio.TaskGroup() as tg:
|
||||
tg.create_task(
|
||||
_poll_rss(
|
||||
rss_source,
|
||||
config.rss_poll_interval_seconds,
|
||||
redis,
|
||||
publisher,
|
||||
articles_fetched_counter,
|
||||
fetch_errors_counter,
|
||||
try:
|
||||
async with asyncio.TaskGroup() as tg:
|
||||
tg.create_task(
|
||||
_poll_rss(
|
||||
rss_source,
|
||||
config.rss_poll_interval_seconds,
|
||||
redis,
|
||||
publisher,
|
||||
articles_fetched_counter,
|
||||
fetch_errors_counter,
|
||||
shutdown_event,
|
||||
)
|
||||
)
|
||||
)
|
||||
tg.create_task(
|
||||
_poll_reddit(
|
||||
reddit_source,
|
||||
config.reddit_poll_interval_seconds,
|
||||
redis,
|
||||
publisher,
|
||||
articles_fetched_counter,
|
||||
fetch_errors_counter,
|
||||
tg.create_task(
|
||||
_poll_reddit(
|
||||
reddit_source,
|
||||
config.reddit_poll_interval_seconds,
|
||||
redis,
|
||||
publisher,
|
||||
articles_fetched_counter,
|
||||
fetch_errors_counter,
|
||||
shutdown_event,
|
||||
)
|
||||
)
|
||||
)
|
||||
finally:
|
||||
await redis.aclose()
|
||||
logger.info("News fetcher stopped gracefully")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue