Skip to content

Commit

Permalink
add background task to prune instances
Browse files Browse the repository at this point in the history
  • Loading branch information
matyson committed Apr 26, 2024
1 parent 47000fd commit 7f49fdb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Simple api to manage tensorboard instances on demand.
- [x] setup reverse proxy with dynamic paths to tensorboards ports
- [x] setup ssl
- [x] clean up idle tensorboards
- [x] background tasks to clean up tensorboards
- [x] security token on api routes
- [] secure tensorboard instances ?
- [] test connection w/ frontend
Expand Down
24 changes: 16 additions & 8 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from typing import Annotated

from fastapi import FastAPI, Depends, Body, HTTPException
from contextlib import asynccontextmanager
from apscheduler.schedulers.background import BackgroundScheduler


from .models import CreateTensorboardInstanceRequest, TensorboardInstance
from .dependencies import verify_token, config
Expand All @@ -10,10 +13,21 @@

hostname = config.hostname
ttl = config.board_ttl
get, get_all, set, remove, contains = create_mobius(ttl)
get, get_all, set, remove, contains, prune = create_mobius(ttl)


@asynccontextmanager
async def lifespan(_: FastAPI):
scheduler = BackgroundScheduler()
scheduler.add_job(
id="mobius.m.mobius", func=prune, trigger="interval", seconds=2 * ttl
)
scheduler.start()
yield
scheduler.shutdown()


app = FastAPI(root_path="/api", dependencies=[Depends(verify_token)])
app = FastAPI(root_path="/api", dependencies=[Depends(verify_token)], lifespan=lifespan)


@app.get("/")
Expand Down Expand Up @@ -58,9 +72,3 @@ def kill_tensorboard_instance(name: str):
@app.get("/tensorboard/instances")
def get_tensorboard_instances():
return get_all()


if __name__ == "__main__":
import uvicorn

uvicorn.run(app, host="0.0.0.0", port=8000)
2 changes: 1 addition & 1 deletion app/tva.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ def contains(loki: str):
except KeyError:
return False

return get, get_all, set, remove, contains
return get, get_all, set, remove, contains, prune
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ fastapi
uvicorn
pydantic
pydantic-settings
tensorboard
tensorboard
apscheduler

0 comments on commit 7f49fdb

Please sign in to comment.