Files
aitbc/cli/handlers/bridge.py
aitbc a7b6e39cdf fix: replace all print() with click.echo() or logger in CLI production code
- 55 CLI files: handlers/, aitbc_cli/commands/, cli/core/, cli/utils/, top-level scripts
- Click-based files: print() -> click.echo()
- Library modules: print() -> logger.info/error/warning
- Fixed pre-existing indentation bugs in monitor.py dashboard function
- Fixed bare print() -> logger.info('') in chain_manager.py
- 0 remaining print() in production CLI code
- All files compile cleanly
2026-05-25 13:53:49 +02:00

127 lines
5.1 KiB
Python

"""Blockchain event bridge handlers."""
import subprocess
from aitbc import AITBCHTTPClient, NetworkError
import logging
logger = logging.getLogger(__name__)
def handle_bridge_health(args):
"""Health check for blockchain event bridge service."""
try:
from commands.legacy.blockchain_event_bridge import get_config as get_bridge_config
config = get_bridge_config()
if args.test_mode:
logger.info("🏥 Blockchain Event Bridge Health (test mode):")
logger.info("✅ Status: healthy")
logger.info("📦 Service: blockchain-event-bridge")
return
bridge_url = getattr(config, "bridge_url", "http://localhost:8204")
http_client = AITBCHTTPClient(base_url=bridge_url, timeout=10)
health = http_client.get("/health")
logger.info("🏥 Blockchain Event Bridge Health:")
for key, value in health.items():
logger.info(f" {key}: {value}")
except NetworkError as e:
logger.error(f"❌ Health check failed: {e}")
except Exception as e:
logger.error(f"❌ Error checking health: {e}")
def handle_bridge_metrics(args):
"""Get Prometheus metrics from blockchain event bridge service."""
try:
from commands.legacy.blockchain_event_bridge import get_config as get_bridge_config
config = get_bridge_config()
if args.test_mode:
logger.info("📊 Prometheus Metrics (test mode):")
logger.info(" bridge_events_total: 103691")
logger.info(" bridge_events_processed_total: 103691")
return
bridge_url = getattr(config, "bridge_url", "http://localhost:8204")
http_client = AITBCHTTPClient(base_url=bridge_url, timeout=10)
metrics = http_client.get("/metrics", return_response=True)
logger.info("📊 Prometheus Metrics:")
logger.info(metrics.text)
except NetworkError as e:
logger.error(f"❌ Failed to get metrics: {e}")
except Exception as e:
logger.error(f"❌ Error getting metrics: {e}")
def handle_bridge_status(args):
"""Get detailed status of blockchain event bridge service."""
try:
from commands.legacy.blockchain_event_bridge import get_config as get_bridge_config
config = get_bridge_config()
if args.test_mode:
logger.info("📊 Blockchain Event Bridge Status (test mode):")
logger.info("✅ Status: running")
logger.info("🔔 Subscriptions: blocks, transactions, contract_events")
return
bridge_url = getattr(config, "bridge_url", "http://localhost:8204")
http_client = AITBCHTTPClient(base_url=bridge_url, timeout=10)
status = http_client.get("/")
logger.info("📊 Blockchain Event Bridge Status:")
for key, value in status.items():
logger.info(f" {key}: {value}")
except NetworkError as e:
logger.error(f"❌ Failed to get status: {e}")
except Exception as e:
logger.error(f"❌ Error getting status: {e}")
def handle_bridge_config(args):
"""Show current configuration of blockchain event bridge service."""
try:
from commands.legacy.blockchain_event_bridge import get_config as get_bridge_config
config = get_bridge_config()
if args.test_mode:
logger.info("⚙️ Blockchain Event Bridge Configuration (test mode):")
logger.info("🔗 Blockchain RPC URL: http://localhost:8006")
logger.info("💬 Gossip Backend: redis")
return
bridge_url = getattr(config, "bridge_url", "http://localhost:8204")
http_client = AITBCHTTPClient(base_url=bridge_url, timeout=10)
service_config = http_client.get("/config")
logger.info("⚙️ Blockchain Event Bridge Configuration:")
for key, value in service_config.items():
logger.info(f" {key}: {value}")
except NetworkError as e:
logger.error(f"❌ Failed to get config: {e}")
except Exception as e:
logger.error(f"❌ Error getting config: {e}")
def handle_bridge_restart(args):
"""Restart blockchain event bridge service (via systemd)."""
try:
if args.test_mode:
logger.info("🔄 Blockchain event bridge restart triggered (test mode)")
logger.info("✅ Restart completed successfully")
return
result = subprocess.run(
["sudo", "systemctl", "restart", "aitbc-blockchain-event-bridge"],
capture_output=True,
text=True,
timeout=30
)
if result.returncode == 0:
logger.info("🔄 Blockchain event bridge restart triggered")
logger.info("✅ Restart completed successfully")
else:
logger.error(f"❌ Restart failed: {result.stderr}")
except subprocess.TimeoutExpired:
logger.info("❌ Restart timeout - service may be starting")
except FileNotFoundError:
logger.info("❌ systemctl not found - cannot restart service")
except Exception as e:
logger.error(f"❌ Error restarting service: {e}")