fix(file-summary): 同步压缩包工作流状态与结果刷新
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from threading import Thread
|
||||
from uuid import uuid4
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import transaction
|
||||
from django.utils import timezone
|
||||
|
||||
@@ -17,6 +19,7 @@ from review_agent.models import (
|
||||
)
|
||||
|
||||
from .events import record_event
|
||||
from .services.archive import ARCHIVE_EXTENSIONS
|
||||
from .skills.archive_extract import ArchiveExtractSkill
|
||||
from .skills.base import WorkflowContext
|
||||
from .skills.document_page_count import DocumentPageCountSkill
|
||||
@@ -54,6 +57,10 @@ def build_batch_no() -> str:
|
||||
return f"FS-{timezone.localtime().strftime('%Y%m%d%H%M%S')}-{uuid4().hex[:6]}"
|
||||
|
||||
|
||||
def build_batch_work_dir(batch_no: str) -> Path:
|
||||
return Path(settings.MEDIA_ROOT) / "file_summary" / "work" / batch_no
|
||||
|
||||
|
||||
@transaction.atomic
|
||||
def create_file_summary_batch(
|
||||
*,
|
||||
@@ -78,15 +85,29 @@ def create_file_summary_batch(
|
||||
},
|
||||
)
|
||||
|
||||
batch_no = build_batch_no()
|
||||
work_dir = build_batch_work_dir(batch_no)
|
||||
work_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
batch = FileSummaryBatch.objects.create(
|
||||
conversation=conversation,
|
||||
user=user,
|
||||
trigger_message=trigger_message,
|
||||
batch_no=build_batch_no(),
|
||||
batch_no=batch_no,
|
||||
work_dir=str(work_dir),
|
||||
)
|
||||
|
||||
for attachment in active_attachments:
|
||||
FileSummaryBatchAttachment.objects.create(batch=batch, attachment=attachment)
|
||||
source_role = (
|
||||
FileSummaryBatchAttachment.SourceRole.ARCHIVE
|
||||
if Path(attachment.original_name).suffix.lower().lstrip(".") in ARCHIVE_EXTENSIONS
|
||||
else FileSummaryBatchAttachment.SourceRole.MULTI_FILE
|
||||
)
|
||||
FileSummaryBatchAttachment.objects.create(
|
||||
batch=batch,
|
||||
attachment=attachment,
|
||||
source_role=source_role,
|
||||
)
|
||||
attachment.upload_status = FileAttachment.UploadStatus.BOUND
|
||||
attachment.save(update_fields=["upload_status"])
|
||||
|
||||
@@ -152,7 +173,7 @@ class WorkflowExecutor:
|
||||
record_event(
|
||||
self.batch,
|
||||
"node_progress",
|
||||
{"node_code": node.node_code, "status": node.status, "progress": node.progress},
|
||||
{"node_code": node.node_code, "status": node.status, "progress": node.progress, "message": node.message},
|
||||
)
|
||||
|
||||
skill_name = next(
|
||||
@@ -160,18 +181,35 @@ class WorkflowExecutor:
|
||||
"",
|
||||
)
|
||||
if skill_name:
|
||||
result = self.registry.execute(skill_name, WorkflowContext(batch=self.batch))
|
||||
if not result.success:
|
||||
logger.warning(
|
||||
"Workflow node skill failed",
|
||||
extra={
|
||||
"batch_id": self.batch.pk,
|
||||
try:
|
||||
result = self.registry.execute(skill_name, WorkflowContext(batch=self.batch))
|
||||
if not result.success:
|
||||
logger.warning(
|
||||
"Workflow node skill failed",
|
||||
extra={
|
||||
"batch_id": self.batch.pk,
|
||||
"node_code": node.node_code,
|
||||
"skill_name": skill_name,
|
||||
"result_message": result.message,
|
||||
},
|
||||
)
|
||||
raise RuntimeError(result.message or f"{node.node_name}执行失败")
|
||||
except Exception as exc:
|
||||
node.status = WorkflowNodeRun.Status.FAILED
|
||||
node.finished_at = timezone.now()
|
||||
node.message = str(exc)
|
||||
node.save(update_fields=["status", "finished_at", "message"])
|
||||
record_event(
|
||||
self.batch,
|
||||
"node_progress",
|
||||
{
|
||||
"node_code": node.node_code,
|
||||
"skill_name": skill_name,
|
||||
"result_message": result.message,
|
||||
"status": node.status,
|
||||
"progress": node.progress,
|
||||
"message": node.message,
|
||||
},
|
||||
)
|
||||
raise RuntimeError(result.message or f"{node.node_name}执行失败")
|
||||
raise
|
||||
|
||||
node.status = WorkflowNodeRun.Status.SUCCESS
|
||||
node.progress = 100
|
||||
@@ -181,7 +219,7 @@ class WorkflowExecutor:
|
||||
record_event(
|
||||
self.batch,
|
||||
"node_progress",
|
||||
{"node_code": node.node_code, "status": node.status, "progress": node.progress},
|
||||
{"node_code": node.node_code, "status": node.status, "progress": node.progress, "message": node.message},
|
||||
)
|
||||
logger.info(
|
||||
"Workflow node finished",
|
||||
|
||||
Reference in New Issue
Block a user