|
@@ -1,5 +1,5 @@
|
|
|
"""
|
|
"""
|
|
|
-从 S/A 级需求及其拓展点位触发 find_agent 视频发现。
|
|
|
|
|
|
|
+从全部 S/A 级需求及其拓展点位触发 find_agent 视频发现;有效视频满 200 提前结束。
|
|
|
|
|
|
|
|
任务层负责查库与组装上下文;Agent 负责搜索、画像与分池落库。
|
|
任务层负责查库与组装上下文;Agent 负责搜索、画像与分池落库。
|
|
|
"""
|
|
"""
|
|
@@ -23,7 +23,7 @@ from agents.find_agent.demand_run import (
|
|
|
from supply_infra.config import get_infra_settings
|
|
from supply_infra.config import get_infra_settings
|
|
|
from supply_infra.db.repositories.demand_grade_repo import DemandGradeRepository
|
|
from supply_infra.db.repositories.demand_grade_repo import DemandGradeRepository
|
|
|
from supply_infra.db.repositories.video_discovery_repo import VideoDiscoveryRepository
|
|
from supply_infra.db.repositories.video_discovery_repo import VideoDiscoveryRepository
|
|
|
-from supply_infra.db.session import get_session
|
|
|
|
|
|
|
+from supply_infra.db.session import ensure_mysql_pool_capacity, get_session
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
@@ -122,14 +122,16 @@ def discover_videos_from_demands(
|
|
|
force: bool = False,
|
|
force: bool = False,
|
|
|
) -> dict[str, Any]:
|
|
) -> dict[str, Any]:
|
|
|
"""
|
|
"""
|
|
|
- 对指定业务日的 S/A 需求拓展点位逐条调用 find_agent。
|
|
|
|
|
|
|
+ 对指定业务日全部 S/A 级需求(有拓展点位)逐条调用 find_agent。
|
|
|
|
|
|
|
|
每条记录对应一个 demand_grade + 其下全部视频与全部拓展点位。
|
|
每条记录对应一个 demand_grade + 其下全部视频与全部拓展点位。
|
|
|
执行前会预写 video_discovery_run,并按 biz_dt + demand_grade_id 跳过已执行记录。
|
|
执行前会预写 video_discovery_run,并按 biz_dt + demand_grade_id 跳过已执行记录。
|
|
|
- top_limit 按 score 取当日 top N 需求(S 优先于 A)。
|
|
|
|
|
|
|
+ 默认处理全部 S/A(S 优先于 A、再按 score 排序);仅 CLI --top-limit 可人为截断。
|
|
|
|
|
+ 当日 primary+backup 去重视频达到 200 时提前结束,否则跑完待处理队列。
|
|
|
"""
|
|
"""
|
|
|
started_at = datetime.now()
|
|
started_at = datetime.now()
|
|
|
batch_run_id = uuid.uuid4().hex
|
|
batch_run_id = uuid.uuid4().hex
|
|
|
|
|
+ ensure_mysql_pool_capacity(max(1, int(workers)))
|
|
|
|
|
|
|
|
try:
|
|
try:
|
|
|
resolved_biz_dt, contexts = list_find_demand_contexts(
|
|
resolved_biz_dt, contexts = list_find_demand_contexts(
|
|
@@ -220,11 +222,23 @@ def discover_videos_from_demands(
|
|
|
)
|
|
)
|
|
|
batch = contexts[next_context : next_context + batch_size]
|
|
batch = contexts[next_context : next_context + batch_size]
|
|
|
next_context += batch_size
|
|
next_context += batch_size
|
|
|
- futures = [
|
|
|
|
|
- executor.submit(process_single_discover, ctx, force=force)
|
|
|
|
|
|
|
+ logger.info(
|
|
|
|
|
+ "discover videos batch start: biz_dt=%s batch_size=%d "
|
|
|
|
|
+ "completed_batches=%d/%d demands=%s",
|
|
|
|
|
+ resolved_biz_dt,
|
|
|
|
|
+ len(batch),
|
|
|
|
|
+ next_context // batch_size,
|
|
|
|
|
+ (len(contexts) + batch_size - 1) // batch_size,
|
|
|
|
|
+ [
|
|
|
|
|
+ f"{ctx.demand_grade_id}:{ctx.demand_name}"
|
|
|
|
|
+ for ctx in batch
|
|
|
|
|
+ ],
|
|
|
|
|
+ )
|
|
|
|
|
+ future_to_ctx = {
|
|
|
|
|
+ executor.submit(process_single_discover, ctx, force=force): ctx
|
|
|
for ctx in batch
|
|
for ctx in batch
|
|
|
- ]
|
|
|
|
|
- pending = set(futures)
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+ pending = set(future_to_ctx.keys())
|
|
|
batch_started = monotonic()
|
|
batch_started = monotonic()
|
|
|
while pending:
|
|
while pending:
|
|
|
completed, pending = wait(
|
|
completed, pending = wait(
|
|
@@ -233,30 +247,65 @@ def discover_videos_from_demands(
|
|
|
return_when=FIRST_COMPLETED,
|
|
return_when=FIRST_COMPLETED,
|
|
|
)
|
|
)
|
|
|
if not completed:
|
|
if not completed:
|
|
|
|
|
+ pending_demands = [
|
|
|
|
|
+ f"{future_to_ctx[future].demand_grade_id}:"
|
|
|
|
|
+ f"{future_to_ctx[future].demand_name}"
|
|
|
|
|
+ for future in pending
|
|
|
|
|
+ ]
|
|
|
logger.info(
|
|
logger.info(
|
|
|
"discover videos still running: biz_dt=%s "
|
|
"discover videos still running: biz_dt=%s "
|
|
|
- "batch_pending=%d processed=%d elapsed_seconds=%d",
|
|
|
|
|
|
|
+ "batch_pending=%d processed=%d elapsed_seconds=%d "
|
|
|
|
|
+ "pending_demands=%s",
|
|
|
resolved_biz_dt,
|
|
resolved_biz_dt,
|
|
|
len(pending),
|
|
len(pending),
|
|
|
result["processed"],
|
|
result["processed"],
|
|
|
int(monotonic() - batch_started),
|
|
int(monotonic() - batch_started),
|
|
|
|
|
+ pending_demands,
|
|
|
)
|
|
)
|
|
|
continue
|
|
continue
|
|
|
|
|
|
|
|
for future in completed:
|
|
for future in completed:
|
|
|
|
|
+ ctx = future_to_ctx[future]
|
|
|
try:
|
|
try:
|
|
|
item_result = future.result()
|
|
item_result = future.result()
|
|
|
except Exception as exc:
|
|
except Exception as exc:
|
|
|
logger.exception(
|
|
logger.exception(
|
|
|
- "discover videos worker 出现未捕获错误: biz_dt=%s",
|
|
|
|
|
|
|
+ "discover videos worker 出现未捕获错误: biz_dt=%s "
|
|
|
|
|
+ "grade_id=%s demand=%s",
|
|
|
resolved_biz_dt,
|
|
resolved_biz_dt,
|
|
|
|
|
+ ctx.demand_grade_id,
|
|
|
|
|
+ ctx.demand_name,
|
|
|
)
|
|
)
|
|
|
result["failed"] += 1
|
|
result["failed"] += 1
|
|
|
result["processed"] += 1
|
|
result["processed"] += 1
|
|
|
- result["errors"].append({"error": str(exc)})
|
|
|
|
|
|
|
+ result["errors"].append(
|
|
|
|
|
+ {
|
|
|
|
|
+ "demand_grade_id": ctx.demand_grade_id,
|
|
|
|
|
+ "demand_name": ctx.demand_name,
|
|
|
|
|
+ "error": str(exc),
|
|
|
|
|
+ }
|
|
|
|
|
+ )
|
|
|
|
|
+ logger.info(
|
|
|
|
|
+ "discover videos batch item failed: grade_id=%s "
|
|
|
|
|
+ "demand=%s batch_remaining=%d total_processed=%d",
|
|
|
|
|
+ ctx.demand_grade_id,
|
|
|
|
|
+ ctx.demand_name,
|
|
|
|
|
+ len(pending),
|
|
|
|
|
+ result["processed"],
|
|
|
|
|
+ )
|
|
|
continue
|
|
continue
|
|
|
|
|
|
|
|
result["processed"] += 1
|
|
result["processed"] += 1
|
|
|
|
|
+ logger.info(
|
|
|
|
|
+ "discover videos batch item finished: grade_id=%s demand=%s "
|
|
|
|
|
+ "success=%s skipped=%s batch_remaining=%d total_processed=%d",
|
|
|
|
|
+ ctx.demand_grade_id,
|
|
|
|
|
+ ctx.demand_name,
|
|
|
|
|
+ item_result.get("success"),
|
|
|
|
|
+ item_result.get("skipped"),
|
|
|
|
|
+ len(pending),
|
|
|
|
|
+ result["processed"],
|
|
|
|
|
+ )
|
|
|
if item_result.get("skipped"):
|
|
if item_result.get("skipped"):
|
|
|
result["skipped"] += 1
|
|
result["skipped"] += 1
|
|
|
continue
|
|
continue
|