forked from mtusi2026/mtusi2026lab3
chore: перенос решения lab2 (src, миграции, student-tests, Dockerfile, compose, lock) без изменений
Исходная точка lab3: код из solution/bvt2302-bodanyuk лабы 2, коммит ef44e48 (PR mtusi2026/mtusi2026lab2#42 принят). README, COURSE, contracts/ и tests/ преподавателя не тронуты; к Makefile дописан студенческий раздел lab2, .env.example — версия lab2. Исследование трека 2 lab2 (bench/track2) не переносилось. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
0ab3a341aa
commit
4f6cfda37a
@@ -0,0 +1,65 @@
|
||||
"""Entry point: `python -m messenger`. The HTTP server is the main process (PID 1 in Docker).
|
||||
|
||||
Shutdown sequence on SIGTERM/SIGINT:
|
||||
1. readiness flips to 503 (`app.state.draining = True`);
|
||||
2. uvicorn closes the listening socket, so new connections are refused;
|
||||
3. in-flight requests get up to SHUTDOWN_TIMEOUT_SECONDS (default 8 s < Docker's 10 s) to finish;
|
||||
4. the lifespan closes the DB pool and the Valkey client; the process exits with code 0.
|
||||
Data stays in PostgreSQL and Valkey volumes.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import sys
|
||||
from types import FrameType
|
||||
|
||||
import uvicorn
|
||||
|
||||
from . import __version__, logging_setup
|
||||
from .app import create_app
|
||||
from .config import ConfigError, Settings
|
||||
|
||||
log = logging.getLogger("messenger")
|
||||
|
||||
|
||||
class GracefulServer(uvicorn.Server):
|
||||
"""uvicorn.Server that marks the app as draining before the normal graceful stop."""
|
||||
|
||||
def handle_exit(self, sig: int, frame: FrameType | None) -> None:
|
||||
if not self.should_exit:
|
||||
log.info("shutdown_requested", extra={"fields": {"signal": sig, "timeout_s": self.config.timeout_graceful_shutdown}})
|
||||
self.config.app.state.draining = True # config.app is our Starlette instance
|
||||
super().handle_exit(sig, frame)
|
||||
|
||||
|
||||
def main() -> int:
|
||||
try:
|
||||
settings = Settings.from_env()
|
||||
except ConfigError as exc:
|
||||
print(f"configuration error: {exc}", file=sys.stderr)
|
||||
return 2
|
||||
logging_setup.configure(settings.log_level)
|
||||
app = create_app(settings)
|
||||
config = uvicorn.Config(
|
||||
app,
|
||||
host=settings.host,
|
||||
port=settings.port,
|
||||
log_config=None,
|
||||
access_log=False,
|
||||
server_header=False,
|
||||
proxy_headers=False,
|
||||
lifespan="on",
|
||||
timeout_graceful_shutdown=settings.shutdown_timeout,
|
||||
)
|
||||
server = GracefulServer(config)
|
||||
log.info("starting", extra={"fields": {
|
||||
"version": __version__, "env": settings.env, "host": settings.host, "port": settings.port,
|
||||
"python": sys.version.split()[0], "storage": "postgresql",
|
||||
}})
|
||||
server.run()
|
||||
log.info("stopped")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user