build: switch services to gunicorn with per-service config

Replace uvicorn with gunicorn + UvicornWorker in backend, runtime,
and schedule Dockerfiles. New gunicorn.conf.py per service exposes
bind/workers/timeout/graceful_timeout as GUNICORN_* env knobs.

Notable details:
- schedule: timeout=0 (disables gunicorn worker heartbeat) so long
  notebook execution isn't killed by gunicorn's silent-worker kill.
- All three configs: drop dead threads=4 setting; UvicornWorker is
  async and ignores threads.
- Pin gunicorn>=26.0.0 in each pyproject; uv.lock regenerated.
- Drop stale 'COPY contracts ./contracts' from runtime and schedule
  Dockerfiles (contracts dir was deleted in an earlier refactor;
  builds would have failed).
- backend Dockerfile: switch to uv sync layout matching runtime/
  schedule; add build deps for native wheels.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
tao.chen
2026-07-31 15:07:45 +08:00
co-authored by Claude
parent 245f4d346d
commit b0f93d976f
10 changed files with 105 additions and 6 deletions
+13 -2
View File
@@ -3,11 +3,22 @@ FROM python:3.12-slim-bookworm
ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1 PYTHONPATH=/app
WORKDIR /app
COPY --from=ghcr.io/astral-sh/uv:latest /uv /bin/uv
RUN apt-get update && apt-get install -y --no-install-recommends \
curl \
ca-certificates \
gcc \
g++ \
python3-dev \
build-essential \
&& rm -rf /var/lib/apt/lists/*
COPY common ./common
COPY backend ./backend
COPY alembic.ini ./
COPY migrations ./migrations
RUN uv pip install --system ./common ./backend
COPY pyproject.toml uv.lock ./
RUN UV_DEFAULT_INDEX="https://pypi.tuna.tsinghua.edu.cn/simple/" uv sync --frozen --no-dev --no-editable --package backend
EXPOSE 8000
CMD ["uvicorn", "backend.main:app", "--host", "0.0.0.0", "--port", "8000"]
CMD ["gunicorn", "--config", "backend/gunicorn.conf.py", "backend.main:app"]
+14
View File
@@ -0,0 +1,14 @@
"""Gunicorn configuration for the backend service.
All values can be overridden with GUNICORN_* environment variables.
"""
import os
bind = os.getenv("GUNICORN_BIND", "0.0.0.0:8000")
workers = int(os.getenv("GUNICORN_WORKERS", "1"))
worker_class = "uvicorn.workers.UvicornWorker"
timeout = int(os.getenv("GUNICORN_TIMEOUT", "120"))
graceful_timeout = int(os.getenv("GUNICORN_GRACEFUL_TIMEOUT", "30"))
accesslog = "-"
errorlog = "-"
+5
View File
@@ -10,11 +10,16 @@ dependencies = [
"croniter==6.2.4",
"alembic==1.18.5",
"cryptography==49.0.0",
"gunicorn>=26.0.0",
]
[tool.uv.sources]
common = { workspace = true }
[[tool.uv.index]]
url = "https://pypi.tuna.tsinghua.edu.cn/simple/"
default = true
[build-system]
requires = ["hatchling"]
+1 -2
View File
@@ -20,9 +20,8 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
COPY pyproject.toml uv.lock ./
COPY common ./common
COPY contracts ./contracts
COPY runtime ./runtime
RUN UV_DEFAULT_INDEX="https://pypi.tuna.tsinghua.edu.cn/simple/" uv sync --frozen --no-dev --no-editable --package runtime
EXPOSE 8000
CMD ["uv", "run", "--frozen", "--package", "runtime", "uvicorn", "runtime.main:app", "--host", "0.0.0.0", "--port", "8000"]
CMD ["uv", "run", "--frozen", "--package", "runtime", "gunicorn", "--config", "runtime/gunicorn.conf.py", "runtime.main:app"]
+15
View File
@@ -0,0 +1,15 @@
"""Gunicorn configuration for the runtime service.
Workers MUST stay at 1: file leases and Jupyter tickets live in
in-memory process state, so a second worker would split that state.
"""
import os
bind = os.getenv("GUNICORN_BIND", "0.0.0.0:8000")
workers = int(os.getenv("GUNICORN_WORKERS", "1"))
worker_class = "uvicorn.workers.UvicornWorker"
timeout = int(os.getenv("GUNICORN_TIMEOUT", "120"))
graceful_timeout = int(os.getenv("GUNICORN_GRACEFUL_TIMEOUT", "30"))
accesslog = "-"
errorlog = "-"
+5
View File
@@ -9,11 +9,16 @@ dependencies = [
"httpx==0.28.1",
"loguru==0.7.2",
"notebook",
"gunicorn>=26.0.0",
]
[tool.uv.sources]
common = { workspace = true }
[[tool.uv.index]]
url = "https://pypi.tuna.tsinghua.edu.cn/simple/"
default = true
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
+12 -2
View File
@@ -2,11 +2,21 @@ FROM python:3.12-slim-bookworm
ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1 PYTHONPATH=/app
WORKDIR /app
RUN apt-get update && apt-get install -y --no-install-recommends \
curl \
ca-certificates \
gcc \
g++ \
python3-dev \
build-essential \
&& rm -rf /var/lib/apt/lists/* \
COPY --from=ghcr.io/astral-sh/uv:latest /uv /bin/uv
COPY pyproject.toml uv.lock ./
COPY common ./common
COPY schedule ./schedule
COPY contracts ./contracts
RUN UV_DEFAULT_INDEX="https://pypi.tuna.tsinghua.edu.cn/simple/" uv sync --frozen --no-dev --no-editable --package schedule
EXPOSE 8000
CMD ["uv", "run", "uvicorn", "schedule.main:app", "--host", "0.0.0.0", "--port", "8000"]
CMD ["uv", "run", "--frozen", "--package", "schedule", "gunicorn", "--config", "schedule/gunicorn.conf.py", "schedule.main:app"]
+17
View File
@@ -0,0 +1,17 @@
"""Gunicorn configuration for the schedule service.
Workers MUST stay at 1: the embedded APScheduler and the Outbox
poller must not run twice in the same container.
"""
import os
bind = os.getenv("GUNICORN_BIND", "0.0.0.0:8000")
workers = int(os.getenv("GUNICORN_WORKERS", "1"))
worker_class = "uvicorn.workers.UvicornWorker"
# 0 disables gunicorn worker heartbeat timeout — needed because
# NodeExecutor runs notebooks synchronously.
timeout = int(os.getenv("GUNICORN_TIMEOUT", "0"))
graceful_timeout = int(os.getenv("GUNICORN_GRACEFUL_TIMEOUT", "30"))
accesslog = "-"
errorlog = "-"
+5
View File
@@ -12,11 +12,16 @@ dependencies = [
"nbclient==0.10.2",
"nbformat==5.10.4",
"ipykernel==6.29.5",
"gunicorn>=26.0.0",
]
[tool.uv.sources]
common = { workspace = true }
[[tool.uv.index]]
url = "https://pypi.tuna.tsinghua.edu.cn/simple/"
default = true
[build-system]
requires = ["hatchling"]
Generated
+18
View File
@@ -198,6 +198,7 @@ dependencies = [
{ name = "croniter" },
{ name = "cryptography" },
{ name = "fastapi" },
{ name = "gunicorn" },
{ name = "httpx" },
{ name = "uvicorn", extra = ["standard"] },
]
@@ -209,6 +210,7 @@ requires-dist = [
{ name = "croniter", specifier = "==6.2.4" },
{ name = "cryptography", specifier = "==49.0.0" },
{ name = "fastapi", specifier = "==0.116.1" },
{ name = "gunicorn", specifier = ">=26.0.0" },
{ name = "httpx", specifier = "==0.28.1" },
{ name = "uvicorn", extras = ["standard"], specifier = "==0.35.0" },
]
@@ -690,6 +692,18 @@ wheels = [
{ url = "https://pypi.tuna.tsinghua.edu.cn/packages/93/e8/65e8707d00fe2a49bf12f609a9b2b39ba6dd23c2810eacad877c4fc94bfe/greenlet-3.5.4-cp315-cp315t-win_arm64.whl", hash = "sha256:08fc36de8442d5c3e95b044550dbea9bf144d31ec0cc58e36fb241cb6ef6a994", size = 250538, upload-time = "2026-07-22T11:40:17.985Z" },
]
[[package]]
name = "gunicorn"
version = "26.0.0"
source = { registry = "https://pypi.tuna.tsinghua.edu.cn/simple/" }
dependencies = [
{ name = "packaging" },
]
sdist = { url = "https://pypi.tuna.tsinghua.edu.cn/packages/6d/b7/a4a3f632f823e432ce6bc65f62961b7980c898c77f075a2f7118cb3846fe/gunicorn-26.0.0.tar.gz", hash = "sha256:ca9346f85e3a4aeeb64d491045c16b9a35647abd37ea15efe53080eb8b090baf", size = 727286, upload-time = "2026-05-05T06:38:25.529Z" }
wheels = [
{ url = "https://pypi.tuna.tsinghua.edu.cn/packages/e6/40/9c2384fc2be4ad25dd4a49decd5ad9ea5a3639814c11bd40ab77cb9f0a14/gunicorn-26.0.0-py3-none-any.whl", hash = "sha256:40233d26a5f0d1872916188c276e21641155111c2853f0c2cd55260aec0d24fc", size = 212009, upload-time = "2026-05-05T06:38:23.007Z" },
]
[[package]]
name = "h11"
version = "0.16.0"
@@ -1864,6 +1878,7 @@ source = { editable = "runtime" }
dependencies = [
{ name = "common" },
{ name = "fastapi" },
{ name = "gunicorn" },
{ name = "httpx" },
{ name = "loguru" },
{ name = "notebook" },
@@ -1874,6 +1889,7 @@ dependencies = [
requires-dist = [
{ name = "common", editable = "common" },
{ name = "fastapi", specifier = "==0.116.1" },
{ name = "gunicorn", specifier = ">=26.0.0" },
{ name = "httpx", specifier = "==0.28.1" },
{ name = "loguru", specifier = "==0.7.2" },
{ name = "notebook" },
@@ -1900,6 +1916,7 @@ dependencies = [
{ name = "apscheduler" },
{ name = "common" },
{ name = "fastapi" },
{ name = "gunicorn" },
{ name = "httpx" },
{ name = "ipykernel" },
{ name = "nbclient" },
@@ -1913,6 +1930,7 @@ requires-dist = [
{ name = "apscheduler", specifier = "==3.11.3" },
{ name = "common", editable = "common" },
{ name = "fastapi", specifier = "==0.116.1" },
{ name = "gunicorn", specifier = ">=26.0.0" },
{ name = "httpx", specifier = "==0.28.1" },
{ name = "ipykernel", specifier = "==6.29.5" },
{ name = "nbclient", specifier = "==0.10.2" },