Integrate Redis (10.30.20.70) for locks, cache, and decision state to maximize infra utilization

This commit is contained in:
2026-03-01 10:43:01 -08:00
parent cd8f8430f9
commit 1ca35733cf
5 changed files with 72 additions and 1 deletions

26
bot.py
View File

@@ -18,6 +18,9 @@ from services import (
qdrant_add_memory,
trilium_log,
n8n_emit,
redis_set_json,
redis_get_json,
redis_lock,
)
scheduler = BackgroundScheduler(timezone=settings.timezone)
@@ -42,6 +45,9 @@ def curate_cycle():
def run_cycle():
if not redis_lock('alpaca:run_cycle:lock', ttl=240):
return
db = SessionLocal()
try:
acct = account_snapshot()
@@ -64,7 +70,12 @@ def run_cycle():
if spent >= settings.max_daily_notional:
break
news = searx_news(symbol)
cached_news = redis_get_json(f'alpaca:news:{symbol}')
news = cached_news if cached_news else searx_news(symbol)
if news:
redis_set_json(f'alpaca:news:{symbol}', {'items': news} if isinstance(news, list) else news, ttl=900)
if isinstance(news, dict) and 'items' in news:
news = news['items']
strat = strategy_signals(symbol, news)
memory_hits = qdrant_similar(symbol, json.dumps(news)[:2000], limit=5)
decision = llm_final_decision(symbol, news, strat, memory_hits)
@@ -82,6 +93,19 @@ def run_cycle():
decision["order_usd"] = max(decision.get("order_usd", 0.0), settings.force_paper_min_usd)
decision["reason"] = f"{decision.get('reason','')} | force_paper_trades"
redis_set_json(
f"alpaca:last_decision:{symbol}",
{
"symbol": symbol,
"action": decision["action"],
"confidence": decision["confidence"],
"order_usd": decision["order_usd"],
"reason": decision["reason"],
"ts": datetime.utcnow().isoformat(),
},
ttl=86400,
)
drow = BotDecision(
symbol=symbol,
action=decision["action"],