feat(rohs-check): 新增 RoHS 检核工具 API 及相关功能
This commit is contained in:
1 parent
8a541211b7
commit
24551e9a9a
39 files changed
+3128
-189
No files matched your search
@@ -12,6 +12,7 @@ import requests
|
||||
|
||||
from app import db
|
||||
from app.models import KbDifySyncRecord, KbDocumentVersion, KbGroup
|
||||
from app.services.db_sequence import add_and_flush_with_sequence_retry
|
||||
from app.services.knowledge_cleaning.settings_service import get_kb_dify_settings
|
||||
|
||||
|
||||
@@ -257,7 +258,7 @@ def sync_document_version_to_dify(document_version_id: int, synced_by_id: int |
|
||||
sync_hash=sync_hash,
|
||||
synced_by_id=synced_by_id,
|
||||
)
|
||||
db.session.add(record)
|
||||
add_and_flush_with_sequence_retry(record, "kb_dify_sync_records")
|
||||
else:
|
||||
record.dataset_id = dataset_id
|
||||
record.dataset_name = dataset_name
|
||||
|
||||
@@ -12,7 +12,7 @@ import zipfile
|
||||
|
||||
from flask import current_app
|
||||
from app import db
|
||||
from app.models import KbDocument, KbDocumentVersion, KbJob
|
||||
from app.models import KbDocument, KbDocumentVersion, KbJob, ScheduleRun
|
||||
from app.services.knowledge_cleaning.edkb_adapter import build_clean_result
|
||||
from app.services.knowledge_cleaning.dify_sync_service import (
|
||||
KbDifySyncError,
|
||||
@@ -25,6 +25,26 @@ def _utc_now() -> datetime:
|
||||
return datetime.utcnow()
|
||||
|
||||
|
||||
def _schedule_run_id_from_job(job: KbJob | None) -> int | None:
|
||||
payload = dict(getattr(job, "payload_json", None) or {})
|
||||
raw_run_id = payload.get("schedule_run_id")
|
||||
if raw_run_id in (None, "", 0, "0"):
|
||||
return None
|
||||
try:
|
||||
return int(raw_run_id)
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
|
||||
|
||||
def _touch_schedule_run_kb_sync(job: KbJob | None, *, status: str, error: str | None = None) -> None:
|
||||
run_id = _schedule_run_id_from_job(job)
|
||||
if not run_id:
|
||||
return
|
||||
from app.services.schedule.kb_sync import update_schedule_run_kb_sync_status
|
||||
|
||||
update_schedule_run_kb_sync_status(run_id, status=status, error=error)
|
||||
|
||||
|
||||
def _infer_missing_extension(source_path: Path) -> str:
|
||||
if zipfile.is_zipfile(source_path):
|
||||
try:
|
||||
@@ -398,6 +418,24 @@ def run_path_upload_job(job_id: int) -> tuple[bool, str]:
|
||||
db.session.commit()
|
||||
return False, 'partial_failed'
|
||||
|
||||
sync_job_id = None
|
||||
payload = dict(job.payload_json or {})
|
||||
if payload.get('auto_sync_dify'):
|
||||
sync_job = KbJob(
|
||||
job_type='sync_dify',
|
||||
status='queued',
|
||||
group_id=job.group_id,
|
||||
document_id=job.document_id,
|
||||
document_version_id=job.document_version_id,
|
||||
progress=0,
|
||||
message='等待同步到 Dify',
|
||||
payload_json={'document_version_id': job.document_version_id, 'source_job_id': job.id},
|
||||
created_by_id=job.created_by_id,
|
||||
)
|
||||
db.session.add(sync_job)
|
||||
db.session.flush()
|
||||
sync_job_id = sync_job.id
|
||||
|
||||
job.status = 'success'
|
||||
job.message = f'路径上传完成,成功 {success_count} 条'
|
||||
job.error_message = None
|
||||
@@ -432,6 +470,7 @@ def run_clean_job(job_id: int) -> tuple[bool, str]:
|
||||
job.error_message = '关联文档版本不存在'
|
||||
job.finished_at = _utc_now()
|
||||
db.session.commit()
|
||||
_touch_schedule_run_kb_sync(job, status='failed', error='关联文档版本不存在')
|
||||
return False, '关联文档版本不存在'
|
||||
|
||||
source_path = Path(version.source_file_path)
|
||||
@@ -442,6 +481,7 @@ def run_clean_job(job_id: int) -> tuple[bool, str]:
|
||||
version.error_message = '原始文件不存在,无法执行 clean'
|
||||
job.finished_at = _utc_now()
|
||||
db.session.commit()
|
||||
_touch_schedule_run_kb_sync(job, status='failed', error='原始文件不存在,无法执行 clean')
|
||||
return False, '原始文件不存在'
|
||||
|
||||
job.status = 'running'
|
||||
@@ -450,6 +490,7 @@ def run_clean_job(job_id: int) -> tuple[bool, str]:
|
||||
job.started_at = _utc_now()
|
||||
version.status = 'processing'
|
||||
db.session.commit()
|
||||
_touch_schedule_run_kb_sync(job, status='cleaning')
|
||||
|
||||
target_dir = source_path.parent
|
||||
warnings = []
|
||||
@@ -479,6 +520,7 @@ def run_clean_job(job_id: int) -> tuple[bool, str]:
|
||||
version.status = 'failed'
|
||||
version.error_message = str(exc)
|
||||
db.session.commit()
|
||||
_touch_schedule_run_kb_sync(job, status='failed', error=str(exc))
|
||||
return False, 'timeout'
|
||||
|
||||
preview_path = target_dir / f'{_normalized_source_filename(version, source_path)}.preview.txt'
|
||||
@@ -516,8 +558,42 @@ def run_clean_job(job_id: int) -> tuple[bool, str]:
|
||||
job.error_message = None
|
||||
job.result_json = result
|
||||
job.finished_at = _utc_now()
|
||||
|
||||
sync_job_id = None
|
||||
payload = dict(job.payload_json or {})
|
||||
if payload.get('auto_sync_dify'):
|
||||
sync_job = KbJob(
|
||||
job_type='sync_dify',
|
||||
status='queued',
|
||||
group_id=job.group_id,
|
||||
document_id=job.document_id,
|
||||
document_version_id=job.document_version_id,
|
||||
progress=0,
|
||||
message='等待同步到 Dify',
|
||||
payload_json={
|
||||
'document_version_id': job.document_version_id,
|
||||
'source_job_id': job.id,
|
||||
'schedule_run_id': payload.get('schedule_run_id'),
|
||||
},
|
||||
created_by_id=job.created_by_id,
|
||||
)
|
||||
db.session.add(sync_job)
|
||||
db.session.flush()
|
||||
sync_job_id = sync_job.id
|
||||
run_id = _schedule_run_id_from_job(job)
|
||||
if run_id:
|
||||
run = db.session.get(ScheduleRun, run_id)
|
||||
if run:
|
||||
run.kb_sync_job_id = sync_job_id
|
||||
run.kb_sync_status = 'syncing'
|
||||
|
||||
db.session.commit()
|
||||
|
||||
if sync_job_id:
|
||||
from app.services.knowledge_cleaning.queue_service import enqueue_kb_job
|
||||
|
||||
enqueue_kb_job(sync_job_id)
|
||||
|
||||
return True, 'ok'
|
||||
|
||||
|
||||
@@ -552,6 +628,7 @@ def run_sync_job(job_id: int) -> tuple[bool, str]:
|
||||
job.message = '同步到 Dify 失败'
|
||||
job.finished_at = _utc_now()
|
||||
db.session.commit()
|
||||
_touch_schedule_run_kb_sync(job, status='failed', error=str(exc))
|
||||
return False, str(exc)
|
||||
except Exception as exc:
|
||||
version.status = 'ready'
|
||||
@@ -561,6 +638,7 @@ def run_sync_job(job_id: int) -> tuple[bool, str]:
|
||||
job.message = '同步到 Dify 失败'
|
||||
job.finished_at = _utc_now()
|
||||
db.session.commit()
|
||||
_touch_schedule_run_kb_sync(job, status='failed', error=str(exc))
|
||||
return False, str(exc)
|
||||
|
||||
version.status = 'synced'
|
||||
@@ -571,4 +649,5 @@ def run_sync_job(job_id: int) -> tuple[bool, str]:
|
||||
job.result_json = result
|
||||
job.finished_at = _utc_now()
|
||||
db.session.commit()
|
||||
_touch_schedule_run_kb_sync(job, status='success')
|
||||
return True, 'ok'
|
||||
Reference in new issue
Block a user