mirror of
https://github.com/tiennm99/DocsGPT.git
synced 2026-10-11 12:11:45 +00:00
The backend import package is now docsgpt, the name it will carry on PyPI; application was far too generic to install into anyone's site-packages. git mv plus a mechanical rewrite of every import, dotted string and path reference: 734 Python files, the compose files, Dockerfile, workflows, docs, setup scripts, devcontainer, k8s manifests, vscode config, pytest and coverage config, .gitignore. Behaviour is unchanged. Kept for one release: - A top-level application package whose meta-path finder resolves application.x.y to the already-imported docsgpt.x.y object, so old imports and entry points (celery -A application.app.celery, uvicorn application.asgi:asgi_app) keep working with a FutureWarning. - Celery registers every application.* task name as an alias of its docsgpt.* task on start-up, so messages queued by the previous release still run. The redbeat key prefix moves to redbeat:docsgpt:v2: so schedule entries the previous release wrote are left unread instead of firing twice. The backend image builds from the repository root (docker build -f docsgpt/Dockerfile .) so it can ship the alias package; a root .dockerignore allow-lists docsgpt/ and application/ and keeps caches, local data, .env files, the sample index files and the Dockerfile out. Compose and the image workflows point at the new context.
58 lines
2.1 KiB
Python
58 lines
2.1 KiB
Python
"""Gunicorn worker that bounds uvicorn's graceful-shutdown drain.
|
|
|
|
``uvicorn_worker`` doesn't forward gunicorn's ``--graceful-timeout`` to uvicorn's
|
|
``timeout_graceful_shutdown``, so after a ``max_requests`` recycle the drain is
|
|
unbounded: a held-open SSE/long-poll connection (a Flask/WSGI generator in
|
|
a2wsgi's thread pool, which asyncio can't cancel) hangs the worker until the
|
|
``--timeout`` watchdog SIGKILLs it (mislabeled "Perhaps out of memory?").
|
|
|
|
This worker bounds the drain (``timeout_graceful_shutdown`` from settings) and
|
|
raises the shutdown flag at drain start — including the signal-less
|
|
``max_requests`` path — so generators stop within one poll tick. Wire in via
|
|
``-k docsgpt.gunicorn_worker.BoundedDrainUvicornWorker``.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import socket
|
|
import sys
|
|
from typing import Any
|
|
|
|
from gunicorn.arbiter import Arbiter
|
|
from uvicorn.server import Server
|
|
from uvicorn_worker import UvicornWorker
|
|
|
|
from docsgpt.core.settings import settings
|
|
from docsgpt.core.shutdown import begin_shutdown
|
|
|
|
|
|
class _ShutdownAwareServer(Server):
|
|
"""uvicorn ``Server`` that raises the shutdown flag when a drain starts.
|
|
|
|
``serve()`` always calls ``shutdown()`` when its loop exits — including the
|
|
signal-less ``max_requests`` recycle — so this is the reliable hook point.
|
|
"""
|
|
|
|
async def shutdown(self, sockets: list[socket.socket] | None = None) -> None:
|
|
begin_shutdown()
|
|
await super().shutdown(sockets=sockets)
|
|
|
|
|
|
class BoundedDrainUvicornWorker(UvicornWorker):
|
|
"""UvicornWorker with a bounded drain and shutdown-aware streaming."""
|
|
|
|
CONFIG_KWARGS: dict[str, Any] = {
|
|
"loop": "auto",
|
|
"http": "auto",
|
|
"timeout_graceful_shutdown": settings.GRACEFUL_SHUTDOWN_TIMEOUT_SECONDS,
|
|
}
|
|
|
|
async def _serve(self) -> None:
|
|
# Mirrors UvicornWorker._serve but swaps in the shutdown-aware Server.
|
|
self.config.app = self.wsgi
|
|
server = _ShutdownAwareServer(config=self.config)
|
|
self._install_sigquit_handler()
|
|
await server.serve(sockets=self.sockets)
|
|
if not server.started:
|
|
sys.exit(Arbiter.WORKER_BOOT_ERROR)
|