Остаток п. 9 ревью: синхронная БД вне event loop, повторное ревью
Повторное ревью кодовой базы: docs/reviews/2026-09-28-1243-codebase-review.md (статус 12 замечаний, новые замечания 13–16). Остаток п. 9 (docs/changes/022): - 14 async-функций API, UI и сервисов больше не обращаются к SQLite напрямую — через asyncio.to_thread; jobs.start_jobs стал async (БД в потоке, create_task в event loop); ops._conn для подключения к устройству; - тест-линтер по AST: в async def нет прямых вызовов функций с session_scope — защита от регресса. Тесты: 29 из 29. Стенд: задачи и актор событий в порядке, параллельные запросы не ждут медленного устройства, боевые данные не изменены. Ручная проверка UI пользователем на момент коммита не подтверждена. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c4079724c5
commit
0bda0038d0
10 files changed
+320
-39
No files matched your search
+11
-7
@@ -29,9 +29,13 @@ def _save_status(device_id: str, fields: dict) -> None:
|
||||
data={"error": (fields.get("last_error") or "")[:300]} if not up else None, s=s)
|
||||
|
||||
|
||||
async def _conn(device_id: str) -> devices.Conn:
|
||||
return await asyncio.to_thread(devices.get_conn, device_id)
|
||||
|
||||
|
||||
async def refresh_status(device_id: str) -> None:
|
||||
"""Полный опрос: статус, версии и проверка обновлений. Недоступность — не исключение."""
|
||||
conn = await asyncio.to_thread(devices.get_conn, device_id)
|
||||
conn = await _conn(device_id)
|
||||
try:
|
||||
async with devices.open_client(conn) as c:
|
||||
status = await ros.get_status(c)
|
||||
@@ -49,7 +53,7 @@ async def poll_device(device_id: str, full: bool = False) -> None:
|
||||
if full or d.online is not True or not d.status:
|
||||
return await refresh_status(device_id)
|
||||
try:
|
||||
conn = await asyncio.to_thread(devices.get_conn, device_id)
|
||||
conn = await _conn(device_id)
|
||||
async with devices.open_client(conn) as c:
|
||||
res = await c.get("system/resource")
|
||||
except RosError as e:
|
||||
@@ -59,7 +63,7 @@ async def poll_device(device_id: str, full: bool = False) -> None:
|
||||
|
||||
|
||||
async def refresh_many(device_ids: list[str] | None = None) -> None:
|
||||
targets = device_ids if device_ids is not None else [d.id for d in devices.list_devices()]
|
||||
targets = device_ids if device_ids is not None else [d.id for d in await asyncio.to_thread(devices.list_devices)]
|
||||
sem = asyncio.Semaphore(get_settings().max_concurrency)
|
||||
|
||||
async def one(i: str):
|
||||
@@ -72,7 +76,7 @@ async def refresh_many(device_ids: list[str] | None = None) -> None:
|
||||
async def run_backup(device_id: str) -> str:
|
||||
"""Бэкап .backup + .rsc: создать на устройстве → скачать через REST во временную папку →
|
||||
загрузить в S3 от имени сервера → удалить файлы с устройства."""
|
||||
conn = devices.get_conn(device_id)
|
||||
conn = await _conn(device_id)
|
||||
stamp = datetime.now(timezone.utc).strftime("%Y%m%d-%H%M%S")
|
||||
base = f"ros_control-{stamp}"
|
||||
files = { # имя на устройстве -> ключ в S3
|
||||
@@ -131,7 +135,7 @@ async def run_backup(device_id: str) -> str:
|
||||
|
||||
|
||||
async def set_channel(device_id: str, channel: str) -> None:
|
||||
async with devices.open_client(devices.get_conn(device_id)) as c:
|
||||
async with devices.open_client(await _conn(device_id)) as c:
|
||||
await ros.set_channel(c, channel)
|
||||
await refresh_status(device_id)
|
||||
|
||||
@@ -143,10 +147,10 @@ async def run_set_channel(device_id: str, channel: str) -> str:
|
||||
|
||||
|
||||
async def run_ros_update(device_id: str) -> str:
|
||||
async with devices.open_client(devices.get_conn(device_id)) as c:
|
||||
async with devices.open_client(await _conn(device_id)) as c:
|
||||
return await ros.install_ros_update(c)
|
||||
|
||||
|
||||
async def run_fw_update(device_id: str) -> str:
|
||||
async with devices.open_client(devices.get_conn(device_id)) as c:
|
||||
async with devices.open_client(await _conn(device_id)) as c:
|
||||
return await ros.upgrade_firmware(c)
|
||||
Reference in new issue
Block a user