fix(intents): isolate alarm context lookup and sanitize alarm failure logs
Co-authored-by: genichin <4282227+genichin@users.noreply.github.com>
This commit is contained in:
co-authored by
genichin
parent
5dd3d57f86
commit
1ca9e60c98
@@ -17,6 +17,7 @@ where no broker connection exists. In that case publish_alarm() logs and skips.
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import sys
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
from trex_core.adapters.messaging.topology import alarm_routing_key
|
||||
@@ -84,15 +85,17 @@ class AlarmEmitter:
|
||||
try:
|
||||
self._transport.publish_alarm_event(routing_key, payload)
|
||||
return True
|
||||
except TransportUnavailableError as exc:
|
||||
except TransportUnavailableError:
|
||||
_log.error(
|
||||
"alarm publish skipped (transport unavailable): code=%s: %s",
|
||||
code, exc,
|
||||
"alarm publish skipped (transport unavailable): code=%s title=%s",
|
||||
code, title,
|
||||
)
|
||||
return False
|
||||
except Exception: # noqa: BLE001 — alarm must never crash the caller
|
||||
exc = sys.exc_info()[1]
|
||||
exc_name = type(exc).__name__ if exc is not None else "Exception"
|
||||
_log.error(
|
||||
"alarm publish failed: code=%s title=%s",
|
||||
code, title, exc_info=True,
|
||||
"alarm publish failed: code=%s title=%s error_type=%s",
|
||||
code, title, exc_name,
|
||||
)
|
||||
return False
|
||||
|
||||
@@ -183,20 +183,11 @@ class IntentService:
|
||||
intent.intent_id, decision.status, decision.slot_id, decision.reason
|
||||
)
|
||||
if decision.status == "rejected" and decision.reason == "reconcile_warned":
|
||||
slot = (
|
||||
self._repo.fetch_slot(intent.slot_id)
|
||||
if intent.slot_id is not None
|
||||
else (
|
||||
self._repo.find_slot_by_portfolio(self._instance_key, intent.portfolio_id)
|
||||
if intent.portfolio_id
|
||||
else None
|
||||
)
|
||||
)
|
||||
self._publish_reconcile_warned_rejection_alarm(
|
||||
intent_id=intent.intent_id,
|
||||
intent_type="buy",
|
||||
strategy_id=intent.strategy_id,
|
||||
slot=slot,
|
||||
slot=self._resolve_buy_alarm_slot(intent),
|
||||
)
|
||||
return decision
|
||||
|
||||
@@ -221,10 +212,28 @@ class IntentService:
|
||||
intent_id=intent.intent_id,
|
||||
intent_type="sell",
|
||||
strategy_id=intent.sell_strategy_id,
|
||||
slot=self._repo.fetch_slot(intent.slot_id),
|
||||
slot=self._resolve_sell_alarm_slot(intent),
|
||||
)
|
||||
return decision
|
||||
|
||||
def _resolve_buy_alarm_slot(self, intent: BuyIntent) -> Any:
|
||||
try:
|
||||
if intent.slot_id is not None:
|
||||
return self._repo.fetch_slot(intent.slot_id)
|
||||
if intent.portfolio_id:
|
||||
return self._repo.find_slot_by_portfolio(
|
||||
self._instance_key, intent.portfolio_id
|
||||
)
|
||||
except Exception: # noqa: BLE001 - optional alarm context lookup
|
||||
return None
|
||||
return None
|
||||
|
||||
def _resolve_sell_alarm_slot(self, intent: SellIntent) -> Any:
|
||||
try:
|
||||
return self._repo.fetch_slot(intent.slot_id)
|
||||
except Exception: # noqa: BLE001 - optional alarm context lookup
|
||||
return None
|
||||
|
||||
def _publish_reconcile_warned_rejection_alarm(
|
||||
self,
|
||||
*,
|
||||
|
||||
Reference in New Issue
Block a user