run_control_service.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. logger = logging.getLogger("tencent_realtime_control.service")
  19. def _env_flag(name: str, default: bool = False) -> bool:
  20. raw = os.getenv(name)
  21. if raw is None:
  22. return default
  23. return raw.strip().lower() in {"1", "true", "yes", "on"}
  24. def parse_args() -> argparse.Namespace:
  25. parser = argparse.ArgumentParser(description=__doc__)
  26. parser.add_argument(
  27. "--apply",
  28. action="store_true",
  29. help="Enable confirmed Feishu commands and real Tencent updates.",
  30. )
  31. return parser.parse_args()
  32. def main() -> None:
  33. load_environment()
  34. args = parse_args()
  35. apply = args.apply or _env_flag("RTC_APPLY_ENABLED")
  36. roi_config = RoiConfig.from_env()
  37. initialize_schema()
  38. if os.getenv("RTC_COMMAND_ENABLED", "1").strip().lower() in {
  39. "1",
  40. "true",
  41. "yes",
  42. }:
  43. from feishu_command_service import FeishuCommandService
  44. FeishuCommandService(apply=apply).start()
  45. logger.info("Feishu command WebSocket started")
  46. else:
  47. logger.warning("Feishu command WebSocket is disabled")
  48. if roi_config.sheet_approval_enabled:
  49. from roi_control.sheet_approval import RoiSheetApprovalService
  50. RoiSheetApprovalService().start()
  51. logger.info("ROI sheet approval polling started")
  52. else:
  53. logger.warning("ROI sheet approval polling is disabled")
  54. run_forever(apply=apply)
  55. if __name__ == "__main__":
  56. logging.basicConfig(
  57. level=os.getenv("LOG_LEVEL", "INFO"),
  58. format="%(asctime)s %(levelname)s %(name)s %(message)s",
  59. )
  60. main()