|
@@ -1,19 +1,11 @@
|
|
|
from __future__ import annotations
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
-import hashlib
|
|
|
|
|
-import json
|
|
|
|
|
from collections.abc import Callable
|
|
from collections.abc import Callable
|
|
|
-from pathlib import Path
|
|
|
|
|
from typing import Any
|
|
from typing import Any
|
|
|
|
|
|
|
|
-from supply_infra.config import get_infra_settings
|
|
|
|
|
-from supply_infra.db.repositories.pipeline_outbox_repo import PipelineOutboxRepository
|
|
|
|
|
-from supply_infra.db.session import get_session
|
|
|
|
|
from supply_infra.pipeline.contracts import StepContext
|
|
from supply_infra.pipeline.contracts import StepContext
|
|
|
|
|
|
|
|
StepHandler = Callable[[StepContext], dict[str, Any]]
|
|
StepHandler = Callable[[StepContext], dict[str, Any]]
|
|
|
-_REPO_ROOT = Path(__file__).resolve().parents[2]
|
|
|
|
|
-_MAX_INLINE_EFFECT_BYTES = 512_000
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _global_tree(context: StepContext) -> dict[str, Any]:
|
|
def _global_tree(context: StepContext) -> dict[str, Any]:
|
|
@@ -104,74 +96,6 @@ def _discover(context: StepContext) -> dict[str, Any]:
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
-def _aigc_write_record(context: StepContext) -> dict[str, Any]:
|
|
|
|
|
- from supply_infra.scheduler.jobs.publish_videos_from_discovery import (
|
|
|
|
|
- publish_videos_from_discovery,
|
|
|
|
|
- )
|
|
|
|
|
-
|
|
|
|
|
- settings = get_infra_settings()
|
|
|
|
|
- payload = publish_videos_from_discovery(biz_dt=context.biz_dt)
|
|
|
|
|
- publish_success = bool(payload.get("success", True))
|
|
|
|
|
- canonical = json.dumps(
|
|
|
|
|
- payload,
|
|
|
|
|
- ensure_ascii=False,
|
|
|
|
|
- sort_keys=True,
|
|
|
|
|
- separators=(",", ":"),
|
|
|
|
|
- default=str,
|
|
|
|
|
- )
|
|
|
|
|
- encoded = canonical.encode("utf-8")
|
|
|
|
|
- payload_hash = hashlib.sha256(encoded).hexdigest()
|
|
|
|
|
- idempotency_key = f"aigc_write_record:{context.biz_dt}:{payload_hash}"
|
|
|
|
|
- payload_uri: str | None = None
|
|
|
|
|
- stored_payload: dict[str, Any] | None = payload
|
|
|
|
|
- if len(encoded) > _MAX_INLINE_EFFECT_BYTES:
|
|
|
|
|
- log_dir = Path(settings.pipeline_log_dir)
|
|
|
|
|
- if not log_dir.is_absolute():
|
|
|
|
|
- log_dir = _REPO_ROOT / log_dir
|
|
|
|
|
- effect_dir = log_dir / context.run_id / "effects"
|
|
|
|
|
- effect_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
- effect_path = effect_dir / f"{context.step_run_id}-{payload_hash}.json"
|
|
|
|
|
- temporary_path = effect_path.with_suffix(".json.tmp")
|
|
|
|
|
- temporary_path.write_bytes(encoded)
|
|
|
|
|
- temporary_path.replace(effect_path)
|
|
|
|
|
- payload_uri = str(effect_path)
|
|
|
|
|
- stored_payload = {
|
|
|
|
|
- "externalized": True,
|
|
|
|
|
- "payload_bytes": len(encoded),
|
|
|
|
|
- "payload_hash": payload_hash,
|
|
|
|
|
- }
|
|
|
|
|
- with get_session() as session:
|
|
|
|
|
- record = PipelineOutboxRepository(session).record_effect(
|
|
|
|
|
- run_id=context.run_id,
|
|
|
|
|
- step_run_id=context.step_run_id,
|
|
|
|
|
- effect_type="aigc_write_record",
|
|
|
|
|
- idempotency_key=idempotency_key,
|
|
|
|
|
- payload_hash=payload_hash,
|
|
|
|
|
- payload=stored_payload,
|
|
|
|
|
- payload_uri=payload_uri,
|
|
|
|
|
- )
|
|
|
|
|
- outbox_id = record.outbox_id
|
|
|
|
|
-
|
|
|
|
|
- result: dict[str, Any] = {
|
|
|
|
|
- "success": publish_success,
|
|
|
|
|
- "effect_recorded": True,
|
|
|
|
|
- "external_request_made": int(payload.get("candidate_count", 0) or 0) > 0,
|
|
|
|
|
- "outbox_id": outbox_id,
|
|
|
|
|
- "payload_hash": payload_hash,
|
|
|
|
|
- "payload_uri": payload_uri,
|
|
|
|
|
- "candidate_count": payload.get("candidate_count", 0),
|
|
|
|
|
- "batch_count": payload.get("batch_count", 0),
|
|
|
|
|
- "failed_batch_count": payload.get("failed_batch_count", 0),
|
|
|
|
|
- }
|
|
|
|
|
- if not publish_success:
|
|
|
|
|
- result["error"] = str(
|
|
|
|
|
- payload.get("error")
|
|
|
|
|
- or f"AIGC publish failed ({result['failed_batch_count']} batch(es))"
|
|
|
|
|
- )
|
|
|
|
|
- result["error_code"] = "aigc_publish_failed"
|
|
|
|
|
- return result
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
STEP_REGISTRY: dict[str, StepHandler] = {
|
|
STEP_REGISTRY: dict[str, StepHandler] = {
|
|
|
"global_tree_sync": _global_tree,
|
|
"global_tree_sync": _global_tree,
|
|
|
"demand_pool_source_sync": _demand_source,
|
|
"demand_pool_source_sync": _demand_source,
|
|
@@ -184,7 +108,6 @@ STEP_REGISTRY: dict[str, StepHandler] = {
|
|
|
"demand_grade": _grade,
|
|
"demand_grade": _grade,
|
|
|
"demand_expand": _expand,
|
|
"demand_expand": _expand,
|
|
|
"video_discovery": _discover,
|
|
"video_discovery": _discover,
|
|
|
- "aigc_write_record": _aigc_write_record,
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|