run_control_service.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/env python
  2. """Run Feishu controls and the ten-minute real-time control loop."""
  3. from __future__ import annotations
  4. import argparse
  5. import logging
  6. import os
  7. import sys
  8. from pathlib import Path
  9. ROOT = Path(__file__).resolve().parents[2]
  10. AUTO_DIR = ROOT / "examples" / "auto_put_ad_mini"
  11. for path in (ROOT, AUTO_DIR):
  12. if str(path) not in sys.path:
  13. sys.path.insert(0, str(path))
  14. from run_once import load_environment # noqa: E402
  15. from run_scheduler import run_forever # noqa: E402
  16. from storage import initialize_schema # noqa: E402
  17. from roi_control.config import RoiConfig # noqa: E402
  18. from revenue_forecast_config import RevenueForecastConfig # noqa: E402
  19. logger = logging.getLogger("tencent_realtime_control.service")
  20. def _env_flag(name: str, default: bool = False) -> bool:
  21. raw = os.getenv(name)
  22. if raw is None:
  23. return default
  24. return raw.strip().lower() in {"1", "true", "yes", "on"}
  25. def parse_args() -> argparse.Namespace:
  26. parser = argparse.ArgumentParser(description=__doc__)
  27. parser.add_argument(
  28. "--apply",
  29. action="store_true",
  30. help="Enable confirmed Feishu commands and real Tencent updates.",
  31. )
  32. return parser.parse_args()
  33. def main() -> None:
  34. load_environment()
  35. args = parse_args()
  36. apply = args.apply or _env_flag("RTC_APPLY_ENABLED")
  37. roi_config = RoiConfig.from_env()
  38. revenue_config = RevenueForecastConfig.from_env()
  39. initialize_schema()
  40. if os.getenv("RTC_COMMAND_ENABLED", "1").strip().lower() in {
  41. "1",
  42. "true",
  43. "yes",
  44. }:
  45. from feishu_command_service import FeishuCommandService
  46. FeishuCommandService(apply=apply).start()
  47. logger.info("Feishu command WebSocket started")
  48. else:
  49. logger.warning("Feishu command WebSocket is disabled")
  50. if roi_config.sheet_approval_enabled:
  51. from roi_control.sheet_approval import RoiSheetApprovalService
  52. RoiSheetApprovalService().start()
  53. logger.info("ROI sheet approval polling started")
  54. else:
  55. logger.warning("ROI sheet approval polling is disabled")
  56. if revenue_config.enabled:
  57. from revenue_forecast_service import start_revenue_forecast_service
  58. start_revenue_forecast_service(revenue_config)
  59. else:
  60. logger.info("Revenue forecast is disabled")
  61. run_forever(apply=apply)
  62. if __name__ == "__main__":
  63. logging.basicConfig(
  64. level=os.getenv("LOG_LEVEL", "INFO"),
  65. format="%(asctime)s %(levelname)s %(name)s %(message)s",
  66. )
  67. main()