fix(knowledge-base): 优化知识库页面,重构后端job队列
This commit is contained in:
1 parent
7e4a2037d8
commit
b476b58a91
38 files changed
+992
-428
No files matched your search
@@ -0,0 +1,61 @@
|
||||
"""
|
||||
tests/jobs/test_store.py
|
||||
|
||||
用途:验证 store.claim_next 归一化与空队列行为。
|
||||
"""
|
||||
from app.jobs.store import _normalize_kb_job
|
||||
from app.jobs.types import JobType
|
||||
from app.models import KbJob
|
||||
|
||||
|
||||
def test_normalize_kb_job_clean(app):
|
||||
with app.app_context():
|
||||
job = KbJob(
|
||||
job_type='clean',
|
||||
status='running',
|
||||
progress=1,
|
||||
payload_json={'document_version_id': 42},
|
||||
)
|
||||
normalized = _normalize_kb_job(job)
|
||||
assert normalized['id'] == job.id
|
||||
assert normalized['job_type'] == JobType.KB_CLEAN
|
||||
assert normalized['raw_job_type'] == 'clean'
|
||||
assert normalized['payload']['job_id'] == job.id
|
||||
assert normalized['payload']['document_version_id'] == 42
|
||||
|
||||
|
||||
def test_normalize_kb_job_sync_dify(app):
|
||||
with app.app_context():
|
||||
job = KbJob(job_type='sync_dify', status='running', progress=1)
|
||||
normalized = _normalize_kb_job(job)
|
||||
assert normalized['job_type'] == JobType.KB_DIFY_SYNC
|
||||
|
||||
|
||||
def test_claim_next_returns_none_when_empty(app):
|
||||
from app.jobs import store
|
||||
|
||||
with app.app_context():
|
||||
assert store.claim_next(queues=['kb']) is None
|
||||
|
||||
|
||||
def test_find_shipping_notice_job_id_matches_task_no(app):
|
||||
from app import db
|
||||
from app.jobs.queue import _find_shipping_notice_job_id
|
||||
|
||||
with app.app_context():
|
||||
target = KbJob(
|
||||
job_type=JobType.SHIPPING_NOTICE_RUN.value,
|
||||
status='queued',
|
||||
progress=0,
|
||||
payload_json={'task_no': 'SHIP-A'},
|
||||
)
|
||||
newer_other = KbJob(
|
||||
job_type=JobType.SHIPPING_NOTICE_RUN.value,
|
||||
status='queued',
|
||||
progress=0,
|
||||
payload_json={'task_no': 'SHIP-B'},
|
||||
)
|
||||
db.session.add_all([target, newer_other])
|
||||
db.session.commit()
|
||||
|
||||
assert _find_shipping_notice_job_id('ship-a') == target.id
|
||||
@@ -0,0 +1,20 @@
|
||||
"""
|
||||
tests/jobs/test_types.py
|
||||
|
||||
用途:验证 JobType 与 KbJob.job_type 映射。
|
||||
"""
|
||||
from app.jobs.types import JobType, handler_type_for_kb_job, kb_job_type_for_handler
|
||||
|
||||
|
||||
def test_handler_type_for_kb_job_mapping():
|
||||
assert handler_type_for_kb_job('clean') == JobType.KB_CLEAN
|
||||
assert handler_type_for_kb_job('path_upload') == JobType.KB_CLEAN
|
||||
assert handler_type_for_kb_job('sync_dify') == JobType.KB_DIFY_SYNC
|
||||
assert handler_type_for_kb_job('shipping_notice_run') == JobType.SHIPPING_NOTICE_RUN
|
||||
assert handler_type_for_kb_job('unknown') is None
|
||||
|
||||
|
||||
def test_kb_job_type_for_handler_mapping():
|
||||
assert kb_job_type_for_handler(JobType.KB_CLEAN) == 'clean'
|
||||
assert kb_job_type_for_handler(JobType.KB_DIFY_SYNC) == 'sync_dify'
|
||||
assert kb_job_type_for_handler(JobType.SHIPPING_NOTICE_RUN) == 'shipping_notice_run'
|
||||
@@ -0,0 +1,35 @@
|
||||
"""
|
||||
tests/jobs/test_worker_kb.py
|
||||
|
||||
用途:验证 KB worker 兼容入口读取 Flask config 中的并发配置。
|
||||
"""
|
||||
|
||||
|
||||
def test_main_kb_uses_app_config_concurrency(monkeypatch):
|
||||
import worker_kb
|
||||
|
||||
started: list[str] = []
|
||||
|
||||
class FakeApp:
|
||||
config = {'KB_WORKER_CONCURRENCY': 3}
|
||||
|
||||
class FakeProcess:
|
||||
def __init__(self, *, target, args, name):
|
||||
self.name = name
|
||||
self.target = target
|
||||
self.args = args
|
||||
|
||||
def start(self):
|
||||
started.append(self.name)
|
||||
|
||||
def join(self, timeout=None):
|
||||
return None
|
||||
|
||||
monkeypatch.setattr(worker_kb, 'create_app', lambda _config_name: FakeApp())
|
||||
monkeypatch.setattr(worker_kb, '_resolve_config_name', lambda: 'production')
|
||||
monkeypatch.setattr(worker_kb.mp, 'Process', FakeProcess)
|
||||
|
||||
worker_kb.main_kb()
|
||||
|
||||
# 断言多进程数量来自 app.config,而非直接读取环境变量默认值。
|
||||
assert started == ['kb-worker-1', 'kb-worker-2', 'kb-worker-3']
|
||||
Reference in new issue
Block a user