Skip to content

Commit

Permalink
feat: periodically update arduino library index
Browse files Browse the repository at this point in the history
  • Loading branch information
rmoesbergen committed Jan 26, 2024
1 parent 517e294 commit 7df78df
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
1 change: 1 addition & 0 deletions conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Settings(BaseSettings):
code_cache_duration: int = 3600
max_library_caches: int = 50
library_cache_duration: int = 24 * 3600
library_index_refresh_interval: int = 3600

# Max number of concurrent compile tasks
max_concurrent_tasks: int = 10
Expand Down
13 changes: 10 additions & 3 deletions deps/lifespan.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@
from contextlib import asynccontextmanager

from fastapi import FastAPI
from fastapi_utils.tasks import repeat_every

from conf import settings
from deps.logs import logger


@asynccontextmanager
async def lifespan(_app: FastAPI):
"""At startup, update the Arduino library index"""
async def startup(_app: FastAPI) -> None:
""" Startup context manager """
await refresh_library_index()
yield


@repeat_every(seconds=settings.library_index_refresh_interval, logger=logger)
async def refresh_library_index():
"""Update the Arduino library index"""
logger.info("Updating library index...")
installer = await asyncio.create_subprocess_exec(
settings.arduino_cli_path,
Expand All @@ -24,4 +32,3 @@ async def lifespan(_app: FastAPI):
raise EnvironmentError(
f"Failed to update library index: {stderr.decode() + stdout.decode()}"
)
yield
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

from conf import settings
from deps.cache import code_cache, get_code_cache_key, library_cache
from deps.lifespan import lifespan
from deps.lifespan import startup
from deps.logs import logger
from deps.session import Session, sessions
from models import Sketch, Library, PythonProgram

app = FastAPI(lifespan=lifespan)
app = FastAPI(lifespan=startup)
app.add_middleware(
CORSMiddleware,
allow_origins=settings.cors_origins,
Expand Down
2 changes: 1 addition & 1 deletion models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pydantic import BaseModel, Field

# Regex match to (hopefully) prevent weird CLI injection issues
Library = Annotated[str, Field(pattern=r"^[a-zA-Z0-9_ @]*$")]
Library = Annotated[str, Field(pattern=r"^[a-zA-Z0-9_ \.@]*$")]


class Sketch(BaseModel):
Expand Down
7 changes: 4 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
fastapi==0.104.1
fastapi==0.109.0
aiofiles==23.2.1
uvicorn==0.24.0.post1
pydantic==2.5.2
uvicorn==0.27.0
pydantic==2.5.3
pydantic-settings==2.1.0
cachetools==5.3.2
python-minifier==2.9.0
fastapi-utils==0.2.1

0 comments on commit 7df78df

Please sign in to comment.