feat: add messaging transport rollout
This commit is contained in:
@@ -40,12 +40,18 @@ class PortfolioHubConfig:
|
||||
timeout: float = 5.0
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class RabbitMQConfig:
|
||||
"""RabbitMQ transport adapter config (REQ-022, dcy-006 OQ-6)."""
|
||||
|
||||
url: str
|
||||
timeout: float = 5.0
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class DaemonConfig:
|
||||
dsn: str
|
||||
total_slots: int
|
||||
host: str
|
||||
port: int
|
||||
broker: str # 'paper' | 'kis'
|
||||
execution_interval_sec: float
|
||||
kis: Optional[KISConfig] = None
|
||||
@@ -53,6 +59,9 @@ class DaemonConfig:
|
||||
# appropriate for slice-1 paper-broker integration tests).
|
||||
portfolio_hub: Optional[PortfolioHubConfig] = None
|
||||
ledger_interval_sec: float = 1.0
|
||||
# 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
|
||||
|
||||
@staticmethod
|
||||
def from_env() -> "DaemonConfig":
|
||||
@@ -82,14 +91,20 @@ class DaemonConfig:
|
||||
token=os.environ.get("TREX_PORTFOLIO_HUB_TOKEN", "").strip(),
|
||||
timeout=float(os.environ.get("TREX_PORTFOLIO_HUB_TIMEOUT", "5")),
|
||||
)
|
||||
rabbitmq: Optional[RabbitMQConfig] = None
|
||||
mq_url = os.environ.get("TREX_RABBITMQ_URL", "").strip()
|
||||
if mq_url:
|
||||
rabbitmq = RabbitMQConfig(
|
||||
url=mq_url,
|
||||
timeout=float(os.environ.get("TREX_RABBITMQ_TIMEOUT", "5")),
|
||||
)
|
||||
return DaemonConfig(
|
||||
dsn=dsn,
|
||||
total_slots=int(os.environ.get("TREX_TOTAL_SLOTS", "10")),
|
||||
host=os.environ.get("TREX_API_HOST", "127.0.0.1"),
|
||||
port=int(os.environ.get("TREX_API_PORT", "8080")),
|
||||
broker=broker,
|
||||
execution_interval_sec=float(os.environ.get("TREX_EXECUTION_INTERVAL_SEC", "1.0")),
|
||||
kis=kis,
|
||||
portfolio_hub=portfolio_hub,
|
||||
ledger_interval_sec=float(os.environ.get("TREX_LEDGER_INTERVAL_SEC", "1.0")),
|
||||
rabbitmq=rabbitmq,
|
||||
)
|
||||
|
||||
+155
-59
@@ -1,22 +1,32 @@
|
||||
"""Slot-core daemon entrypoint (REQ-005, REQ-012/013/014).
|
||||
"""Slot-core daemon entrypoint (REQ-005, REQ-012/013/014, BAT-007).
|
||||
|
||||
Builds the FastAPI app wired to PostgreSQL adapters + a broker (paper|KIS),
|
||||
applies migrations and preallocates slots on startup, runs a background
|
||||
execution loop (claim -> submit -> poll -> transition, with restart
|
||||
reconciliation), and serves the /v1 API via uvicorn.
|
||||
Sync + threaded runtime. HTTP `/v1` API has been removed (REQ-025); all
|
||||
strategy-facing communication runs over RabbitMQ (REQ-021/022/023).
|
||||
|
||||
Threads (started in `run()`):
|
||||
- intent RPC consumer — `trex.intent.req`, prefetch=1, FIFO, dedup via intent_log
|
||||
- snapshot RPC consumer — `trex.snapshot.req`
|
||||
- execution loop — broker dispatch + poll + slot transitions + event emit
|
||||
- ledger writer — ledger outbox drain (REQ-019)
|
||||
|
||||
Each consumer/publisher uses its OWN RabbitMQTransport instance because
|
||||
pika BlockingConnection is not thread-safe across channels.
|
||||
Console script: `trex-core-daemon`.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import signal
|
||||
import threading
|
||||
from contextlib import asynccontextmanager
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from fastapi import FastAPI
|
||||
|
||||
from trex_core.adapters.api.app import create_app
|
||||
from trex_core.adapters.broker.paper import PaperBroker
|
||||
from trex_core.adapters.ledger import InMemoryLedger, PortfolioHubLedger
|
||||
from trex_core.adapters.messaging import (
|
||||
InMemoryTransport,
|
||||
RabbitMQTransport,
|
||||
topology,
|
||||
)
|
||||
from trex_core.adapters.persistence.migrate import apply_migrations
|
||||
from trex_core.adapters.persistence.postgresql import (
|
||||
PostgreSQLIntentStore,
|
||||
@@ -28,9 +38,17 @@ from trex_core.application.execution import ExecutionService
|
||||
from trex_core.application.intents import IntentService
|
||||
from trex_core.application.ledger_outbox import LedgerWriter
|
||||
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_snapshot_rpc_handler,
|
||||
slot_transition_event_payload,
|
||||
)
|
||||
from trex_core.domain.slot import SlotService
|
||||
from trex_core.runtime.config import DaemonConfig
|
||||
|
||||
_log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _build_broker(config: DaemonConfig) -> BrokerPort:
|
||||
if config.broker == "kis":
|
||||
@@ -58,67 +76,145 @@ def _build_ledger(config: DaemonConfig) -> LedgerPort:
|
||||
return PortfolioHubLedger(pf.base_url, token=pf.token or None, timeout=pf.timeout)
|
||||
|
||||
|
||||
def build_app(config: DaemonConfig) -> FastAPI:
|
||||
slot_repo = PostgreSQLSlotRepository(config.dsn)
|
||||
intent_store = PostgreSQLIntentStore(config.dsn)
|
||||
slot_service = SlotService(slot_repo)
|
||||
intent_service = IntentService(slot_service, slot_repo, intent_store)
|
||||
broker = _build_broker(config)
|
||||
outbox = PostgreSQLLedgerOutbox(config.dsn)
|
||||
execution = ExecutionService(slot_service, slot_repo, broker, ledger_outbox=outbox)
|
||||
ledger_writer = LedgerWriter(outbox, _build_ledger(config))
|
||||
def _build_transport(config: DaemonConfig) -> MessagingTransport:
|
||||
"""RabbitMQ when configured; otherwise in-memory stub (REQ-022)."""
|
||||
mq = config.rabbitmq
|
||||
if mq is None:
|
||||
# Stub: lets the daemon boot in environments without a broker (CI,
|
||||
# local dev). REQ-024 degrade policy still applies — call_rpc raises.
|
||||
return InMemoryTransport()
|
||||
return RabbitMQTransport(mq.url, timeout=mq.timeout)
|
||||
|
||||
base = create_app(
|
||||
slot_service=slot_service,
|
||||
slot_repo=slot_repo,
|
||||
intent_service=intent_service,
|
||||
default_total_slots=config.total_slots,
|
||||
)
|
||||
|
||||
stop = threading.Event()
|
||||
class Daemon:
|
||||
"""Thread-orchestrating daemon (no HTTP)."""
|
||||
|
||||
def _execution_loop() -> None:
|
||||
while not stop.wait(config.execution_interval_sec):
|
||||
try:
|
||||
execution.run_once()
|
||||
except Exception: # noqa: BLE001 - loop must survive transient errors
|
||||
continue
|
||||
def __init__(self, config: DaemonConfig) -> None:
|
||||
self.config = config
|
||||
self.stop = threading.Event()
|
||||
self._threads: list[threading.Thread] = []
|
||||
self._transports: list[MessagingTransport] = []
|
||||
|
||||
def _ledger_loop() -> None:
|
||||
while not stop.wait(config.ledger_interval_sec):
|
||||
try:
|
||||
ledger_writer.run_once()
|
||||
except Exception: # noqa: BLE001 - loop must survive transient errors
|
||||
continue
|
||||
# Persistence + services
|
||||
self.slot_repo = PostgreSQLSlotRepository(config.dsn)
|
||||
self.intent_store = PostgreSQLIntentStore(config.dsn)
|
||||
self.slot_service = SlotService(self.slot_repo)
|
||||
self.intent_service = IntentService(self.slot_service, self.slot_repo, self.intent_store)
|
||||
self.broker = _build_broker(config)
|
||||
self.outbox = PostgreSQLLedgerOutbox(config.dsn)
|
||||
self.ledger_writer = LedgerWriter(self.outbox, _build_ledger(config))
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(_: FastAPI):
|
||||
apply_migrations(config.dsn)
|
||||
slot_service.preallocate_daily_slots(
|
||||
datetime.now(tz=timezone.utc).date(), config.total_slots
|
||||
# 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(),
|
||||
)
|
||||
execution.reconcile() # restart recovery before serving (REQ-014)
|
||||
exec_worker = threading.Thread(target=_execution_loop, name="trex-execution", daemon=True)
|
||||
ledger_worker = threading.Thread(target=_ledger_loop, name="trex-ledger", daemon=True)
|
||||
exec_worker.start()
|
||||
ledger_worker.start()
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
stop.set()
|
||||
exec_worker.join(timeout=config.execution_interval_sec * 3)
|
||||
ledger_worker.join(timeout=config.ledger_interval_sec * 3)
|
||||
|
||||
base.router.lifespan_context = lifespan
|
||||
return base
|
||||
# ------------------------------------------------------------ event hook
|
||||
def _make_event_emitter(self):
|
||||
def emit(event_type: str, slot) -> None:
|
||||
payload = slot_transition_event_payload(slot, event_type=event_type)
|
||||
rk = topology.event_routing_key(
|
||||
event_type, slot.strategy_id or "_", slot.symbol or "_",
|
||||
)
|
||||
try:
|
||||
self.event_publisher.publish_event(rk, payload)
|
||||
except Exception: # noqa: BLE001 - publish failure must not roll back
|
||||
_log.warning("event publish failed (rk=%s)", rk, exc_info=True)
|
||||
return emit
|
||||
|
||||
# ------------------------------------------------------------ workers
|
||||
def _execution_loop(self) -> None:
|
||||
while not self.stop.wait(self.config.execution_interval_sec):
|
||||
try:
|
||||
self.execution.run_once()
|
||||
except Exception: # noqa: BLE001 - loop must survive transient errors
|
||||
continue
|
||||
|
||||
def _ledger_loop(self) -> None:
|
||||
while not self.stop.wait(self.config.ledger_interval_sec):
|
||||
try:
|
||||
self.ledger_writer.run_once()
|
||||
except Exception: # noqa: BLE001
|
||||
continue
|
||||
|
||||
def _intent_rpc_loop(self) -> None:
|
||||
transport = _build_transport(self.config)
|
||||
self._transports.append(transport)
|
||||
try:
|
||||
transport.declare_topology()
|
||||
except Exception: # noqa: BLE001
|
||||
pass
|
||||
handler = make_intent_rpc_handler(self.intent_service)
|
||||
transport.consume_rpc_forever(topology.INTENT_REQ_QUEUE, handler, self.stop)
|
||||
|
||||
def _snapshot_rpc_loop(self) -> None:
|
||||
transport = _build_transport(self.config)
|
||||
self._transports.append(transport)
|
||||
try:
|
||||
transport.declare_topology()
|
||||
except Exception: # noqa: BLE001
|
||||
pass
|
||||
handler = make_snapshot_rpc_handler(self.slot_repo, self.slot_service)
|
||||
transport.consume_rpc_forever(topology.SNAPSHOT_REQ_QUEUE, handler, self.stop)
|
||||
|
||||
# ------------------------------------------------------------ lifecycle
|
||||
def setup(self) -> None:
|
||||
apply_migrations(self.config.dsn)
|
||||
self.slot_service.preallocate_daily_slots(
|
||||
datetime.now(tz=timezone.utc).date(), self.config.total_slots
|
||||
)
|
||||
self.execution.reconcile() # REQ-014 restart recovery
|
||||
try:
|
||||
self.event_publisher.declare_topology()
|
||||
except Exception: # noqa: BLE001
|
||||
_log.warning("event publisher topology declare failed", exc_info=True)
|
||||
|
||||
def start(self) -> None:
|
||||
workers = [
|
||||
("trex-execution", self._execution_loop),
|
||||
("trex-ledger", self._ledger_loop),
|
||||
("trex-intent-rpc", self._intent_rpc_loop),
|
||||
("trex-snapshot-rpc", self._snapshot_rpc_loop),
|
||||
]
|
||||
for name, target in workers:
|
||||
t = threading.Thread(target=target, name=name, daemon=True)
|
||||
t.start()
|
||||
self._threads.append(t)
|
||||
|
||||
def shutdown(self) -> None:
|
||||
self.stop.set()
|
||||
for t in self._threads:
|
||||
t.join(
|
||||
timeout=max(self.config.execution_interval_sec, self.config.ledger_interval_sec) * 3
|
||||
)
|
||||
for tr in self._transports:
|
||||
try:
|
||||
tr.close()
|
||||
except Exception: # noqa: BLE001
|
||||
pass
|
||||
|
||||
def run(self) -> None: # pragma: no cover - process entrypoint
|
||||
self.setup()
|
||||
self.start()
|
||||
signal.signal(signal.SIGINT, lambda *_: self.stop.set())
|
||||
signal.signal(signal.SIGTERM, lambda *_: self.stop.set())
|
||||
try:
|
||||
while not self.stop.is_set():
|
||||
self.stop.wait(1.0)
|
||||
finally:
|
||||
self.shutdown()
|
||||
|
||||
|
||||
def main() -> None: # pragma: no cover - process entrypoint
|
||||
import uvicorn
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
config = DaemonConfig.from_env()
|
||||
app = build_app(config)
|
||||
uvicorn.run(app, host=config.host, port=config.port)
|
||||
Daemon(config).run()
|
||||
|
||||
|
||||
if __name__ == "__main__": # pragma: no cover
|
||||
|
||||
Reference in New Issue
Block a user