feat(alarm): trex-core 알람 emitter + trex.alerts exchange (dcy-033 BAT-049, Implemented)
AlarmEmitter(application, best-effort 비차단)+trex.alerts topic exchange 가산·격리+8 CRITICAL code 배선. 608 passed, opus QC PASS, 팀장 e2e(텔레그램 HTTP200) 통과. REQ-098~101 Implemented. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VHtM5Ws1HKCKQ4JGi6GRb5
This commit is contained in:
+26
-12
@@ -44,6 +44,7 @@ from trex_core.adapters.portfolio import (
|
||||
StubPortfolioRead,
|
||||
)
|
||||
from trex_core.adapters.portfolio.killswitch_store import PostgreSQLKillSwitchStore
|
||||
from trex_core.application.alarm_emitter import AlarmEmitter
|
||||
from trex_core.application.broker_port import BrokerPort
|
||||
from trex_core.application.execution import ExecutionService
|
||||
from trex_core.application.intents import IntentService
|
||||
@@ -169,25 +170,42 @@ class Daemon:
|
||||
# BAT-008: portfolio-hub read + snapshot store + durable pause + scheduler.
|
||||
# (Created before the ledger writer so option-D write-back can target it.)
|
||||
self.snapshot_store = SnapshotStore()
|
||||
# BAT-010/011: per-slot watchdog (snapshot RPC writer, safety-sell reader).
|
||||
self.watchdog = WatchdogStore()
|
||||
self.pause_store = PostgreSQLPauseStore(config.dsn)
|
||||
# BAT-012: day-scoped kill-switch exclusion store + service.
|
||||
self.killswitch_store = PostgreSQLKillSwitchStore(config.dsn)
|
||||
self.portfolio_read = _build_portfolio_read(config)
|
||||
|
||||
# Event publisher transport — one dedicated instance for the
|
||||
# execution-loop thread (pika is not thread-safe).
|
||||
self.event_publisher: MessagingTransport = _build_transport(config)
|
||||
self._transports.append(self.event_publisher)
|
||||
|
||||
# dcy-033 / REQ-100: alarm emitter — shares the event_publisher transport
|
||||
# (both run on the execution-loop thread; pika thread-safety rule satisfied).
|
||||
# Created before services so all services receive the same emitter instance.
|
||||
self.alarm_emitter = AlarmEmitter(self.event_publisher)
|
||||
# Late-bind alarm callback into event_publisher for adapter-layer alarm codes
|
||||
# (KD-1: publish_unavailable_after_reconnect / dlq_publish_failed).
|
||||
if hasattr(self.event_publisher, "set_alarm_callback"):
|
||||
self.event_publisher.set_alarm_callback(self.alarm_emitter.publish_alarm)
|
||||
|
||||
self.ledger_writer = LedgerWriter(
|
||||
self.outbox, _build_ledger(config),
|
||||
snapshot_store=self.snapshot_store, # dcy-018 / REQ-030: option D write-back
|
||||
retry_base_sec=config.ledger_retry_base_sec,
|
||||
retry_cap_sec=config.ledger_retry_cap_sec,
|
||||
max_attempts=config.ledger_max_attempts,
|
||||
alarm_emitter=self.alarm_emitter, # dcy-033 / REQ-100
|
||||
)
|
||||
# BAT-010/011: per-slot watchdog (snapshot RPC writer, safety-sell reader).
|
||||
self.watchdog = WatchdogStore()
|
||||
self.pause_store = PostgreSQLPauseStore(config.dsn)
|
||||
# BAT-012: day-scoped kill-switch exclusion store + service.
|
||||
self.killswitch_store = PostgreSQLKillSwitchStore(config.dsn)
|
||||
self.kill_switch = KillSwitchService(
|
||||
slot_repo=self.slot_repo,
|
||||
slot_service=self.slot_service,
|
||||
killswitch_store=self.killswitch_store,
|
||||
instance_key=config.instance_key,
|
||||
alarm_emitter=self.alarm_emitter, # dcy-033 / REQ-100
|
||||
)
|
||||
self.portfolio_read = _build_portfolio_read(config)
|
||||
self.snapshot_scheduler = SnapshotScheduler(
|
||||
read_port=self.portfolio_read,
|
||||
store=self.snapshot_store,
|
||||
@@ -199,19 +217,15 @@ class Daemon:
|
||||
slot_repo=self.slot_repo,
|
||||
slot_service=self.slot_service,
|
||||
instance_key=config.instance_key,
|
||||
alarm_emitter=self.alarm_emitter, # dcy-033 / REQ-100
|
||||
)
|
||||
|
||||
# Event publisher transport — one dedicated instance for the
|
||||
# execution-loop thread (pika is not thread-safe).
|
||||
self.event_publisher: MessagingTransport = _build_transport(config)
|
||||
self._transports.append(self.event_publisher)
|
||||
|
||||
self.execution = ExecutionService(
|
||||
self.slot_service, self.slot_repo, self.broker,
|
||||
ledger_outbox=self.outbox,
|
||||
event_emitter=self._make_event_emitter(),
|
||||
stale_pending_threshold_sec=config.pending_stale_threshold_sec,
|
||||
instance_key=config.instance_key,
|
||||
alarm_emitter=self.alarm_emitter, # dcy-033 / REQ-100
|
||||
)
|
||||
|
||||
# BAT-011: safety-sell trigger service (forced_close / SL-TP / market-close).
|
||||
|
||||
@@ -29,6 +29,21 @@ from typing import Any, Optional
|
||||
|
||||
_log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _emit_reset_alarm(
|
||||
alarm_emitter: Optional[object],
|
||||
code: str,
|
||||
title: str,
|
||||
fields: dict | None = None,
|
||||
) -> None:
|
||||
"""Best-effort alarm from reset.py (transport may be None — dcy-033 / REQ-100 KD-2)."""
|
||||
if alarm_emitter is None:
|
||||
return
|
||||
try:
|
||||
alarm_emitter.publish_alarm(code, title, fields=fields) # type: ignore[union-attr]
|
||||
except Exception: # noqa: BLE001
|
||||
_log.debug("reset alarm emit failed (code=%s)", code, exc_info=True)
|
||||
|
||||
# slot-core tables owned by trex-core migrations (drop order irrelevant — no FKs).
|
||||
SLOT_CORE_TABLES: tuple[str, ...] = (
|
||||
"daily_slot",
|
||||
@@ -74,6 +89,7 @@ def run_reset(
|
||||
*,
|
||||
dry_run: bool = True,
|
||||
psycopg_module: Any = None,
|
||||
alarm_emitter: Optional[object] = None,
|
||||
) -> ResetReport:
|
||||
"""Wipe the slot-core tables and reapply migrations to the asset-centric schema.
|
||||
|
||||
@@ -106,7 +122,17 @@ def run_reset(
|
||||
# 010 sets the inactive CHECK, 011 drops the pool-model columns.
|
||||
from trex_core.adapters.persistence.migrate import apply_migrations
|
||||
|
||||
report.migrations_applied = apply_migrations(dsn, psycopg_module=psycopg_module)
|
||||
try:
|
||||
report.migrations_applied = apply_migrations(dsn, psycopg_module=psycopg_module)
|
||||
except Exception as mig_exc: # noqa: BLE001
|
||||
# dcy-033 / REQ-100: CRITICAL alarm — migration failed.
|
||||
_emit_reset_alarm(
|
||||
alarm_emitter,
|
||||
"migration_failed",
|
||||
f"마이그레이션 실패: {mig_exc.__class__.__name__}",
|
||||
fields={"error": str(mig_exc)},
|
||||
)
|
||||
raise
|
||||
|
||||
from trex_core.domain.slot import EXPECTED_SLOT_FIELDS
|
||||
|
||||
@@ -118,6 +144,14 @@ def run_reset(
|
||||
)
|
||||
cols = {r[0] for r in cur.fetchall()}
|
||||
report.schema_ok = cols == set(EXPECTED_SLOT_FIELDS)
|
||||
if not report.schema_ok:
|
||||
# dcy-033 / REQ-100: CRITICAL alarm — schema verification failed post-reset.
|
||||
_emit_reset_alarm(
|
||||
alarm_emitter,
|
||||
"schema_verification_failed",
|
||||
"리셋 후 schema 검증 실패: daily_slot 컬럼 불일치",
|
||||
fields={"expected": sorted(EXPECTED_SLOT_FIELDS), "actual": sorted(cols)},
|
||||
)
|
||||
return report
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user