Files
2026-07-27 18:59:28 +08:00

75 lines
2.3 KiB
Python

from datetime import timedelta
from celery import shared_task
from celery.exceptions import SoftTimeLimitExceeded
from django.conf import settings
from django.utils import timezone
from apps.agents.models import AgentRun
from apps.agents.services import append_run_event
from apps.agents.services import execute_agent_run as execute_agent_run_service
from apps.audit.models import OperationAuditLog
@shared_task(
bind=True,
name="apps.agents.tasks.execute_agent_run",
soft_time_limit=600,
time_limit=630,
)
def execute_agent_run(self, run_id: int) -> None:
try:
execute_agent_run_service(run_id)
except SoftTimeLimitExceeded:
run = AgentRun.objects.get(id=run_id)
run.status = "timed_out"
run.error_code = "run_timeout"
run.error_message = "Agent 运行超过 10 分钟限制"
run.progress_stage = "timed_out"
run.progress_message = "Agent 运行超时"
run.finished_at = timezone.now()
run.save(
update_fields=[
"status",
"error_code",
"error_message",
"progress_stage",
"progress_message",
"finished_at",
]
)
append_run_event(
run,
"timed_out",
{"error_code": "run_timeout", "error_message": run.error_message},
)
OperationAuditLog.objects.create(
user=run.created_by,
module="agents",
action="run_finished",
target_type="agent_run",
target_id=str(run.public_id),
request_snapshot={"status": "timed_out"},
result="timed_out",
)
@shared_task(name="apps.agents.tasks.cleanup_expired_agent_runs")
def cleanup_expired_agent_runs() -> int:
cutoff = timezone.now() - timedelta(days=settings.AI_RUN_RETENTION_DAYS)
queryset = AgentRun.objects.filter(created_at__lt=cutoff)
count = queryset.count()
if count:
OperationAuditLog.objects.create(
module="agents",
action="cleanup_runs",
target_type="agent_run",
request_snapshot={
"deleted_count": count,
"retention_days": settings.AI_RUN_RETENTION_DAYS,
},
result="success",
)
queryset.delete()
return count