realtime_config.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. """Runtime configuration for Tencent real-time control."""
  2. from __future__ import annotations
  3. import os
  4. from dataclasses import dataclass
  5. from decimal import Decimal
  6. @dataclass(frozen=True)
  7. class RealtimeControlConfig:
  8. start_hour: int = 12
  9. stop_hour: int = 21
  10. poll_seconds: int = 600
  11. high_cpm: Decimal = Decimal("250")
  12. low_cpm: Decimal = Decimal("190")
  13. bid_up_ratio: Decimal = Decimal("1.05")
  14. inventory_refresh_minutes: int = 60
  15. max_partition_lag_hours: int = 2
  16. lock_name: str = "tencent_realtime_control"
  17. next_delivery_hour: int = 6
  18. @classmethod
  19. def from_env(cls) -> "RealtimeControlConfig":
  20. config = cls(
  21. start_hour=int(os.getenv("RTC_START_HOUR", "12")),
  22. stop_hour=int(os.getenv("RTC_STOP_HOUR", "21")),
  23. poll_seconds=int(os.getenv("RTC_POLL_SECONDS", "600")),
  24. high_cpm=Decimal(os.getenv("RTC_HIGH_CPM", "250")),
  25. low_cpm=Decimal(os.getenv("RTC_LOW_CPM", "190")),
  26. bid_up_ratio=Decimal(os.getenv("RTC_BID_UP_RATIO", "1.05")),
  27. inventory_refresh_minutes=int(
  28. os.getenv("RTC_INVENTORY_REFRESH_MINUTES", "60")
  29. ),
  30. max_partition_lag_hours=int(
  31. os.getenv("RTC_MAX_PARTITION_LAG_HOURS", "2")
  32. ),
  33. lock_name=os.getenv(
  34. "RTC_DB_LOCK_NAME", "tencent_realtime_control"
  35. ).strip(),
  36. next_delivery_hour=int(
  37. os.getenv("RTC_COMMAND_NEXT_DELIVERY_HOUR", "6")
  38. ),
  39. )
  40. if not 0 <= config.start_hour < config.stop_hour <= 23:
  41. raise ValueError("RTC hours must satisfy 0 <= start < stop <= 23")
  42. if config.poll_seconds < 60:
  43. raise ValueError("RTC_POLL_SECONDS must be at least 60")
  44. if config.low_cpm >= config.high_cpm:
  45. raise ValueError("RTC_LOW_CPM must be lower than RTC_HIGH_CPM")
  46. if config.bid_up_ratio <= 1:
  47. raise ValueError("RTC_BID_UP_RATIO must be greater than 1")
  48. if config.inventory_refresh_minutes < 5:
  49. raise ValueError("RTC_INVENTORY_REFRESH_MINUTES must be at least 5")
  50. if config.max_partition_lag_hours < 1:
  51. raise ValueError("RTC_MAX_PARTITION_LAG_HOURS must be at least 1")
  52. if not config.lock_name:
  53. raise ValueError("RTC_DB_LOCK_NAME must not be empty")
  54. if not 0 <= config.next_delivery_hour <= 23:
  55. raise ValueError("RTC_COMMAND_NEXT_DELIVERY_HOUR must be in [0, 23]")
  56. return config