run_supply_pipeline.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. """Compatibility entry point for the durable supply pipeline."""
  2. from __future__ import annotations
  3. import argparse
  4. import time
  5. from typing import Any
  6. from supply_infra.pipeline.run_service import get_pipeline_run, submit_pipeline_run
  7. from supply_infra.scheduler.cli_result import run_cli
  8. _TERMINAL = {
  9. "succeeded",
  10. "failed",
  11. "cancelled",
  12. "deadline_exceeded",
  13. }
  14. def run_supply_pipeline(
  15. biz_dt: str | None = None,
  16. *,
  17. wait: bool = False,
  18. wait_timeout_seconds: int = 86400,
  19. poll_seconds: float = 2.0,
  20. ) -> dict[str, Any]:
  21. """
  22. Submit a durable batch.
  23. This function no longer executes business steps directly. ``wait=True`` is
  24. retained for diagnostics and waits on MySQL state rather than an in-memory
  25. thread.
  26. """
  27. submission = submit_pipeline_run(
  28. biz_dt=biz_dt,
  29. trigger_type="cli",
  30. trigger_source="legacy_run_supply_pipeline",
  31. )
  32. accepted = submission.to_dict()
  33. if not wait:
  34. return accepted
  35. deadline = time.monotonic() + wait_timeout_seconds
  36. while time.monotonic() < deadline:
  37. payload = get_pipeline_run(submission.run_id)
  38. if payload is None:
  39. return {
  40. **accepted,
  41. "success": False,
  42. "error": "Persistent run disappeared",
  43. }
  44. if payload["status"] in _TERMINAL:
  45. return {
  46. **payload,
  47. "success": payload["status"] == "succeeded",
  48. }
  49. time.sleep(max(0.1, poll_seconds))
  50. return {
  51. **accepted,
  52. "success": False,
  53. "error": f"Timed out waiting for run after {wait_timeout_seconds}s",
  54. }
  55. if __name__ == "__main__":
  56. parser = argparse.ArgumentParser(description="Submit the durable supply pipeline")
  57. parser.add_argument("biz_dt", nargs="?")
  58. parser.add_argument("--wait", action="store_true")
  59. args = parser.parse_args()
  60. run_cli(
  61. lambda: run_supply_pipeline(args.biz_dt, wait=args.wait),
  62. label="run_supply_pipeline",
  63. )