feat(file-check): 新增文件资产管理与清理功能,支持上传、查询、删除及下载
This commit is contained in:
1 parent
143e054d86
commit
729546a7b7
52 files changed
+3420
-34
No files matched your search
@@ -5,4 +5,4 @@ backend/app/jobs/handlers/__init__.py
|
||||
逻辑:Wave 2 起在 worker 启动前 import 本包。
|
||||
"""
|
||||
|
||||
from app.jobs.handlers import file_check_run, kb_clean, kb_dify_sync, schedule_run, shipping_notice_run, wecom_directory_sync # noqa: F401
|
||||
from app.jobs.handlers import file_check_run, kb_clean, kb_dify_sync, schedule_run, shipping_notice_parse, shipping_notice_run, wecom_directory_sync # noqa: F401
|
||||
@@ -0,0 +1,102 @@
|
||||
"""
|
||||
backend/app/jobs/handlers/shipping_notice_parse.py
|
||||
|
||||
用途:发货通知接入记录异步预解析 handler。
|
||||
依赖:jobs.context、jobs.registry、intake_parse_workflow。
|
||||
逻辑:worker 消费 shipping_notice_parse 队列任务,更新 intake 状态 parsed/parse_failed。
|
||||
领取与写回前均跳过 cancelled/expired/consumed,避免用户取消后被重新解析。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from app import db
|
||||
from app.core.errors import NotFoundError
|
||||
from app.jobs.context import JobContext
|
||||
from app.jobs.queue import fail_job, finish_job
|
||||
from app.jobs.registry import register_handler
|
||||
from app.jobs.types import JobType
|
||||
from app.models import FileAsset, Job, ShippingNoticeIntake
|
||||
from app.services.shipping_notice.intake_parse_workflow import run_intake_parse
|
||||
|
||||
# 终态:不可再领取解析;含用户取消与 TTL 过期
|
||||
_SKIP_CLAIM_STATUSES = frozenset({'parsed', 'consumed', 'cancelled', 'expired'})
|
||||
# 允许领取进入 parsing 的状态(含失败后重试)
|
||||
_CLAIMABLE_STATUSES = frozenset({'created', 'queued', 'parsing', 'parse_failed'})
|
||||
|
||||
|
||||
def _load_asset(asset_id: int) -> FileAsset:
|
||||
asset = db.session.get(FileAsset, asset_id)
|
||||
if not asset or asset.deleted_at is not None:
|
||||
raise NotFoundError(code='FILE_ASSET.NOT_FOUND', message='关联文件资产不存在')
|
||||
return asset
|
||||
|
||||
|
||||
@register_handler(JobType.SHIPPING_NOTICE_PARSE)
|
||||
def run_shipping_notice_parse(ctx: JobContext, payload: dict) -> None:
|
||||
"""
|
||||
消费发货通知预解析任务。
|
||||
Args:
|
||||
ctx: Worker 上下文。
|
||||
payload: 须含 job_id 或 intake_no。
|
||||
"""
|
||||
raw_job_id = payload.get('job_id')
|
||||
job = db.session.get(Job, int(raw_job_id)) if raw_job_id else None
|
||||
intake_no = str(
|
||||
(job.payload_json or {}).get('intake_no') if job else payload.get('intake_no') or ''
|
||||
).strip().upper()
|
||||
if not intake_no:
|
||||
if job:
|
||||
fail_job(job, error='missing intake_no')
|
||||
raise RuntimeError('missing intake_no')
|
||||
|
||||
intake = db.session.query(ShippingNoticeIntake).filter_by(intake_no=intake_no).first()
|
||||
if not intake:
|
||||
if job:
|
||||
fail_job(job, error=f'intake not found: {intake_no}')
|
||||
raise RuntimeError(f'intake not found: {intake_no}')
|
||||
|
||||
# [STEP 1] 领取前跳过终态(含 cancelled/expired)
|
||||
if intake.parse_status in _SKIP_CLAIM_STATUSES:
|
||||
if job:
|
||||
finish_job(job, result={'status': intake.parse_status, 'intake_no': intake_no, 'skipped': True})
|
||||
return
|
||||
if intake.parse_status not in _CLAIMABLE_STATUSES:
|
||||
if job:
|
||||
finish_job(job, result={'status': intake.parse_status, 'intake_no': intake_no, 'skipped': True})
|
||||
return
|
||||
|
||||
intake.parse_status = 'parsing'
|
||||
intake.updated_at = datetime.utcnow()
|
||||
db.session.commit()
|
||||
|
||||
asset = _load_asset(intake.asset_id)
|
||||
try:
|
||||
result = run_intake_parse(intake, asset)
|
||||
if job:
|
||||
refreshed = db.session.get(Job, job.id)
|
||||
if refreshed and refreshed.status == 'running':
|
||||
status = str(result.get('status') or '')
|
||||
if status in {'parsed', 'skipped'}:
|
||||
finish_job(refreshed, result=result, message='发货通知预解析完成' if status == 'parsed' else '接入已取消或过期,跳过')
|
||||
else:
|
||||
fail_job(
|
||||
refreshed,
|
||||
error=str(result.get('error_message') or intake.error_message or 'parse failed'),
|
||||
retryable=False,
|
||||
)
|
||||
except Exception as exc:
|
||||
db.session.rollback()
|
||||
intake = db.session.query(ShippingNoticeIntake).filter_by(intake_no=intake_no).first()
|
||||
# 取消/过期后不再覆盖为 parse_failed
|
||||
if intake and intake.parse_status not in _SKIP_CLAIM_STATUSES:
|
||||
intake.parse_status = 'parse_failed'
|
||||
intake.error_code = 'SHIPPING_NOTICE.INTAKE_PARSE_FAILED'
|
||||
intake.error_message = str(exc)[:1000]
|
||||
db.session.commit()
|
||||
if job:
|
||||
refreshed = db.session.get(Job, job.id)
|
||||
if refreshed:
|
||||
fail_job(refreshed, error=str(exc), retryable=True)
|
||||
raise
|
||||
+32
-12
@@ -10,7 +10,21 @@ from __future__ import annotations
|
||||
from typing import Any
|
||||
|
||||
from app.jobs.types import QUEUE_KB_JOB_TYPES, JobType, handler_type_for_kb_job
|
||||
from app.models import KbJob
|
||||
from app.models import Job, KbJob
|
||||
|
||||
|
||||
def _normalize_generic_job(job: Job) -> dict[str, Any]:
|
||||
"""将通用 Job ORM 实例转为 runner/handler 使用的归一化任务 dict。"""
|
||||
job_type_value = str(job.job_type or '').strip()
|
||||
return {
|
||||
'id': job.id,
|
||||
'job_type': JobType(job_type_value),
|
||||
'raw_job_type': job_type_value,
|
||||
'payload': {
|
||||
'job_id': job.id,
|
||||
**(job.payload_json or {}),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def _normalize_kb_job(job: KbJob) -> dict[str, Any]:
|
||||
@@ -59,7 +73,7 @@ def claim_next(*, queues: list[str] | None = None) -> dict[str, Any] | None:
|
||||
from app.services.knowledge_cleaning.queue_service import recover_stale_running_jobs
|
||||
|
||||
recover_stale_running_jobs()
|
||||
if 'shipping_notice_run' in allowed_types:
|
||||
if 'shipping_notice_run' in allowed_types or 'shipping_notice_parse' in allowed_types:
|
||||
from app.services.shipping_notice.queue_service import recover_stale_shipping_notice_jobs
|
||||
|
||||
recover_stale_shipping_notice_jobs()
|
||||
@@ -77,30 +91,36 @@ def claim_next(*, queues: list[str] | None = None) -> dict[str, Any] | None:
|
||||
|
||||
job = claim_next_shipping_notice_job()
|
||||
if job:
|
||||
return _normalize_kb_job(job)
|
||||
return _normalize_generic_job(job)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def mark_failed(job_id: int, *, message: str) -> None:
|
||||
"""
|
||||
handler 未捕获异常时,将 KbJob 标记为 failed。
|
||||
handler 未捕获异常时,将任务标记为 failed(KbJob 或通用 Job)。
|
||||
Args:
|
||||
job_id: KbJob 主键。
|
||||
job_id: 任务主键。
|
||||
message: 失败原因(截断至 1000 字符)。
|
||||
"""
|
||||
from datetime import datetime
|
||||
|
||||
from app import db
|
||||
from app.jobs.queue import fail_job
|
||||
|
||||
job = db.session.get(KbJob, job_id)
|
||||
if not job or job.status != 'running':
|
||||
job = db.session.get(Job, job_id)
|
||||
if job and job.status == 'running':
|
||||
fail_job(job, error=message[:1000], retryable=True)
|
||||
return
|
||||
|
||||
kb_job = db.session.get(KbJob, job_id)
|
||||
if not kb_job or kb_job.status != 'running':
|
||||
return
|
||||
|
||||
now = datetime.utcnow()
|
||||
job.status = 'failed'
|
||||
job.error_message = message[:1000]
|
||||
job.message = message[:500]
|
||||
job.finished_at = now
|
||||
job.progress = 100
|
||||
kb_job.status = 'failed'
|
||||
kb_job.error_message = message[:1000]
|
||||
kb_job.message = message[:500]
|
||||
kb_job.finished_at = now
|
||||
kb_job.progress = 100
|
||||
db.session.commit()
|
||||
@@ -15,6 +15,7 @@ class JobType(StrEnum):
|
||||
KB_CLEAN = 'kb_clean'
|
||||
KB_DIFY_SYNC = 'kb_dify_sync'
|
||||
SHIPPING_NOTICE_RUN = 'shipping_notice_run'
|
||||
SHIPPING_NOTICE_PARSE = 'shipping_notice_parse'
|
||||
SCHEDULE_RUN = 'schedule_run'
|
||||
FILE_CHECK_RUN = 'file_check_run'
|
||||
WECOM_DIRECTORY_SYNC = 'wecom_directory_sync'
|
||||
@@ -48,7 +49,7 @@ _HANDLER_TO_KB_JOB_TYPE: dict[JobType, str] = {
|
||||
# worker --queues 参数 → 可消费的 KbJob.job_type 集合
|
||||
QUEUE_KB_JOB_TYPES: dict[str, frozenset[str]] = {
|
||||
'kb': frozenset({'clean', 'path_upload', 'sync_dify'}),
|
||||
'shipping': frozenset({'shipping_notice_run'}),
|
||||
'shipping': frozenset({'shipping_notice_run', 'shipping_notice_parse'}),
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user