(
run_id: int,
*,
mode: Optional[str] = None,
source: Optional[str] = None,
progress_callback: Optional[Callable[[Dict[str, Any]], None]] = None,
cancel_checker: Optional[Callable[[], bool]] = None,
)
| 668 | |
| 669 | |
| 670 | def execute_selfcheck_run( |
| 671 | run_id: int, |
| 672 | *, |
| 673 | mode: Optional[str] = None, |
| 674 | source: Optional[str] = None, |
| 675 | progress_callback: Optional[Callable[[Dict[str, Any]], None]] = None, |
| 676 | cancel_checker: Optional[Callable[[], bool]] = None, |
| 677 | ) -> Dict[str, Any]: |
| 678 | with get_db() as db: |
| 679 | run = db.query(SelfCheckRun).filter(SelfCheckRun.id == int(run_id)).first() |
| 680 | if not run: |
| 681 | raise ValueError("自检任务不存在") |
| 682 | mode_text = "full" if str(mode or run.mode or "").strip().lower() == "full" else "quick" |
| 683 | source_text = str(source or run.source or "manual").strip().lower() or "manual" |
| 684 | run.mode = mode_text |
| 685 | run.source = source_text |
| 686 | run.status = RUN_STATUS_RUNNING |
| 687 | run.started_at = _utc_now() |
| 688 | run.finished_at = None |
| 689 | run.error_message = None |
| 690 | run.result_data = {"checks": [], "repairs": [], "progress": {"completed": 0, "total": 0}} |
| 691 | run.summary = "系统自检运行中" |
| 692 | db.commit() |
| 693 | |
| 694 | started = time.perf_counter() |
| 695 | checks: List[Dict[str, Any]] = [] |
| 696 | proxy_url = _resolve_selfcheck_proxy_url() |
| 697 | check_funcs: List[Callable[[], Dict[str, Any]]] = [_check_environment, lambda: _check_network(proxy_url), lambda: _check_accounts_auth(mode_text, proxy_url)] |
| 698 | if mode_text == "full": |
| 699 | check_funcs.extend([_check_team_pool, _check_payment_pipeline, _check_data_consistency]) |
| 700 | |
| 701 | total = len(check_funcs) |
| 702 | failed_error = "" |
| 703 | try: |
| 704 | for idx, func in enumerate(check_funcs, start=1): |
| 705 | if cancel_checker and cancel_checker(): |
| 706 | score_info = _compute_score(checks) |
| 707 | elapsed_ms = int((time.perf_counter() - started) * 1000) |
| 708 | with get_db() as db: |
| 709 | run = db.query(SelfCheckRun).filter(SelfCheckRun.id == int(run_id)).first() |
| 710 | if not run: |
| 711 | raise ValueError("自检任务不存在") |
| 712 | run.status = RUN_STATUS_CANCELLED |
| 713 | run.total_checks = score_info["total"] |
| 714 | run.passed_checks = score_info["passed"] |
| 715 | run.warning_checks = score_info["warns"] |
| 716 | run.failed_checks = score_info["failed"] |
| 717 | run.score = score_info["score"] |
| 718 | run.duration_ms = elapsed_ms |
| 719 | run.summary = f"任务已取消(已完成 {len(checks)}/{total})" |
| 720 | run.error_message = None |
| 721 | run.finished_at = _utc_now() |
| 722 | data = _safe_dict(run.result_data) |
| 723 | data["checks"] = checks |
| 724 | data["progress"] = {"completed": len(checks), "total": total} |
| 725 | run.result_data = data |
| 726 | db.commit() |
| 727 | db.refresh(run) |
no test coverage detected