76 lines
2.3 KiB
Python
76 lines
2.3 KiB
Python
"""Запуск фоновой проверки обновлений (без автоустановки) при старте процесса."""
|
||
|
||
from __future__ import annotations
|
||
|
||
import atexit
|
||
import logging
|
||
import threading
|
||
import time
|
||
from typing import Any, Optional
|
||
|
||
log = logging.getLogger(__name__)
|
||
|
||
_started = False
|
||
|
||
|
||
def get_auto_updater() -> Any:
|
||
"""Singleton AutoUpdater или None, если проверка не инициализирована."""
|
||
try:
|
||
from update import auto_updater
|
||
|
||
return auto_updater
|
||
except Exception:
|
||
return None
|
||
|
||
|
||
def schedule_update_checker(app, *, delay_sec: Optional[float] = None) -> None:
|
||
"""Стартует фоновый цикл check_for_updates, если включено в recipes.db."""
|
||
global _started
|
||
if _started or app.config.get("TESTING"):
|
||
return
|
||
_started = True
|
||
|
||
if delay_sec is None:
|
||
from app.services.startup_defer import default_startup_delay_sec
|
||
|
||
delay_sec = default_startup_delay_sec(
|
||
"WESP_STARTUP_UPDATE_CHECK_DELAY_SEC", arm_default=20.0, other_default=5.0
|
||
)
|
||
|
||
def _run() -> None:
|
||
sec = max(0.0, float(delay_sec or 0))
|
||
if sec > 0:
|
||
log.info("Проверка обновлений: старт через %.1f с", sec)
|
||
time.sleep(sec)
|
||
try:
|
||
from update import init_auto_updater
|
||
|
||
init_auto_updater()
|
||
except Exception:
|
||
log.exception("Не удалось инициализировать проверку обновлений")
|
||
|
||
threading.Thread(target=_run, daemon=True, name="wesp-update-check-defer").start()
|
||
|
||
|
||
def reload_update_checker() -> None:
|
||
"""Перечитать настройки из recipes.db без перезапуска процесса."""
|
||
try:
|
||
from update import init_auto_updater
|
||
|
||
init_auto_updater(reload=True)
|
||
except Exception:
|
||
log.exception("Не удалось перезагрузить проверку обновлений")
|
||
|
||
|
||
def stop_update_checker() -> None:
|
||
try:
|
||
from update import stop_auto_updater
|
||
|
||
stop_auto_updater()
|
||
except Exception:
|
||
log.debug("stop_update_checker: ошибка остановки", exc_info=True)
|
||
|
||
|
||
def register_update_checker_shutdown() -> None:
|
||
atexit.register(stop_update_checker)
|