feat: 同步构建真实状态并支持在列表停止在途任务
- Gitea 运行未结束时探测作业级结论,修复前置作业失败后可复用工作流父作业/等待标签作业长期挂起导致状态永远构建中 - 打开我的构建/管理仪表盘时主动向构建平台同步在途任务状态;创建超 6 小时仍在途的任务判超时 - 构建中任务列表增加停止按钮(Gitea 需 1.28+ 取消 API,旧版本提示去运行页手动 Cancel),终态任务保留删除 - GitHub/Gitea 后端统一 cancel_run 接口
This commit is contained in:
+35
-8
@@ -3,6 +3,7 @@ import json
|
||||
import os
|
||||
import re
|
||||
import uuid
|
||||
from datetime import timedelta
|
||||
from pathlib import Path
|
||||
from urllib.parse import quote
|
||||
|
||||
@@ -22,12 +23,45 @@ from django.http import (
|
||||
Http404,
|
||||
)
|
||||
from django.shortcuts import render
|
||||
from django.utils import timezone
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
|
||||
from . import app_settings, build_backends, versions as rdeskVersions
|
||||
from .forms import GenerateForm
|
||||
from .models import GithubRun, STATUS_LABELS, STATUS_BADGE
|
||||
|
||||
# 终态状态集合
|
||||
TERMINAL_STATUSES = ('success', 'failure', 'cancelled', 'timed_out', 'skipped')
|
||||
# 正常构建约 30~45 分钟;超过该时长平台仍显示进行中的,视为 runner 离线/作业僵死
|
||||
STALE_BUILD_HOURS = 6
|
||||
|
||||
|
||||
def refresh_active_run(gh_run):
|
||||
"""向构建平台同步一次任务状态,状态落库后返回 True;网络异常或仍在进行则 False。
|
||||
|
||||
同时兜底两种平台侧的僵死:
|
||||
- Gitea 前置作业失败后可复用工作流父作业/无匹配标签的作业长期挂起;
|
||||
- 自建 runner 离线导致运行一直 in_progress(超过 STALE_BUILD_HOURS 判超时)。
|
||||
"""
|
||||
if gh_run.is_finished() or not gh_run.github_run_id:
|
||||
return False
|
||||
backend = build_backends.get_backend(gh_run.backend or 'github')
|
||||
try:
|
||||
info = backend.get_run(gh_run.github_run_id)
|
||||
except Exception as e:
|
||||
print(f"查询构建状态出错:{e}")
|
||||
return False
|
||||
if info is None:
|
||||
return False
|
||||
new_status = info.status if info.finished else None
|
||||
if new_status is None and timezone.now() - gh_run.created_at > timedelta(hours=STALE_BUILD_HOURS):
|
||||
new_status = 'timed_out'
|
||||
if new_status and new_status != gh_run.status:
|
||||
gh_run.status = new_status
|
||||
gh_run.save(update_fields=['status', 'updated_at'])
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def _webhook_denied(request):
|
||||
"""配置了 WEBHOOK_SECRET 时,校验 GitHub Actions 回调请求头;未配置则放行(兼容旧工作流)。"""
|
||||
@@ -390,14 +424,7 @@ def _get_run_status(uuid_val):
|
||||
backend = build_backends.get_backend(gh_run.backend or 'github')
|
||||
github_log_url = backend.log_url(gh_run.github_run_id) if gh_run.github_run_id else ''
|
||||
|
||||
if gh_run.status not in ['success', 'failure', 'cancelled', 'timed_out', 'skipped'] and gh_run.github_run_id:
|
||||
try:
|
||||
info = backend.get_run(gh_run.github_run_id)
|
||||
if info is not None and info.finished:
|
||||
gh_run.status = info.status
|
||||
gh_run.save()
|
||||
except Exception as e:
|
||||
print(f"查询构建状态出错:{e}")
|
||||
refresh_active_run(gh_run)
|
||||
|
||||
return {
|
||||
"found": True,
|
||||
|
||||
Reference in New Issue
Block a user