Source: immutable lab5 snapshot f88e9ba43cdfd7eb6a5a7ae5feb4247b4e820071; forward the allowed diff from 64d5c67. Carry SQLAlchemy, Alembic, reliability fixes, versioned jobs, infrastructure and student tests without changing protected coursework files.
Validation: make check, validate, lint; 515 student tests passed with one Linux-only skip; public smoke with telemetry enabled and repeat migration passed. Stop the owned stack and retain volumes. Preserve the existing OTel work in progress; grade implementation remains a separate phase.
67 lines
2.2 KiB
Python
67 lines
2.2 KiB
Python
import asyncio
|
|
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.infrastructure.logging import StructlogLogger, configure
|
|
from messenger.shared.infrastructure.readiness import DRAINING
|
|
|
|
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"))
|
|
LOGGER = StructlogLogger(level=LOG_LEVEL, allowed_fields=LOG_FIELDS)
|
|
|
|
|
|
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()
|
|
LOGGER.info("service.draining", limit=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
|