feat: add portfolio snapshot read layer
This commit is contained in:
@@ -20,6 +20,8 @@ import signal
|
||||
import threading
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from datetime import time as time_cls
|
||||
|
||||
from trex_core.adapters.broker.paper import PaperBroker
|
||||
from trex_core.adapters.ledger import InMemoryLedger, PortfolioHubLedger
|
||||
from trex_core.adapters.messaging import (
|
||||
@@ -33,6 +35,11 @@ from trex_core.adapters.persistence.postgresql import (
|
||||
PostgreSQLLedgerOutbox,
|
||||
PostgreSQLSlotRepository,
|
||||
)
|
||||
from trex_core.adapters.portfolio import (
|
||||
PortfolioHubReadAdapter,
|
||||
PostgreSQLPauseStore,
|
||||
StubPortfolioRead,
|
||||
)
|
||||
from trex_core.application.broker_port import BrokerPort
|
||||
from trex_core.application.execution import ExecutionService
|
||||
from trex_core.application.intents import IntentService
|
||||
@@ -41,9 +48,15 @@ from trex_core.application.ledger_port import LedgerPort
|
||||
from trex_core.application.messaging import MessagingTransport
|
||||
from trex_core.application.messaging_handlers import (
|
||||
make_intent_rpc_handler,
|
||||
make_pause_rpc_handler,
|
||||
make_snapshot_rpc_handler,
|
||||
slot_transition_event_payload,
|
||||
)
|
||||
from trex_core.application.portfolio import (
|
||||
PortfolioHubReadPort,
|
||||
SnapshotStore,
|
||||
)
|
||||
from trex_core.application.snapshot_scheduler import SnapshotScheduler
|
||||
from trex_core.domain.slot import SlotService
|
||||
from trex_core.runtime.config import DaemonConfig
|
||||
|
||||
@@ -86,6 +99,23 @@ def _build_transport(config: DaemonConfig) -> MessagingTransport:
|
||||
return RabbitMQTransport(mq.url, timeout=mq.timeout)
|
||||
|
||||
|
||||
def _build_portfolio_read(config: DaemonConfig) -> PortfolioHubReadPort:
|
||||
"""portfolio-hub HTTP read adapter when configured; otherwise stub (REQ-027)."""
|
||||
pf = config.portfolio_hub
|
||||
if pf is None:
|
||||
return StubPortfolioRead()
|
||||
return PortfolioHubReadAdapter(
|
||||
pf.base_url, token=pf.token or None, timeout=pf.timeout,
|
||||
)
|
||||
|
||||
|
||||
def _parse_kst_time(spec: str) -> time_cls:
|
||||
parts = spec.split(":")
|
||||
h = int(parts[0])
|
||||
m = int(parts[1]) if len(parts) > 1 else 0
|
||||
return time_cls(hour=h, minute=m)
|
||||
|
||||
|
||||
class Daemon:
|
||||
"""Thread-orchestrating daemon (no HTTP)."""
|
||||
|
||||
@@ -104,6 +134,17 @@ class Daemon:
|
||||
self.outbox = PostgreSQLLedgerOutbox(config.dsn)
|
||||
self.ledger_writer = LedgerWriter(self.outbox, _build_ledger(config))
|
||||
|
||||
# BAT-008: portfolio-hub read + snapshot store + durable pause + scheduler.
|
||||
self.snapshot_store = SnapshotStore()
|
||||
self.pause_store = PostgreSQLPauseStore(config.dsn)
|
||||
self.portfolio_read = _build_portfolio_read(config)
|
||||
self.snapshot_scheduler = SnapshotScheduler(
|
||||
read_port=self.portfolio_read,
|
||||
store=self.snapshot_store,
|
||||
broker=self.broker,
|
||||
schedule_kst=_parse_kst_time(config.snapshot_schedule.kst_time),
|
||||
)
|
||||
|
||||
# Event publisher transport — one dedicated instance for the
|
||||
# execution-loop thread (pika is not thread-safe).
|
||||
self.event_publisher: MessagingTransport = _build_transport(config)
|
||||
@@ -160,9 +201,32 @@ class Daemon:
|
||||
transport.declare_topology()
|
||||
except Exception: # noqa: BLE001
|
||||
pass
|
||||
handler = make_snapshot_rpc_handler(self.slot_repo, self.slot_service)
|
||||
handler = make_snapshot_rpc_handler(
|
||||
self.slot_repo, self.slot_service,
|
||||
snapshot_store=self.snapshot_store,
|
||||
pause_store=self.pause_store,
|
||||
default_total_slots=self.config.total_slots,
|
||||
)
|
||||
transport.consume_rpc_forever(topology.SNAPSHOT_REQ_QUEUE, handler, self.stop)
|
||||
|
||||
def _pause_rpc_loop(self) -> None:
|
||||
transport = _build_transport(self.config)
|
||||
self._transports.append(transport)
|
||||
try:
|
||||
transport.declare_topology()
|
||||
except Exception: # noqa: BLE001
|
||||
pass
|
||||
handler = make_pause_rpc_handler(self.pause_store)
|
||||
transport.consume_rpc_forever(topology.PAUSE_REQ_QUEUE, handler, self.stop)
|
||||
|
||||
def _snapshot_scheduler_loop(self) -> None:
|
||||
try:
|
||||
self.snapshot_scheduler.run_forever(
|
||||
self.stop, tick_sec=self.config.snapshot_schedule.tick_sec,
|
||||
)
|
||||
except Exception: # noqa: BLE001
|
||||
_log.exception("snapshot scheduler loop crashed")
|
||||
|
||||
# ------------------------------------------------------------ lifecycle
|
||||
def setup(self) -> None:
|
||||
apply_migrations(self.config.dsn)
|
||||
@@ -181,6 +245,8 @@ class Daemon:
|
||||
("trex-ledger", self._ledger_loop),
|
||||
("trex-intent-rpc", self._intent_rpc_loop),
|
||||
("trex-snapshot-rpc", self._snapshot_rpc_loop),
|
||||
("trex-pause-rpc", self._pause_rpc_loop),
|
||||
("trex-snapshot-scheduler", self._snapshot_scheduler_loop),
|
||||
]
|
||||
for name, target in workers:
|
||||
t = threading.Thread(target=target, name=name, daemon=True)
|
||||
|
||||
Reference in New Issue
Block a user