dag.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. from __future__ import annotations
  2. from dataclasses import dataclass
  3. @dataclass(frozen=True)
  4. class StepDefinition:
  5. key: str
  6. order: int
  7. dependencies: tuple[str, ...]
  8. timeout_seconds: int
  9. max_attempts: int = 1
  10. retryable: bool = False
  11. critical: bool = True
  12. _RAW_STEPS: tuple[tuple[str, int, bool], ...] = (
  13. ("global_tree_sync", 3600, True),
  14. ("demand_pool_source_sync", 3600, True),
  15. ("demand_classify", 21600, True),
  16. ("demand_belong_rel_sync", 3600, True),
  17. ("real_metrics_sync", 3600, True),
  18. ("popularity_stats", 3600, True),
  19. ("category_tree_weight", 3600, True),
  20. ("source_video_sync", 7200, True),
  21. ("demand_grade", 21600, False),
  22. ("platform_demand_materialize", 3600, True),
  23. ("daily_demand_package", 3600, True),
  24. ("demand_expand", 21600, False),
  25. ("video_discovery", 21600, False),
  26. ("content_feedback_materialize", 3600, True),
  27. ("aigc_write_record", 1800, True),
  28. )
  29. def _build_steps() -> tuple[StepDefinition, ...]:
  30. result: list[StepDefinition] = []
  31. previous: str | None = None
  32. for order, (key, timeout_seconds, retryable) in enumerate(_RAW_STEPS, start=1):
  33. result.append(
  34. StepDefinition(
  35. key=key,
  36. order=order,
  37. dependencies=(previous,) if previous else (),
  38. timeout_seconds=timeout_seconds,
  39. max_attempts=2 if retryable else 1,
  40. retryable=retryable,
  41. )
  42. )
  43. previous = key
  44. return tuple(result)
  45. PIPELINE_STEPS = _build_steps()
  46. STEP_BY_KEY = {step.key: step for step in PIPELINE_STEPS}
  47. def validate_dag() -> None:
  48. seen: set[str] = set()
  49. for step in PIPELINE_STEPS:
  50. if step.key in seen:
  51. raise ValueError(f"Duplicate step key: {step.key}")
  52. missing = set(step.dependencies) - seen
  53. if missing:
  54. raise ValueError(f"Step {step.key} has unresolved dependencies: {sorted(missing)}")
  55. seen.add(step.key)
  56. validate_dag()