run_existing_execution_mysql.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. """Cloud MySQL entrypoint for an existing PG Pattern V2 execution.
  2. This entrypoint intentionally writes only content-deconstruction-supply.demand_content.
  3. It never runs prepare/mining and does not write demand_task, Hive, PG Pattern, or
  4. legacy MySQL Pattern tables.
  5. """
  6. from __future__ import annotations
  7. import argparse
  8. import asyncio
  9. import json
  10. import os
  11. import sys
  12. from pathlib import Path
  13. from datetime import datetime
  14. sys.path.insert(0, str(Path(__file__).parent.parent.parent))
  15. def _load_project_env() -> None:
  16. try:
  17. from dotenv import load_dotenv
  18. except ImportError:
  19. return
  20. load_dotenv(Path(__file__).parent.parent.parent / ".env")
  21. def _safe_path_segment(value: str) -> str:
  22. return "".join(ch if ch.isalnum() or ch in {"-", "_"} else "_" for ch in value).strip("_") or "run"
  23. def _prepare_run_output_paths(execution_id: int, run_label: str) -> None:
  24. run_root = Path(__file__).parent / "test_output_data" / "mysql_runs" / _safe_path_segment(run_label)
  25. result_base_dir = run_root / "intermediate" / "result"
  26. os.environ["DEMAND_RESULT_BASE_DIR"] = str(result_base_dir)
  27. os.environ["DEMAND_OUTPUT_BASE_DIR"] = str(run_root / "output")
  28. os.environ["DEMAND_TRACE_STORE_PATH"] = str(run_root / ".trace")
  29. demand_items_path = result_base_dir / str(execution_id) / f"execution_id_{execution_id}_demand_items.json"
  30. if demand_items_path.exists():
  31. demand_items_path.unlink()
  32. def _configure_mysql_env(run_label: str) -> None:
  33. os.environ["DEMAND_OUTPUT_MODE"] = "mysql_demand_content"
  34. os.environ["DEMAND_MYSQL_ENTRYPOINT"] = "run_existing_execution_mysql"
  35. os.environ["DEMAND_RUN_LABEL"] = run_label
  36. def _validate_execution_success(execution_id: int) -> None:
  37. from examples.demand.db_manager import query_execution_for_evidence
  38. execution = query_execution_for_evidence(execution_id)
  39. if not execution:
  40. raise ValueError(f"execution_id={execution_id} 不存在,MySQL 写入入口只允许跑已有 execution")
  41. if str(execution.get("status") or "").strip().lower() != "success":
  42. raise ValueError(
  43. f"execution_id={execution_id} status={execution.get('status')},不是 success,拒绝生成需求"
  44. )
  45. def parse_args() -> argparse.Namespace:
  46. parser = argparse.ArgumentParser(
  47. description="Run DemandAgent against an existing PG Pattern V2 execution_id and write only MySQL demand_content."
  48. )
  49. parser.add_argument("--execution-id", type=int, required=True, help="Existing PG pattern_mining_execution.id")
  50. parser.add_argument("--merge-level2", required=True, help="Current merge_leve2 / cluster name")
  51. parser.add_argument("--platform-type", default="piaoquan", help="Platform label")
  52. parser.add_argument("--count", type=int, default=5, help="Approximate demand count requested from the prompt")
  53. parser.add_argument("--run-label", default=None, help="Label stored at ext_data.run_label for verification")
  54. return parser.parse_args()
  55. async def async_main() -> dict:
  56. _load_project_env()
  57. args = parse_args()
  58. run_label = args.run_label or f"mysql_{args.execution_id}_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
  59. _configure_mysql_env(run_label)
  60. _prepare_run_output_paths(args.execution_id, run_label)
  61. _validate_execution_success(args.execution_id)
  62. from examples.demand.run import main
  63. result = await main(
  64. cluster_name=args.merge_level2,
  65. platform_type=args.platform_type,
  66. count=args.count,
  67. execution_id=args.execution_id,
  68. task_id=None,
  69. )
  70. result["run_label"] = run_label
  71. return result
  72. def main() -> None:
  73. result = asyncio.run(async_main())
  74. print(json.dumps(result, ensure_ascii=False, indent=2))
  75. if __name__ == "__main__":
  76. main()