feat: add portfolio snapshot read layer
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
"""trex-core CLI entrypoints (REQ-032).
|
||||
|
||||
Subcommands:
|
||||
pause <portfolio_id> — durably pause a portfolio (Postgres)
|
||||
resume <portfolio_id> — clear the pause
|
||||
paused — list paused portfolio_ids
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
|
||||
from trex_core.adapters.portfolio.pause_store import PostgreSQLPauseStore
|
||||
from trex_core.runtime.config import DaemonConfig
|
||||
|
||||
|
||||
def main(argv: list[str] | None = None) -> int:
|
||||
parser = argparse.ArgumentParser(prog="trex-core", description="trex-core admin CLI")
|
||||
sub = parser.add_subparsers(dest="cmd", required=True)
|
||||
|
||||
p_pause = sub.add_parser("pause", help="Pause a portfolio_id")
|
||||
p_pause.add_argument("portfolio_id")
|
||||
p_pause.add_argument("--actor", default="cli")
|
||||
|
||||
p_resume = sub.add_parser("resume", help="Resume a portfolio_id")
|
||||
p_resume.add_argument("portfolio_id")
|
||||
|
||||
sub.add_parser("paused", help="List paused portfolio_ids")
|
||||
|
||||
args = parser.parse_args(argv)
|
||||
cfg = DaemonConfig.from_env()
|
||||
store = PostgreSQLPauseStore(cfg.dsn)
|
||||
|
||||
if args.cmd == "pause":
|
||||
store.pause(args.portfolio_id, actor=args.actor)
|
||||
print(f"paused {args.portfolio_id}")
|
||||
elif args.cmd == "resume":
|
||||
store.resume(args.portfolio_id)
|
||||
print(f"resumed {args.portfolio_id}")
|
||||
elif args.cmd == "paused":
|
||||
for pid in sorted(store.list_paused()):
|
||||
print(pid)
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__": # pragma: no cover
|
||||
sys.exit(main())
|
||||
@@ -48,6 +48,14 @@ class RabbitMQConfig:
|
||||
timeout: float = 5.0
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class SnapshotScheduleConfig:
|
||||
"""Daily snapshot refresh schedule (REQ-030)."""
|
||||
|
||||
kst_time: str = "07:30"
|
||||
tick_sec: float = 60.0
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class DaemonConfig:
|
||||
dsn: str
|
||||
@@ -62,6 +70,8 @@ class DaemonConfig:
|
||||
# REQ-022 (BAT-007): RabbitMQ transport config. Required by the new
|
||||
# sync+thread daemon — HTTP API has been removed (REQ-025).
|
||||
rabbitmq: Optional[RabbitMQConfig] = None
|
||||
# REQ-030 (BAT-008): daily snapshot refresh.
|
||||
snapshot_schedule: SnapshotScheduleConfig = SnapshotScheduleConfig()
|
||||
|
||||
@staticmethod
|
||||
def from_env() -> "DaemonConfig":
|
||||
@@ -98,6 +108,10 @@ class DaemonConfig:
|
||||
url=mq_url,
|
||||
timeout=float(os.environ.get("TREX_RABBITMQ_TIMEOUT", "5")),
|
||||
)
|
||||
snapshot_schedule = SnapshotScheduleConfig(
|
||||
kst_time=os.environ.get("TREX_SNAPSHOT_SCHEDULE_KST", "07:30").strip() or "07:30",
|
||||
tick_sec=float(os.environ.get("TREX_SNAPSHOT_TICK_SEC", "60")),
|
||||
)
|
||||
return DaemonConfig(
|
||||
dsn=dsn,
|
||||
total_slots=int(os.environ.get("TREX_TOTAL_SLOTS", "10")),
|
||||
@@ -107,4 +121,5 @@ class DaemonConfig:
|
||||
portfolio_hub=portfolio_hub,
|
||||
ledger_interval_sec=float(os.environ.get("TREX_LEDGER_INTERVAL_SEC", "1.0")),
|
||||
rabbitmq=rabbitmq,
|
||||
snapshot_schedule=snapshot_schedule,
|
||||
)
|
||||
|
||||
@@ -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