feat(bgp): improve prefix-geo pipeline and collector reliability

This commit is contained in:
linkong
2026-04-07 15:23:26 +08:00
parent 5b9ef0223d
commit c439e91d12
11 changed files with 621 additions and 62 deletions

View File

@@ -17,6 +17,7 @@ from app.models.user import User
from app.services.scheduler import get_latest_task_id_for_datasource, run_collector_now, sync_datasource_job
router = APIRouter()
STALE_RUNNING_TASK_TIMEOUT_MINUTES = 90
def format_frequency_label(minutes: int) -> str:
@@ -71,7 +72,32 @@ async def get_running_task(db: AsyncSession, datasource_id: int) -> Optional[Col
.order_by(CollectionTask.started_at.desc())
.limit(1)
)
return result.scalar_one_or_none()
task = result.scalar_one_or_none()
if not task:
return None
started_at = task.started_at
if started_at is None:
return task
now = datetime.now(timezone.utc)
if started_at.tzinfo is None:
started_at = started_at.replace(tzinfo=timezone.utc)
if now - started_at <= timedelta(minutes=STALE_RUNNING_TASK_TIMEOUT_MINUTES):
return task
existing_error = (task.error_message or "").strip()
stale_reason = (
f"Marked failed automatically after stale running timeout "
f"({STALE_RUNNING_TASK_TIMEOUT_MINUTES}m)"
)
task.status = "failed"
task.phase = "failed"
task.completed_at = now
task.error_message = f"{existing_error}\n{stale_reason}".strip() if existing_error else stale_reason
await db.commit()
return None
@router.get("")