From c114e77b1cfb12d487c24daeaf06d7f728311e96 Mon Sep 17 00:00:00 2001 From: Celes Renata Date: Thu, 16 Apr 2026 15:05:26 +0000 Subject: [PATCH] fix: limit recommendation poll to 50 per cycle to prevent 85k-rec processing stall, add poll logging --- services/trading/engine.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/services/trading/engine.py b/services/trading/engine.py index 43c3ac4..1a39669 100644 --- a/services/trading/engine.py +++ b/services/trading/engine.py @@ -584,11 +584,21 @@ class TradingEngine: "WHERE action IN ('buy','sell') " "AND mode IN ('paper_eligible','live_eligible') " "AND generated_at > $1 " - "ORDER BY confidence DESC", + "ORDER BY confidence DESC " + "LIMIT 50", self._last_poll_timestamp, ) - self._last_poll_timestamp = datetime.now(tz=timezone.utc) + if rows: + # Advance timestamp to the oldest rec in this batch + # so next poll picks up where we left off + last_gen = max(r["generated_at"] for r in rows) + self._last_poll_timestamp = last_gen recs = [dict(r) for r in rows] + if recs: + logger.info( + "Polled %d recommendations (highest confidence=%.3f)", + len(recs), recs[0].get("confidence", 0), + ) except Exception: logger.debug("Could not poll recommendations — table may not exist yet") continue