Skip to content

Commit

Permalink
Merge pull request #256 from flatironinstitute/restartable-server
Browse files Browse the repository at this point in the history
Add 'restart' endpoint to server for deploy automation
  • Loading branch information
WardBrian authored Dec 19, 2024
2 parents f4c4684 + 1e724b3 commit ad6ea03
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/docker_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ jobs:
TAG=${INPUT_TAG:-"latest"}
docker build -t ghcr.io/flatironinstitute/stan-wasm-server:$TAG .
docker push ghcr.io/flatironinstitute/stan-wasm-server:$TAG
- Name: Ping public server to restart
run: |
curl -X POST https://stan-wasm.flatironinstitute.org/restart -H "Authorization: Bearer ${{ secrets.PUBLIC_SERVER_RESTART_TOKEN }}"
2 changes: 1 addition & 1 deletion backend/stan-wasm-server/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

set -ex

uvicorn --app-dir ./src/app main:app --host 0.0.0.0 --port 8080 --workers 4
uvicorn --app-dir ./src/app main:app --host 0.0.0.0 --port 8080 --workers 4 --timeout-graceful-shutdown 20
3 changes: 2 additions & 1 deletion backend/stan-wasm-server/src/app/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
from functools import lru_cache
from pathlib import Path
from typing import Annotated, Literal
from typing import Annotated, Literal, Optional

from pydantic import (
AliasChoices,
Expand All @@ -23,6 +23,7 @@ class StanWasmServerSettings(BaseSettings):
model_config = SettingsConfigDict(env_prefix="SWS_")

passcode: SecretStr
restart_token: Optional[SecretStr] = None
job_dir: Path = Path("/jobs")
built_model_dir: Path = Path("/compiled_models")
compilation_timeout: PositiveInt = 60 * 5
Expand Down
17 changes: 17 additions & 0 deletions backend/stan-wasm-server/src/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,20 @@ async def run_job(job_id: str, settings: DependsOnSettings) -> DictResponse:
)

return {"success": True}


@app.post("/restart")
async def restart(
settings: DependsOnSettings, authorization: str = Header(None)
) -> None:
if settings.restart_token is None:
raise StanPlaygroundAuthenticationException("Restart token not set at startup")
check_authorization(authorization, settings.restart_token)

import os
import signal

# send an interrupt signal to the parent process
# uvicorn interprets this like Ctrl-C, and gracefully shuts down
os.kill(os.getppid(), signal.SIGINT)
# actual restart is handled by the orchestrator

0 comments on commit ad6ea03

Please sign in to comment.