diff --git a/deps/tasks.py b/deps/tasks.py index bf5d2fe..79262cb 100644 --- a/deps/tasks.py +++ b/deps/tasks.py @@ -6,7 +6,7 @@ from conf import settings from deps.logs import logger -from deps.utils import repeat_every +from deps.utils import repeat_every, check_for_internet @asynccontextmanager @@ -20,6 +20,8 @@ async def startup(_app: FastAPI) -> None: @repeat_every(seconds=settings.library_index_refresh_interval, logger=logger) async def refresh_library_index(): """Update the Arduino library index""" + if not check_for_internet(): + return logger.info("Updating library index...") installer = await asyncio.create_subprocess_exec( settings.arduino_cli_path, diff --git a/deps/utils.py b/deps/utils.py index 2412cb0..a43f879 100644 --- a/deps/utils.py +++ b/deps/utils.py @@ -6,6 +6,7 @@ from traceback import format_exception from typing import Any, Callable, Coroutine, Optional, Union +import httpx from starlette.concurrency import run_in_threadpool NoArgsNoReturnFuncT = Callable[[], None] @@ -85,3 +86,16 @@ async def loop() -> None: return wrapped return decorator + + +async def check_for_internet() -> bool: + """Check if internet connection is available""" + client = httpx.AsyncClient() + try: + response = await client.get( + "https://downloads.arduino.cc", headers={"User-Agent": ""} + ) + response.raise_for_status() + except (httpx.RequestError, httpx.HTTPError): + return False + return True diff --git a/main.py b/main.py index 0442491..9dbcc5f 100644 --- a/main.py +++ b/main.py @@ -14,6 +14,7 @@ from deps.logs import logger from deps.session import Session, sessions from deps.tasks import startup +from deps.utils import check_for_internet from models import Sketch, Library, PythonProgram app = FastAPI(lifespan=startup) @@ -32,6 +33,9 @@ async def _install_libraries(libraries: list[Library]) -> None: # Install required libraries + if not check_for_internet(): + logger.warning("No internet connection, skipping library install") + return for library in libraries: if library_cache.get(library): continue diff --git a/requirements.txt b/requirements.txt index 0d529d6..9cf03f4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,3 +6,4 @@ pydantic-settings==2.2.1 cachetools==5.3.3 python-minifier==2.9.0 pyserial==3.5 +httpx==0.27.0