Skip to content

Add back health check to ephemeral_server #277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ jobs:
uses: ./.github/actions/setup-backend
- name: Run ATS and Tests
uses: ./.github/actions/run_ats
timeout-minutes: 5
timeout-minutes: 15
with:
default_tests: "tests/unit"
codecov_static_token: ${{ secrets.CODECOV_STATIC_TOKEN }}
codecov_token: ${{ secrets.CODECOV_TOKEN }}
collect_args: "--timeout 5"
collect_args: "--timeout 15"
codecov_flags: unit-tests
codemod-tests:
# changing the following value will significantly affect github's cost. Be careful and consult with the team before changing it.
Expand All @@ -53,7 +53,7 @@ jobs:
uses: ./.github/actions/setup-oss-repos
- name: Run ATS and Tests
uses: ./.github/actions/run_ats
timeout-minutes: 10
timeout-minutes: 15
with:
default_tests: "tests/integration/codemod/test_codemods.py"
codecov_static_token: ${{ secrets.CODECOV_STATIC_TOKEN }}
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ dependencies = [
"starlette<1.0.0,>=0.16.0",
"tqdm>=4.67.1",
"tomlkit>=0.13.2",
"uvicorn[standard]>=0.30.0",
]

license = { text = "Apache-2.0" }
Expand Down Expand Up @@ -138,7 +139,7 @@ dev-dependencies = [
"pytest-benchmark[histogram]>=5.1.0",
"loguru>=0.7.3",
]
#skeyring-provider = "subprocess"


[tool.uv.workspace]
members = []
Expand Down
4 changes: 1 addition & 3 deletions src/codegen/runner/models/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@


class ServerInfo(BaseModel):
repo_id: int = 0
container_id: str = ""
repo_name: str | None = None
is_running_codemod: bool = False
is_shutting_down: bool = False
warmup_state: WarmupState = WarmupState.PENDING
label: str | None = ""


class UtilizationMetrics(BaseModel):
Expand Down
30 changes: 29 additions & 1 deletion src/codegen/runner/sandbox/ephemeral_server.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,46 @@
import logging
import tempfile
from contextlib import asynccontextmanager

from fastapi import FastAPI

from codegen.runner.enums.warmup_state import WarmupState
from codegen.runner.models.apis import (
RUN_ON_STRING_ENDPOINT,
GetRunOnStringRequest,
GetRunOnStringResult,
ServerInfo,
)
from codegen.runner.sandbox.executor import SandboxExecutor
from codegen.sdk.codebase.factory.get_session import get_codebase_session
from codegen.sdk.enums import ProgrammingLanguage
from codegen.shared.compilation.string_to_code import create_execute_function_from_codeblock

logger = logging.getLogger(__name__)
app = FastAPI()

server_info: ServerInfo


@asynccontextmanager
async def lifespan(server: FastAPI):
global server_info
server_info = ServerInfo(warmup_state=WarmupState.COMPLETED)
logger.info("Ephemeral server is ready to accept requests")
yield
logger.info("Shutting down fastapi server")


app = FastAPI(lifespan=lifespan)


@app.get("/")
def health() -> ServerInfo:
return server_info


@app.post(RUN_ON_STRING_ENDPOINT)
async def run_on_string(request: GetRunOnStringRequest) -> GetRunOnStringResult:
server_info.is_running_codemod = True
logger.info(f"====[ run_on_string ]====\n> Codemod source: {request.codemod_source}\n> Input: {request.files}\n> Language: {request.language}\n")
language = ProgrammingLanguage(request.language.upper())
with get_codebase_session(tmpdir=tempfile.mkdtemp(), files=request.files, programming_language=language) as codebase:
Expand All @@ -27,3 +49,9 @@ async def run_on_string(request: GetRunOnStringRequest) -> GetRunOnStringResult:
result = await executor.execute(code_to_exec)
logger.info(f"Result: {result}")
return GetRunOnStringResult(result=result)


if __name__ == "__main__":
import uvicorn

uvicorn.run(app, host="0.0.0.0", port=8000)
6 changes: 3 additions & 3 deletions src/codegen/runner/sandbox/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import psutil
from fastapi import FastAPI

from codegen.runner.constants.envvars import CUSTOMER_REPO_ID
from codegen.runner.enums.warmup_state import WarmupState
from codegen.runner.models.apis import (
BRANCH_ENDPOINT,
Expand Down Expand Up @@ -38,10 +37,11 @@ async def lifespan(server: FastAPI):
global runner

try:
server_info = ServerInfo(repo_id=int(os.getenv(CUSTOMER_REPO_ID)), container_id=os.getenv("MODAL_TASK_ID"))
repo_config = get_repo_config()
server_info = ServerInfo(repo_name=repo_config.full_name)
logger.info(f"Starting up sandbox fastapi server for repo_id={server_info.repo_id} in container ID={server_info.container_id}")

runner = SandboxRunner(container_id=server_info.container_id, repo_config=get_repo_config())
runner = SandboxRunner(container_id=server_info.container_id, repo_config=repo_config)
server_info.warmup_state = WarmupState.PENDING
await runner.warmup()
server_info.warmup_state = WarmupState.COMPLETED
Expand Down
2 changes: 2 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading