Brings the system changes of lab 2 from 8cddb09..102579e onto the lab 3 code, resolving each conflict by meaning: - the request pipeline counts the body bytes actually received, so a chunked body over the limit gets 413 instead of reaching the app; - logout revokes through a SessionRevocations port and withdraws the mark when PostgreSQL rejects the delete, so 503 means the session still works; PostgresSessionDirectory loses its no-op invalidate; - an empty cursor is 400 on history and on the chat list; - EventBus.project and the test that allowed importing an event across zones are gone, the config rules speak of the schema foreign key; - the worker waits one second less than the rest of the shutdown budget, so it cancels a stuck request with 500 and exits 0 instead of being killed by the gunicorn master. Lab 3 keeps 3 s after the drain window for the database timeouts, so SHUTDOWN_TIMEOUT_SECONDS becomes 6 s and the stop hooks wait 7 s, still under the 8 s grace; - the lab 3 single statement post already locks the chat only for a member, so lab 2's LOCK_CHAT edit is not needed; its test is kept; - lab 2 tests come along with Idempotency-Key where lab 3 needs it; the short TTL after restart runs without api-shortttl by shortening the session in the database; test_drain adds a stuck request; - make validate uses .venv with openapi-spec-validator, PYTEST_ARGS, faults-evidence, down with the bench profile, the image is tagged by the compose project, start probes every 200 ms, cache evidence fails without a tombstone and checks the key expiry, kdf.txt shows the seed stub apart. The track 2 tooling of lab 2 (track.py, postgres-restore and the database argument of migrate) and its evidence scripts stay out. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01V1d3RLHNBBMA6UqBLLJv27
67 lines
2.1 KiB
Python
67 lines
2.1 KiB
Python
import asyncio
|
|
import logging
|
|
import os
|
|
import sys
|
|
from typing import Any
|
|
|
|
from gunicorn.arbiter import Arbiter
|
|
from gunicorn.glogging import Logger as GunicornBaseLogger
|
|
from uvicorn.server import Server
|
|
from uvicorn_worker import UvicornWorker
|
|
|
|
from messenger.main import LOG_FIELDS
|
|
from messenger.shared.entrypoints.health import DRAINING
|
|
from messenger.shared.infrastructure.logging import configure
|
|
|
|
LOG_LEVEL = os.environ.get("LOG_LEVEL", "INFO")
|
|
SHUTDOWN_SECONDS = int(os.environ.get("SHUTDOWN_TIMEOUT_SECONDS", "6"))
|
|
DRAIN_SECONDS = float(os.environ.get("DRAIN_SECONDS", "2"))
|
|
|
|
|
|
class StructlogGunicornLogger(GunicornBaseLogger):
|
|
def setup(self, cfg: object) -> None:
|
|
configure(LOG_LEVEL, LOG_FIELDS)
|
|
|
|
def access(self, resp: object, req: object, environ: object, request_time: object) -> None:
|
|
return
|
|
|
|
|
|
class DrainingServer(Server):
|
|
async def main_loop(self) -> None:
|
|
await super().main_loop()
|
|
if not self.force_exit:
|
|
DRAINING.set()
|
|
logging.getLogger("uvicorn.error").info("Draining for %.1f s", DRAIN_SECONDS)
|
|
await asyncio.sleep(DRAIN_SECONDS)
|
|
|
|
|
|
class MessengerWorker(UvicornWorker):
|
|
CONFIG_KWARGS: dict[str, Any] = {
|
|
"access_log": False,
|
|
"server_header": False,
|
|
"date_header": False,
|
|
"timeout_graceful_shutdown": max(1, int(SHUTDOWN_SECONDS - DRAIN_SECONDS) - 1),
|
|
}
|
|
|
|
async def _serve(self) -> None:
|
|
self.config.app = self.wsgi
|
|
server = DrainingServer(config=self.config)
|
|
self._install_sigquit_handler()
|
|
await server.serve(sockets=self.sockets)
|
|
if not server.started:
|
|
sys.exit(Arbiter.WORKER_BOOT_ERROR)
|
|
|
|
|
|
bind = f"{os.environ.get('HOST', '0.0.0.0')}:{os.environ.get('PORT', '8080')}"
|
|
workers = int(os.environ.get("WEB_CONCURRENCY", "2"))
|
|
worker_class = "messenger.gunicorn_conf.MessengerWorker"
|
|
logger_class = StructlogGunicornLogger
|
|
loglevel = LOG_LEVEL.lower()
|
|
accesslog = None
|
|
errorlog = "-"
|
|
timeout = int(float(os.environ.get("REQUEST_TIMEOUT_SECONDS", "8")) + 5)
|
|
graceful_timeout = SHUTDOWN_SECONDS
|
|
keepalive = 5
|
|
preload_app = False
|
|
control_socket_disable = True
|