#!/usr/bin/env python3
"""手动重试 demand_grade_plan_group_item 中失败的分级任务。

用法:
    python jobs/retry_failed_grade_plan_items.py
    python jobs/retry_failed_grade_plan_items.py 20260721
    python jobs/retry_failed_grade_plan_items.py 20260721 5
    python jobs/retry_failed_grade_plan_items.py 20260721 5 --dry-run
"""
from __future__ import annotations

import logging
import sys

from supply_infra.scheduler.jobs.grade_demand_pool import retry_failed_plan_group_items

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
)


def main(
    biz_dt: str | None = None,
    workers_arg: str | None = None,
    *,
    dry_run: bool = False,
) -> dict:
    workers = int(workers_arg) if workers_arg else 5
    result = retry_failed_plan_group_items(
        biz_dt,
        workers=workers,
        dry_run=dry_run,
    )
    print(result)
    return result


if __name__ == "__main__":
    args = sys.argv[1:]
    biz_dt_arg = args[0] if args and not args[0].startswith("-") else None
    workers_arg = None
    if biz_dt_arg and len(args) > 1 and not args[1].startswith("-"):
        workers_arg = args[1]
    main(biz_dt_arg, workers_arg, dry_run="--dry-run" in args)
