feat: 管理后台可动态调整采集和导出的并行数量
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
import shutil
|
||||
import time
|
||||
@@ -16,7 +17,7 @@ from app.collect.io import parse_csv, write_csv # noqa: E402
|
||||
from app.db import reset_engine # noqa: E402
|
||||
from app.main import create_app # noqa: E402
|
||||
from app.nqi.client import needs_captcha, rsa_encrypt # noqa: E402
|
||||
from app.tasks.runner import check_period # noqa: E402
|
||||
from app.tasks.runner import check_concurrency, check_period # noqa: E402
|
||||
|
||||
PUBLIC_KEY = (
|
||||
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCwjDm1HXDw8QH5ZtGMQIl2h/I8E+"
|
||||
@@ -92,3 +93,58 @@ def test_local_login_task_and_admin(client: TestClient):
|
||||
downloaded = client.get(f"/api/tasks/{task_id}/file", headers=user_headers)
|
||||
assert downloaded.status_code == 200
|
||||
assert "小区" in downloaded.content.decode("utf-8-sig")
|
||||
|
||||
|
||||
def test_concurrency_bounds():
|
||||
assert check_concurrency(1) == 1
|
||||
assert check_concurrency(32) == 32
|
||||
with pytest.raises(ValueError):
|
||||
check_concurrency(0)
|
||||
with pytest.raises(ValueError):
|
||||
check_concurrency(33)
|
||||
|
||||
|
||||
def test_admin_can_set_concurrency(client: TestClient):
|
||||
logged = client.post("/api/auth/local", json={"username": "admin", "password": "admin-pass"})
|
||||
headers = {"Authorization": f"Bearer {logged.json()['token']}"}
|
||||
current = client.get("/api/admin/settings", headers=headers)
|
||||
assert current.json()["workerConcurrency"] == 4
|
||||
saved = client.put(
|
||||
"/api/admin/settings",
|
||||
json={"loginMode": "local", "credentialRefreshSeconds": 300, "workerConcurrency": 8},
|
||||
headers=headers,
|
||||
)
|
||||
assert saved.status_code == 200
|
||||
assert saved.json()["workerConcurrency"] == 8
|
||||
rejected = client.put(
|
||||
"/api/admin/settings",
|
||||
json={"loginMode": "local", "credentialRefreshSeconds": 300, "workerConcurrency": 0},
|
||||
headers=headers,
|
||||
)
|
||||
assert rejected.status_code == 400
|
||||
|
||||
|
||||
def test_dynamic_gate_shrinks_after_running_tasks_finish():
|
||||
from app.tasks.queue import DynamicGate
|
||||
|
||||
async def scenario() -> None:
|
||||
gate = DynamicGate(1)
|
||||
await gate.acquire()
|
||||
waiting = asyncio.create_task(gate.acquire())
|
||||
await asyncio.sleep(0.05)
|
||||
assert not waiting.done()
|
||||
gate.set_limit(2)
|
||||
await asyncio.wait_for(waiting, timeout=1)
|
||||
assert gate.held == 2
|
||||
gate.set_limit(1)
|
||||
blocked = asyncio.create_task(gate.acquire())
|
||||
await asyncio.sleep(0.05)
|
||||
assert not blocked.done()
|
||||
gate.release()
|
||||
await asyncio.sleep(0.05)
|
||||
assert not blocked.done()
|
||||
gate.release()
|
||||
await asyncio.wait_for(blocked, timeout=1)
|
||||
assert gate.held == 1
|
||||
|
||||
asyncio.run(scenario())
|
||||
|
||||
Reference in New Issue
Block a user