Исправить балансировку и миграции лабы 3

This commit is contained in:
2026-11-18 17:44:53 +03:00
parent 2f1cf3afac
commit e51dc20fce
6 changed files with 106 additions and 6 deletions
+8 -1
View File
@@ -20,7 +20,6 @@ async def init_pool(dsn: str) -> asyncpg.Pool:
command_timeout=5,
timeout=5,
)
await _run_migrations(_pool)
return _pool
@@ -51,6 +50,14 @@ async def _run_migrations(p: asyncpg.Pool) -> None:
await conn.execute("SELECT pg_advisory_unlock($1)", MIGRATION_LOCK_KEY)
async def run_migrations(dsn: str) -> None:
p = await asyncpg.create_pool(dsn, min_size=1, max_size=2, command_timeout=5, timeout=5)
try:
await _run_migrations(p)
finally:
await p.close()
async def _apply_migrations(conn: asyncpg.Connection) -> None:
await conn.execute(
"""
+20 -2
View File
@@ -10,6 +10,7 @@ from datetime import datetime, timezone
import asyncpg
from fastapi import FastAPI, Header, Request
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse, Response
from pydantic import BaseModel, ConfigDict, Field, ValidationError
@@ -34,6 +35,8 @@ async def tagging_middleware(request: Request, call_next):
request_id = uuid.uuid4().hex
request.state.request_id = request_id
try:
if request.url.path.startswith("/api/") and not await _dependencies_ready():
raise unavailable("database or session store not ready")
response = await call_next(request)
except ApiError as exc:
response = _error_response(exc, request_id)
@@ -44,6 +47,10 @@ async def tagging_middleware(request: Request, call_next):
return response
async def _dependencies_ready() -> bool:
return await db.ping() and await sessions.ping()
def _error_response(exc: ApiError, request_id: str) -> JSONResponse:
headers = {}
if exc.status == 401:
@@ -66,6 +73,14 @@ async def api_error_handler(request: Request, exc: ApiError):
return _error_response(exc, getattr(request.state, "request_id", uuid.uuid4().hex))
@app.exception_handler(RequestValidationError)
async def request_validation_error_handler(request: Request, exc: RequestValidationError):
return _error_response(
bad_request("invalid request parameters"),
getattr(request.state, "request_id", uuid.uuid4().hex),
)
def iso(value: datetime) -> str:
return value.astimezone(timezone.utc).isoformat().replace("+00:00", "Z")
@@ -111,7 +126,10 @@ def decode_cursor(value: str) -> dict:
try:
padded = value + "=" * (-len(value) % 4)
raw = base64.urlsafe_b64decode(padded.encode())
return json.loads(raw)
payload = json.loads(raw)
if not isinstance(payload, dict):
raise bad_request("malformed cursor")
return payload
except (binascii.Error, ValueError, UnicodeDecodeError):
raise bad_request("malformed cursor")
@@ -210,7 +228,7 @@ async def health_ready():
@app.post("/api/v1/users", status_code=201)
async def register_user(request: Request):
body = await parse_body(request, CreateUser)
if not USERNAME_RE.match(body.username):
if not USERNAME_RE.fullmatch(body.username):
raise bad_request("username must match ^[a-z0-9_]{3,32}$")
if not body.display_name.strip():
raise bad_request("display_name must not be blank")
+24
View File
@@ -0,0 +1,24 @@
"""Run database migrations as a dedicated deployment step."""
import asyncio
import os
from pathlib import Path
from . import db
def read_secret(name: str, fallback: str) -> str:
path = os.environ.get(name)
if path:
return Path(path).read_text(encoding="utf-8").strip()
value = os.environ.get(fallback)
if not value:
raise RuntimeError(f"{name} or {fallback} is required")
return value
async def main() -> None:
await db.run_migrations(read_secret("DATABASE_URL_FILE", "DATABASE_URL"))
if __name__ == "__main__":
asyncio.run(main())
+17 -1
View File
@@ -6,7 +6,9 @@ services:
ports:
- "8080:8080"
volumes:
- ./infra/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
- ./infra/nginx/nginx.conf:/etc/nginx/nginx.conf.template:ro
- ./infra/nginx/run.sh:/usr/local/bin/run-nginx.sh:ro
command: ["/bin/sh", "/usr/local/bin/run-nginx.sh"]
depends_on:
api1:
condition: service_healthy
@@ -33,6 +35,8 @@ services:
- database_url
- cache_url
depends_on:
migrate:
condition: service_completed_successfully
postgres:
condition: service_healthy
redis:
@@ -55,6 +59,18 @@ services:
<<: *api-env
INSTANCE_ID: api2
migrate:
build: .
image: mtusi2026lab3-api:local
command: ["python", "-m", "app.migrate"]
environment:
DATABASE_URL_FILE: /run/secrets/database_url
secrets:
- database_url
depends_on:
postgres:
condition: service_healthy
postgres:
image: postgres:16.4
environment:
+2 -2
View File
@@ -8,8 +8,8 @@ http {
upstream messenger_api {
# Passive health check: a backend failing 1 probe is removed for 3s,
# which keeps failover detection well under the 10s budget.
server api1:8080 max_fails=1 fail_timeout=3s;
server api2:8080 max_fails=1 fail_timeout=3s;
server API1_ADDRESS max_fails=1 fail_timeout=3s;
server API2_ADDRESS max_fails=1 fail_timeout=3s;
}
server {
+35
View File
@@ -0,0 +1,35 @@
#!/bin/sh
set -eu
template=/etc/nginx/nginx.conf.template
rendered=/tmp/nginx.conf
last_addresses=
resolve_address() {
getent hosts "$1" | awk 'NR == 1 { print $1 ":8080" }'
}
render() {
api1="$(resolve_address api1 || true)"
api2="$(resolve_address api2 || true)"
[ -n "$api1" ] || api1="127.0.0.1:9"
[ -n "$api2" ] || api2="127.0.0.1:9"
printf '%s\n' "$api1 $api2"
sed -e "s/API1_ADDRESS/$api1/" -e "s/API2_ADDRESS/$api2/" "$template" > "$rendered"
}
current="$(render)"
last_addresses="$current"
nginx -c "$rendered" -g 'daemon off;' &
nginx_pid=$!
trap 'kill "$nginx_pid" 2>/dev/null || true; wait "$nginx_pid" 2>/dev/null || true' INT TERM EXIT
while kill -0 "$nginx_pid" 2>/dev/null; do
sleep 2
current="$(render)"
if [ "$current" != "$last_addresses" ]; then
nginx -s reload -c "$rendered"
last_addresses="$current"
fi
done
wait "$nginx_pid"