forked from mtusi2026/mtusi2026lab5
Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2d40313318 | ||
|
|
2639c08944 | ||
|
|
9899ee4676 | ||
|
|
7cd02da51d | ||
|
|
2b9fb3f375 | ||
|
|
0a70358829 |
@@ -18,3 +18,4 @@ __pycache__
|
|||||||
node_modules
|
node_modules
|
||||||
evidence
|
evidence
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
docs
|
||||||
|
|||||||
+86
-12
@@ -1,25 +1,99 @@
|
|||||||
# Copy to .env for your LOCAL synthetic test environment; never commit .env.
|
# Скопируйте в .env для локального окружения; .env не коммитится.
|
||||||
# These are logical interface suggestions; map actual framework names in REPORT.md.
|
# Настройки приложения, Compose и очереди.
|
||||||
APP_HOST=0.0.0.0
|
APP_HOST=0.0.0.0
|
||||||
APP_PORT=8080
|
APP_PORT=8080
|
||||||
APP_ENV=lab
|
APP_ENV=development
|
||||||
|
LOG_LEVEL=INFO
|
||||||
|
|
||||||
|
MAX_MESSAGES=100000
|
||||||
|
# Лимит одновременных запросов НА ОДНУ реплику
|
||||||
|
MAX_IN_FLIGHT=16
|
||||||
|
|
||||||
SESSION_TTL_SECONDS=3600
|
SESSION_TTL_SECONDS=3600
|
||||||
# Choose credentials locally. File paths contain no secret values.
|
SESSION_STORE=redis
|
||||||
DATABASE_URL_FILE=/run/secrets/database_url
|
|
||||||
# Optional on 3, required use on 4 in lab2; Redis OR Valkey:
|
REDIS_HOST=redis
|
||||||
SESSION_STORE=postgres
|
REDIS_PORT=6379
|
||||||
CACHE_URL_FILE=/run/secrets/cache_url
|
REDIS_DB=0
|
||||||
|
REDIS_MAX_CONNECTIONS=20
|
||||||
|
REDIS_POOL_TIMEOUT_SECONDS=1
|
||||||
|
REDIS_CONNECT_TIMEOUT_SECONDS=1
|
||||||
|
REDIS_SOCKET_TIMEOUT_SECONDS=0.5
|
||||||
|
|
||||||
|
DB_HOST=postgres
|
||||||
|
DB_PORT=5432
|
||||||
|
DB_NAME=messenger
|
||||||
|
DB_USER=messenger
|
||||||
|
|
||||||
|
# Пул на одну реплику: суммарно API_REPLICAS * (POOL_SIZE + MAX_OVERFLOW)
|
||||||
|
# соединений должно помещаться в max_connections PostgreSQL.
|
||||||
|
DB_POOL_SIZE=5
|
||||||
|
DB_MAX_OVERFLOW=10
|
||||||
|
DB_POOL_TIMEOUT_SECONDS=3
|
||||||
|
DB_CONNECT_TIMEOUT_SECONDS=3
|
||||||
|
DB_STATEMENT_TIMEOUT_MS=5000
|
||||||
|
|
||||||
|
# --- Балансировка и идемпотентность ---
|
||||||
API_REPLICAS=2
|
API_REPLICAS=2
|
||||||
|
API_IMAGE=messenger:local
|
||||||
|
API_BUILD_REVISION=development
|
||||||
IDEMPOTENCY_TTL_SECONDS=86400
|
IDEMPOTENCY_TTL_SECONDS=86400
|
||||||
S3_ENDPOINT=http://s3:9000
|
SHUTDOWN_DRAIN_SECONDS=1
|
||||||
|
GRACEFUL_SHUTDOWN_TIMEOUT_SECONDS=5
|
||||||
|
READINESS_TIMEOUT_SECONDS=2
|
||||||
|
|
||||||
|
# Порт единственной публичной точки входа (nginx)
|
||||||
|
LB_HOST_PORT=18081
|
||||||
|
# Используются только тестовым стендом (compose.test.yaml)
|
||||||
|
DB_HOST_PORT=25432
|
||||||
|
REDIS_HOST_PORT=26379
|
||||||
|
|
||||||
|
# --- MinIO, ограниченный upload и очистка журнала ---
|
||||||
|
S3_ENDPOINT=http://minio:9000
|
||||||
S3_REGION=us-east-1
|
S3_REGION=us-east-1
|
||||||
S3_BUCKET=messenger-attachments
|
S3_BUCKET=messenger-attachments
|
||||||
S3_ACCESS_KEY_ID_FILE=/run/secrets/s3_access_key_id
|
S3_ACCESS_KEY_ID_FILE=/run/secrets/s3_access_key_id
|
||||||
S3_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_secret_access_key
|
S3_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_secret_access_key
|
||||||
MAX_UPLOAD_BYTES=10485760
|
S3_CONNECT_TIMEOUT_SECONDS=2
|
||||||
MAX_IMAGE_PIXELS=40000000
|
S3_READ_TIMEOUT_SECONDS=10
|
||||||
BROKER_URL_FILE=/run/secrets/broker_url
|
S3_UPLOAD_TIMEOUT_SECONDS=30
|
||||||
|
S3_DOWNLOAD_TIMEOUT_SECONDS=60
|
||||||
|
S3_IMAGE_TIMEOUT_SECONDS=10
|
||||||
|
S3_MAX_UPLOAD_BYTES=10485760
|
||||||
|
S3_MAX_IMAGE_PIXELS=40000000
|
||||||
|
S3_CHUNK_BYTES=65536
|
||||||
|
S3_MAX_CONCURRENT_UPLOADS=2
|
||||||
|
S3_CLEANUP_GRACE_SECONDS=86400
|
||||||
|
S3_CLEANUP_INTERVAL_SECONDS=300
|
||||||
|
|
||||||
|
# Пути секретов внутри контейнеров; должны совпадать с secrets mounts.
|
||||||
|
POSTGRES_PASSWORD_FILE=/run/secrets/db_password
|
||||||
|
MINIO_ROOT_USER_FILE=/run/secrets/minio_root_user
|
||||||
|
MINIO_ROOT_PASSWORD_FILE=/run/secrets/minio_root_password
|
||||||
|
MINIO_BROWSER=off
|
||||||
|
|
||||||
|
# Отдельный RabbitMQ broker и ограниченный worker.
|
||||||
WORKER_CONCURRENCY=2
|
WORKER_CONCURRENCY=2
|
||||||
JOB_MAX_ATTEMPTS=5
|
JOB_MAX_ATTEMPTS=5
|
||||||
JOB_TIMEOUT_SECONDS=60
|
JOB_TIMEOUT_SECONDS=60
|
||||||
STAGING_RETENTION_SECONDS=86400
|
STAGING_RETENTION_SECONDS=86400
|
||||||
|
BROKER_HOST=broker
|
||||||
|
BROKER_PORT=5672
|
||||||
|
BROKER_PASSWORD_FILE=/run/secrets/broker_password
|
||||||
|
BROKER_QUEUE=attachment-jobs
|
||||||
|
BROKER_USER=messenger
|
||||||
|
BROKER_VHOST=/
|
||||||
|
JOB_LEASE_SECONDS=90
|
||||||
|
JOB_RETRY_SECONDS=5
|
||||||
|
PUBLISHER_POLL_SECONDS=1
|
||||||
|
|
||||||
|
# Качество вариантов и лимиты одного процесса обработки
|
||||||
|
IMAGE_JPEG_QUALITY=85
|
||||||
|
IMAGE_WEBP_QUALITY=80
|
||||||
|
IMAGE_WEBP_METHOD=4
|
||||||
|
IMAGE_MEMORY_MIB=768
|
||||||
|
IMAGE_CPU_SECONDS=20
|
||||||
|
|
||||||
|
# Retry cap and retained failed sources for explicit replay
|
||||||
|
JOB_RETRY_MAX_SECONDS=120
|
||||||
|
FAILED_STAGING_RETENTION_SECONDS=604800
|
||||||
|
|||||||
@@ -28,5 +28,11 @@ coverage/
|
|||||||
.DS_Store
|
.DS_Store
|
||||||
.idea/
|
.idea/
|
||||||
.vscode/
|
.vscode/
|
||||||
|
venv/
|
||||||
|
.pytest_cache/
|
||||||
|
.ruff_cache/
|
||||||
# Keep small, sanitized evidence summaries in evidence/ under version control.
|
# Keep small, sanitized evidence summaries in evidence/ under version control.
|
||||||
# Inspect git diff --cached: ignore rules are not a secret scanner.
|
# Inspect git diff --cached: ignore rules are not a secret scanner.
|
||||||
|
|
||||||
|
# Private working documentation
|
||||||
|
/docs/
|
||||||
|
|||||||
+29
@@ -0,0 +1,29 @@
|
|||||||
|
FROM python:3.12.15-slim-bookworm
|
||||||
|
|
||||||
|
ENV PYTHONDONTWRITEBYTECODE=1 \
|
||||||
|
PYTHONUNBUFFERED=1 \
|
||||||
|
PIP_NO_CACHE_DIR=1 \
|
||||||
|
PIP_DISABLE_PIP_VERSION_CHECK=1
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
RUN useradd --system --uid 10001 --no-create-home appuser
|
||||||
|
|
||||||
|
COPY requirements.txt .
|
||||||
|
RUN pip install -r requirements.txt
|
||||||
|
|
||||||
|
COPY alembic.ini ./
|
||||||
|
COPY migrations ./migrations
|
||||||
|
COPY app ./app
|
||||||
|
|
||||||
|
ARG API_BUILD_REVISION
|
||||||
|
LABEL org.opencontainers.image.revision=$API_BUILD_REVISION
|
||||||
|
|
||||||
|
USER appuser
|
||||||
|
|
||||||
|
EXPOSE 8080
|
||||||
|
|
||||||
|
HEALTHCHECK --interval=3s --timeout=3s --start-period=10s --retries=2 \
|
||||||
|
CMD ["python", "-c", "import os, urllib.request; urllib.request.urlopen('http://127.0.0.1:' + os.environ['APP_PORT'] + '/health/ready', timeout=2.5)"]
|
||||||
|
|
||||||
|
CMD ["python", "-m", "app"]
|
||||||
@@ -1,13 +1,79 @@
|
|||||||
PYTHON ?= python3
|
-include .env
|
||||||
BASE_URL ?= http://localhost:8080
|
|
||||||
|
PYTHON ?= $(if $(wildcard .venv/bin/python),.venv/bin/python,$(if $(wildcard venv/bin/python),venv/bin/python,python3))
|
||||||
|
PYTEST_ARGS ?=
|
||||||
|
BASE_URL ?= http://localhost:$(or $(LB_HOST_PORT),8080)
|
||||||
CA_FILE ?=
|
CA_FILE ?=
|
||||||
|
SERVICE ?=
|
||||||
ACTION_SCRIPT ?=
|
ACTION_SCRIPT ?=
|
||||||
TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",)
|
TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",)
|
||||||
|
COMPOSE = docker compose --env-file .env
|
||||||
|
|
||||||
.PHONY: help check validate test load test-persistence test-failover
|
TEST_PROJECT ?= messenger-test
|
||||||
|
TEST_LB_PORT ?= 28080
|
||||||
|
TEST_DB_PORT ?= $(DB_HOST_PORT)
|
||||||
|
TEST_REDIS_PORT ?= $(REDIS_HOST_PORT)
|
||||||
|
TEST_BASE_URL = http://localhost:$(TEST_LB_PORT)
|
||||||
|
TEST_ENV = COMPOSE_PROJECT_NAME=$(TEST_PROJECT) COMPOSE_FILE=compose.yaml:compose.test.yaml \
|
||||||
|
LB_HOST_PORT=$(TEST_LB_PORT) DB_HOST_PORT=$(TEST_DB_PORT) REDIS_HOST_PORT=$(TEST_REDIS_PORT)
|
||||||
|
TEST_COMPOSE = $(TEST_ENV) $(COMPOSE)
|
||||||
|
|
||||||
|
TEST_DB_PASSWORD = $(if $(wildcard secrets/db_password),$(file <secrets/db_password),$(DB_PASSWORD))
|
||||||
|
TEST_DATABASE_URL ?= postgresql+asyncpg://$(DB_USER):$(TEST_DB_PASSWORD)@localhost:$(TEST_DB_PORT)/$(DB_NAME)_test
|
||||||
|
TEST_REDIS_HOST ?= localhost
|
||||||
|
TEST_REDIS_PASSWORD ?= $(if $(wildcard secrets/redis_password),$(file <secrets/redis_password),$(REDIS_PASSWORD))
|
||||||
|
TEST_REDIS_DB ?= 15
|
||||||
|
export TEST_DATABASE_URL TEST_REDIS_HOST TEST_REDIS_PORT TEST_REDIS_PASSWORD TEST_REDIS_DB
|
||||||
|
|
||||||
|
.PHONY: help init up down restart ps logs migrate migration worker-stop worker-start \
|
||||||
|
cleanup-dry-run cleanup-apply check validate test test-unit test-pipeline \
|
||||||
|
test-nginx test-storage test-outbox test-jobs test-persistence test-failover \
|
||||||
|
test-suite test-stand-up test-stand-destroy
|
||||||
|
|
||||||
help:
|
help:
|
||||||
@echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes"
|
@echo "Запуск: init | up | down | restart | ps | logs SERVICE=worker"
|
||||||
|
@echo "Обслуживание: migrate | migration m='описание' | worker-stop | worker-start"
|
||||||
|
@echo "Очистка объектов: cleanup-dry-run | cleanup-apply"
|
||||||
|
@echo "Проверки: check | validate | test | test-pipeline | test-unit PYTEST_ARGS=..."
|
||||||
|
@echo "Сервисы: test-nginx | test-storage | test-outbox | test-jobs"
|
||||||
|
@echo "Отказы API (по запросу): test-persistence | test-failover"
|
||||||
|
@echo "Автономный прогон с удалением тестового стенда: test-suite"
|
||||||
|
|
||||||
|
init:
|
||||||
|
$(PYTHON) scripts/init_local.py
|
||||||
|
|
||||||
|
up:
|
||||||
|
$(COMPOSE) up -d --build --wait --wait-timeout 120
|
||||||
|
|
||||||
|
down:
|
||||||
|
$(COMPOSE) down
|
||||||
|
|
||||||
|
restart:
|
||||||
|
scripts/restart.sh
|
||||||
|
|
||||||
|
ps:
|
||||||
|
$(COMPOSE) ps $(SERVICE)
|
||||||
|
|
||||||
|
logs:
|
||||||
|
$(COMPOSE) logs -f $(SERVICE)
|
||||||
|
|
||||||
|
migrate:
|
||||||
|
$(COMPOSE) run --rm --build migrate
|
||||||
|
|
||||||
|
migration:
|
||||||
|
DB_HOST=localhost DB_PORT=$(TEST_DB_PORT) $(PYTHON) -m alembic revision --autogenerate -m "$(m)"
|
||||||
|
|
||||||
|
worker-stop:
|
||||||
|
$(COMPOSE) stop worker
|
||||||
|
|
||||||
|
worker-start:
|
||||||
|
$(COMPOSE) start worker
|
||||||
|
|
||||||
|
cleanup-dry-run:
|
||||||
|
$(COMPOSE) run --rm --no-deps cleanup python -m app.core.cleanup --once
|
||||||
|
|
||||||
|
cleanup-apply:
|
||||||
|
$(COMPOSE) run --rm --no-deps cleanup python -m app.core.cleanup --once --apply
|
||||||
|
|
||||||
check:
|
check:
|
||||||
$(PYTHON) scripts/check_contract.py
|
$(PYTHON) scripts/check_contract.py
|
||||||
@@ -16,15 +82,48 @@ validate:
|
|||||||
$(PYTHON) scripts/validate_openapi.py
|
$(PYTHON) scripts/validate_openapi.py
|
||||||
|
|
||||||
test:
|
test:
|
||||||
$(PYTHON) tests/smoke.py --base-url "$(BASE_URL)" $(TLS_ARGS)
|
$(PYTHON) tests/smoke.py --base-url "$(TEST_BASE_URL)" $(TLS_ARGS)
|
||||||
|
|
||||||
load:
|
# Все сохранённые регрессионные тесты запускаются явно; PYTEST_ARGS выбирает подмножество.
|
||||||
$(PYTHON) tests/load.py --base-url "$(BASE_URL)" $(TLS_ARGS)
|
test-unit:
|
||||||
|
$(TEST_ENV) $(PYTHON) -m pytest -q $(PYTEST_ARGS)
|
||||||
|
|
||||||
|
test-pipeline:
|
||||||
|
$(TEST_ENV) $(PYTHON) -m pytest -q student-tests/test_attachments.py student-tests/test_jobs.py student-tests/test_image_pipeline.py $(PYTEST_ARGS)
|
||||||
|
|
||||||
|
test-nginx:
|
||||||
|
$(PYTHON) scripts/check_nginx.py
|
||||||
|
|
||||||
|
test-storage:
|
||||||
|
$(PYTHON) -m scripts.check_s3 --base-url "$(BASE_URL)" $(TLS_ARGS)
|
||||||
|
|
||||||
|
test-outbox:
|
||||||
|
$(PYTHON) -m scripts.check_outbox_recovery --base-url "$(BASE_URL)" $(TLS_ARGS)
|
||||||
|
|
||||||
|
test-jobs:
|
||||||
|
$(PYTHON) -m scripts.check_jobs --base-url "$(BASE_URL)" $(TLS_ARGS)
|
||||||
|
|
||||||
test-persistence:
|
test-persistence:
|
||||||
@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable restart hook"; exit 2)
|
$(PYTHON) tests/resilience.py --mode persistence --action-script "$(or $(ACTION_SCRIPT),scripts/restart.sh)" --base-url "$(BASE_URL)" $(TLS_ARGS)
|
||||||
$(PYTHON) tests/resilience.py --mode persistence --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)
|
|
||||||
|
|
||||||
test-failover:
|
test-failover:
|
||||||
@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable stop-one hook"; exit 2)
|
$(PYTHON) tests/resilience.py --mode failover --action-script "$(or $(ACTION_SCRIPT),scripts/stop-one.sh)" --base-url "$(BASE_URL)" $(TLS_ARGS)
|
||||||
$(PYTHON) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)
|
|
||||||
|
# Отдельные PostgreSQL/Redis нужны для локальных тестов; тестовая БД создаётся явно.
|
||||||
|
test-stand-up:
|
||||||
|
$(TEST_COMPOSE) up -d --build --wait --wait-timeout 120
|
||||||
|
$(TEST_COMPOSE) exec -T postgres sh -c 'psql -U "$$POSTGRES_USER" -d postgres -tAc "SELECT 1 FROM pg_database WHERE datname = '\''$${POSTGRES_DB}_test'\''" | grep -q 1 || createdb -U "$$POSTGRES_USER" "$${POSTGRES_DB}_test"'
|
||||||
|
|
||||||
|
test-stand-destroy:
|
||||||
|
$(TEST_COMPOSE) down --volumes --remove-orphans
|
||||||
|
|
||||||
|
# Проверки текущего pipeline; полный регрессионный набор и отказы API — отдельные цели.
|
||||||
|
test-suite:
|
||||||
|
@set -eu; trap '$(TEST_COMPOSE) down --volumes --remove-orphans' EXIT; \
|
||||||
|
$(MAKE) check validate; \
|
||||||
|
$(MAKE) test-stand-up; \
|
||||||
|
$(TEST_ENV) $(MAKE) test-pipeline; \
|
||||||
|
$(TEST_ENV) $(MAKE) test BASE_URL="$(TEST_BASE_URL)"; \
|
||||||
|
$(TEST_ENV) $(MAKE) test-storage BASE_URL="$(TEST_BASE_URL)"; \
|
||||||
|
$(TEST_ENV) $(MAKE) test-outbox BASE_URL="$(TEST_BASE_URL)"; \
|
||||||
|
$(TEST_ENV) $(MAKE) test-jobs BASE_URL="$(TEST_BASE_URL)";
|
||||||
|
|||||||
@@ -1,37 +1,95 @@
|
|||||||
# Отчёт — лабораторная 5
|
# Отчёт — лабораторная 5
|
||||||
|
|
||||||
- Автор, группа: TODO
|
- Автор, группа: Григорян Илья Мурадович, БПИ2303.
|
||||||
- Целевая оценка; выбранный трек 5, если нужен: TODO
|
- Целевая оценка: **4**. Все пункты уровней **3 и 4** реализованы и проверены.
|
||||||
- Commit/PR: TODO после публикации
|
- Commit/PR: TODO после публикации.
|
||||||
- Использованные источники, библиотеки и помощь ИИ: TODO
|
- Использованные источники, библиотеки и помощь ИИ: Python 3.12, FastAPI, SQLAlchemy, Pillow, aio-pika; [RabbitMQ](https://www.rabbitmq.com/docs/reliability), [Pillow](https://pillow.readthedocs.io/en/stable/); помощь Codex при реализации и проверках.
|
||||||
|
|
||||||
## Запуск из чистого clone
|
## Запуск из чистого clone
|
||||||
|
|
||||||
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
|
Нужны Python 3.12, pip и Docker Compose. Версии зависимостей закреплены в requirements.txt;
|
||||||
|
основные образы — PostgreSQL 16.10, Redis 7.4.2, RabbitMQ 4.4.0 и nginx 1.28.0.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
python3 -m venv .venv
|
||||||
|
.venv/bin/python -m pip install -r requirements.txt -r requirements-dev.txt
|
||||||
|
make init
|
||||||
|
make up
|
||||||
|
make test
|
||||||
|
make down
|
||||||
|
```
|
||||||
|
|
||||||
|
`make init` создаёт отсутствующие .env и secrets/, миграции и bootstrap выполняются
|
||||||
|
при запуске. Все настройки берутся из .env; образец — .env.example.
|
||||||
|
HTTP: http://localhost:18081. Внутренние хранилища и брокер наружу не открыты.
|
||||||
|
`make down` сохраняет данные; `docker compose down --volumes` намеренно удаляет их.
|
||||||
|
При обновлении схемы сначала остановить старые процессы, сохранив volumes.
|
||||||
|
|
||||||
## Архитектура и решения
|
## Архитектура и решения
|
||||||
|
|
||||||
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab4, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
|
API → приватный MinIO staging + PostgreSQL job/outbox → RabbitMQ → worker →
|
||||||
|
конечные объекты MinIO + PostgreSQL metadata. Redis хранит сессии.
|
||||||
|
Сохранены аутентификация, чаты, сообщения, пагинация и идемпотентность предыдущих работ.
|
||||||
|
Загрузка возвращает **202 queued** после устойчивого приёма. Состояния:
|
||||||
|
queued / processing / ready / failed; скачивание до ready возвращает 409.
|
||||||
|
|
||||||
|
RabbitMQ использует устойчивую очередь, сохранение сообщений на диск и подтверждения.
|
||||||
|
Publisher отмечает публикацию после подтверждения брокера; worker подтверждает доставку
|
||||||
|
после фиксации результата. Outbox восстанавливает разрыв COMMIT → publish.
|
||||||
|
Фиксированные ключи объектов, блокировка job и lease защищают от повторов и устаревшего worker.
|
||||||
|
|
||||||
|
Original не перекодируется. Варианты: JPEG thumbnail ≤256×256, центральный JPEG crop
|
||||||
|
128×128 и WebP ≤1280×1280. JPEG quality=85, WebP quality=80/method=4; EXIF orientation
|
||||||
|
учитывается, EXIF/ICC/XMP удаляются. Для JPEG alpha заменяется белой подложкой,
|
||||||
|
WebP сохраняет прозрачность. Лимиты: 10 MiB, 40 млн пикселей, timeout 60 секунд,
|
||||||
|
768 MiB и 20 секунд CPU на дочерний процесс; concurrency=2, prefetch=1, lease=90 секунд.
|
||||||
|
|
||||||
|
Повторы ограничены пятью попытками. Задержка со случайным разбросом:
|
||||||
|
c=min(120, 5×2^(n−1)), delay∈[c/2,c]. Постоянные ошибки завершаются сразу.
|
||||||
|
Реестр failed и аудит находятся в PostgreSQL; явный replay проверяет исходник,
|
||||||
|
выделяет новый бюджет и не запускает уже готовую job. Staging активных jobs сохраняется;
|
||||||
|
ready очищается через сутки, failed — через семь суток.
|
||||||
|
|
||||||
|
Операторские команды:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
docker compose run --rm --no-deps worker python -m app.core.job_admin list-failed
|
||||||
|
docker compose run --rm --no-deps worker python -m app.core.job_admin replay JOB_ID --request-id REQUEST_UUID --actor operator --reason dependency_restored
|
||||||
|
```
|
||||||
|
|
||||||
|
JOB_ID и REQUEST_UUID заменить UUID. Повтор команды с тем же request-id безопасен.
|
||||||
|
|
||||||
## Доказательства критериев
|
## Доказательства критериев
|
||||||
|
|
||||||
|
`make test-suite` поднимает отдельный стенд, проверяет pipeline и удаляет контейнеры,
|
||||||
|
сети и volumes при завершении, включая ошибку. Полная сохранённая регрессия запускается
|
||||||
|
отдельно через `make test-unit`; отдельные сервисы — `test-nginx` и `test-storage`.
|
||||||
|
Проверки pipeline: 42 теста. Полная сохранённая регрессия: 201 тест.
|
||||||
|
Публичные smoke/persistence/failover прошли. Для отдельных pytest-прогонов нужен
|
||||||
|
`make test-stand-up`; после проверки — `make test-stand-destroy`.
|
||||||
|
|
||||||
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
||||||
| --- | --- | --- | --- |
|
| --- | --- | --- | --- |
|
||||||
| Публичный smoke | make test | TODO | TODO |
|
| Публичный smoke | make test | 202 → ready, неизменный original, JPEG thumbnail | evidence/verification.json |
|
||||||
| Остальные пункты уровня 3 | TODO | TODO | TODO |
|
| Устойчивый приём и сохранение возможностей lab4 | DOCKER_TESTS=1 make test-unit; make test-persistence | Данные и сессия сохранены, загрузка асинхронная | evidence/verification.json |
|
||||||
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
|
| Отдельные broker/worker, поздний ack | make test-jobs | Сообщения сохранены, очередь подтверждена | evidence/jobs.json |
|
||||||
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
|
| Состояния, ограниченные повторы, идемпотентность | make test-pipeline; make test-jobs | failed после бюджета, дубли не создают новые объекты | evidence/verification.json |
|
||||||
|
| Остановка worker и рестарт API/брокера | make test-jobs | То же вложение стало ready, сообщение пережило рестарт | evidence/jobs.json |
|
||||||
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
|
| Crop/WebP и ограничения worker | make test-pipeline; make test-jobs | Форматы/размеры, metadata и лимиты проверены | evidence/verification.json |
|
||||||
|
| Разрыв COMMIT → publish | make test-outbox | SIGKILL publisher, затем ready без нового upload | evidence/verification.json |
|
||||||
|
| Backoff/jitter, failed registry, replay | make test-jobs | Постоянные/временные ошибки различаются, replay безопасен | evidence/jobs.json |
|
||||||
|
| Crash после PUT до ack, lease и cleanup | make test-jobs; make test-pipeline | Тот же результат, prefetch=1; активный staging сохранён | evidence/jobs.json; evidence/verification.json |
|
||||||
|
|
||||||
## Эксперименты и отказы
|
## Эксперименты и отказы
|
||||||
|
|
||||||
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
|
|
||||||
|
|
||||||
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
|
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
|
||||||
| --- | --- | --- | --- |
|
| --- | --- | --- | --- |
|
||||||
| TODO | TODO | TODO | TODO |
|
| Crash после PUT до COMMIT | Повтор той же job | ready на второй попытке, те же четыре объекта и SHA-256 | После lease/retry; потерь нет |
|
||||||
|
| Crash после COMMIT до ack | Готовый дубль без обработки | Metadata и Last-Modified не изменились | После доставки; потерь нет |
|
||||||
|
| Временная / постоянная ошибка | Ограниченный retry / немедленный failed | Пять / одна попытка; явный replay завершился ready | В пределах бюджета; потерь нет |
|
||||||
|
|
||||||
## Ограничения и следующие шаги
|
## Ограничения и следующие шаги
|
||||||
|
|
||||||
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
|
Один Docker-хост, без репликации хранилищ и защиты от потери диска. Очередь не имеет
|
||||||
|
admission control; pipeline фиксирован на версии 1. Доставка как минимум один раз,
|
||||||
|
с идемпотентным результатом.
|
||||||
|
|||||||
+38
@@ -0,0 +1,38 @@
|
|||||||
|
[alembic]
|
||||||
|
script_location = migrations
|
||||||
|
prepend_sys_path = .
|
||||||
|
path_separator = os
|
||||||
|
|
||||||
|
[loggers]
|
||||||
|
keys = root,sqlalchemy,alembic
|
||||||
|
|
||||||
|
[handlers]
|
||||||
|
keys = console
|
||||||
|
|
||||||
|
[formatters]
|
||||||
|
keys = generic
|
||||||
|
|
||||||
|
[logger_root]
|
||||||
|
level = WARNING
|
||||||
|
handlers = console
|
||||||
|
qualname =
|
||||||
|
|
||||||
|
[logger_sqlalchemy]
|
||||||
|
level = WARNING
|
||||||
|
handlers =
|
||||||
|
qualname = sqlalchemy.engine
|
||||||
|
|
||||||
|
[logger_alembic]
|
||||||
|
level = INFO
|
||||||
|
handlers =
|
||||||
|
qualname = alembic
|
||||||
|
|
||||||
|
[handler_console]
|
||||||
|
class = StreamHandler
|
||||||
|
args = (sys.stderr,)
|
||||||
|
level = NOTSET
|
||||||
|
formatter = generic
|
||||||
|
|
||||||
|
[formatter_generic]
|
||||||
|
format = %(levelname)-5.5s [%(name)s] %(message)s
|
||||||
|
datefmt = %H:%M:%S
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
from app.main import main
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
"""Остановка фоновых процессов и безопасные сообщения об отказах."""
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import logging
|
||||||
|
import signal
|
||||||
|
|
||||||
|
from aio_pika.exceptions import AMQPException, ChannelInvalidStateError
|
||||||
|
from sqlalchemy.exc import InterfaceError, OperationalError
|
||||||
|
from sqlalchemy.exc import TimeoutError as PoolTimeoutError
|
||||||
|
|
||||||
|
TRANSIENT_JOB_ERRORS = (
|
||||||
|
AMQPException,
|
||||||
|
ChannelInvalidStateError,
|
||||||
|
InterfaceError,
|
||||||
|
OperationalError,
|
||||||
|
PoolTimeoutError,
|
||||||
|
OSError,
|
||||||
|
)
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def shutdown_event() -> asyncio.Event:
|
||||||
|
stop = asyncio.Event()
|
||||||
|
loop = asyncio.get_running_loop()
|
||||||
|
for signum in (signal.SIGTERM, signal.SIGINT):
|
||||||
|
loop.add_signal_handler(signum, stop.set)
|
||||||
|
return stop
|
||||||
|
|
||||||
|
|
||||||
|
async def wait_or_stop(stop: asyncio.Event, seconds: float) -> None:
|
||||||
|
try:
|
||||||
|
async with asyncio.timeout(seconds):
|
||||||
|
await stop.wait()
|
||||||
|
except TimeoutError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def log_background_error(operation: str, error: Exception) -> None:
|
||||||
|
# SDK/DB exceptions могут содержать параметры и credentials; выводим только тип.
|
||||||
|
logger.warning("%s: %s", operation, type(error).__name__)
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
"""Периодическая компенсация незавершённых загрузок; отдельный Compose-сервис."""
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import asyncio
|
||||||
|
from dataclasses import asdict
|
||||||
|
import json
|
||||||
|
|
||||||
|
from app.core.config import Settings
|
||||||
|
from app.core.jobs import JobSettings
|
||||||
|
from app.core.database import create_db_engine, create_session_factory
|
||||||
|
from app.core.s3 import S3Settings
|
||||||
|
from app.domain.attachments import reconcile_attachments
|
||||||
|
from app.domain.attachment_jobs import cleanup_staging
|
||||||
|
from app.errors.handlers import RECOVERABLE_STORAGE_ERRORS, log_recoverable_error
|
||||||
|
from app.storage.s3 import create_s3_storage
|
||||||
|
|
||||||
|
|
||||||
|
async def cleanup_once(factory, storage, apply: bool, jobs: JobSettings) -> bool:
|
||||||
|
completed = True
|
||||||
|
try:
|
||||||
|
async with factory() as session:
|
||||||
|
result = await reconcile_attachments(session, storage, dry_run=not apply)
|
||||||
|
staging = await cleanup_staging(session, storage, jobs, apply=apply)
|
||||||
|
print(
|
||||||
|
json.dumps({**asdict(result), "terminal_staging": staging}), flush=True
|
||||||
|
)
|
||||||
|
except RECOVERABLE_STORAGE_ERRORS as error:
|
||||||
|
log_recoverable_error("Reconciliation", error)
|
||||||
|
completed = False
|
||||||
|
return completed
|
||||||
|
|
||||||
|
|
||||||
|
async def run(once: bool, apply: bool) -> None:
|
||||||
|
settings = Settings() # type: ignore
|
||||||
|
s3_settings = S3Settings()
|
||||||
|
jobs = JobSettings()
|
||||||
|
engine = create_db_engine(settings)
|
||||||
|
factory = create_session_factory(engine)
|
||||||
|
storage = create_s3_storage(s3_settings)
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
completed = await cleanup_once(factory, storage, apply, jobs)
|
||||||
|
if once:
|
||||||
|
if not completed:
|
||||||
|
raise SystemExit(1)
|
||||||
|
break
|
||||||
|
await asyncio.sleep(s3_settings.cleanup_interval_seconds)
|
||||||
|
finally:
|
||||||
|
await storage.aclose()
|
||||||
|
await engine.dispose()
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
parser = argparse.ArgumentParser(description="Сверка журнала загрузок MinIO/БД")
|
||||||
|
parser.add_argument("--once", action="store_true")
|
||||||
|
parser.add_argument(
|
||||||
|
"--apply",
|
||||||
|
action="store_true",
|
||||||
|
help="удалять безопасные кандидаты; по умолчанию dry-run",
|
||||||
|
)
|
||||||
|
args = parser.parse_args()
|
||||||
|
asyncio.run(run(args.once, args.apply))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -0,0 +1,129 @@
|
|||||||
|
from enum import StrEnum
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Literal, Self
|
||||||
|
|
||||||
|
from pydantic import Field, SecretStr, model_validator
|
||||||
|
from pydantic_settings import (
|
||||||
|
BaseSettings,
|
||||||
|
PydanticBaseSettingsSource,
|
||||||
|
SettingsConfigDict,
|
||||||
|
)
|
||||||
|
from sqlalchemy import URL
|
||||||
|
from app.core.s3 import S3Settings
|
||||||
|
|
||||||
|
ASYNC_DRIVER = "postgresql+asyncpg"
|
||||||
|
SECRETS_DIR = next(
|
||||||
|
(
|
||||||
|
path
|
||||||
|
for path in (
|
||||||
|
Path("/run/secrets"),
|
||||||
|
Path(__file__).resolve().parents[2] / "secrets",
|
||||||
|
)
|
||||||
|
if (path / "db_password").is_file()
|
||||||
|
),
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class LogLevel(StrEnum):
|
||||||
|
DEBUG = "DEBUG"
|
||||||
|
INFO = "INFO"
|
||||||
|
WARNING = "WARNING"
|
||||||
|
ERROR = "ERROR"
|
||||||
|
CRITICAL = "CRITICAL"
|
||||||
|
|
||||||
|
|
||||||
|
class Settings(BaseSettings):
|
||||||
|
s3_enabled: bool = True
|
||||||
|
s3: S3Settings = Field(default_factory=S3Settings)
|
||||||
|
app_host: str
|
||||||
|
app_port: int = Field(ge=1, le=65535)
|
||||||
|
app_env: str
|
||||||
|
log_level: LogLevel = LogLevel.INFO
|
||||||
|
|
||||||
|
max_messages: int = Field(ge=1)
|
||||||
|
max_in_flight: int = Field(ge=1)
|
||||||
|
|
||||||
|
session_ttl_seconds: int = Field(default=3600, ge=1)
|
||||||
|
session_store: Literal["redis"] = "redis"
|
||||||
|
|
||||||
|
redis_host: str = "localhost"
|
||||||
|
redis_port: int = Field(default=6379, ge=1, le=65535)
|
||||||
|
redis_db: int = Field(default=0, ge=0)
|
||||||
|
redis_password: SecretStr | None = None
|
||||||
|
|
||||||
|
redis_max_connections: int = Field(default=20, ge=1)
|
||||||
|
redis_pool_timeout_seconds: float = Field(default=1.0, gt=0)
|
||||||
|
redis_connect_timeout_seconds: float = Field(default=1.0, gt=0)
|
||||||
|
redis_socket_timeout_seconds: float = Field(default=0.5, gt=0)
|
||||||
|
|
||||||
|
database_url: SecretStr | None = None
|
||||||
|
db_host: str | None = None
|
||||||
|
db_port: int = Field(default=5432, ge=1, le=65535)
|
||||||
|
db_name: str | None = None
|
||||||
|
db_user: str | None = None
|
||||||
|
db_password: SecretStr | None = None
|
||||||
|
|
||||||
|
db_pool_size: int = Field(default=5, ge=1)
|
||||||
|
db_max_overflow: int = Field(default=5, ge=0)
|
||||||
|
db_pool_timeout_seconds: float = Field(default=3.0, gt=0)
|
||||||
|
db_connect_timeout_seconds: int = Field(default=3, ge=1)
|
||||||
|
db_statement_timeout_ms: int = Field(default=5000, ge=1)
|
||||||
|
|
||||||
|
instance_id: str | None = None
|
||||||
|
idempotency_ttl_seconds: int = Field(default=86400, ge=1)
|
||||||
|
shutdown_drain_seconds: float = Field(default=1, ge=0)
|
||||||
|
graceful_shutdown_timeout_seconds: int = Field(default=5, ge=1)
|
||||||
|
readiness_timeout_seconds: float = Field(default=2, gt=0, lt=2.5)
|
||||||
|
|
||||||
|
model_config = SettingsConfigDict(
|
||||||
|
env_file=".env",
|
||||||
|
env_file_encoding="utf-8",
|
||||||
|
case_sensitive=False,
|
||||||
|
extra="ignore",
|
||||||
|
secrets_dir=SECRETS_DIR,
|
||||||
|
)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def settings_customise_sources(
|
||||||
|
cls,
|
||||||
|
settings_cls: type[BaseSettings],
|
||||||
|
init_settings: PydanticBaseSettingsSource,
|
||||||
|
env_settings: PydanticBaseSettingsSource,
|
||||||
|
dotenv_settings: PydanticBaseSettingsSource,
|
||||||
|
file_secret_settings: PydanticBaseSettingsSource,
|
||||||
|
) -> tuple[PydanticBaseSettingsSource, ...]:
|
||||||
|
"""Явные параметры и файлы имеют приоритет над окружением."""
|
||||||
|
return init_settings, file_secret_settings, env_settings, dotenv_settings
|
||||||
|
|
||||||
|
@model_validator(mode="after")
|
||||||
|
def _require_database_source(self) -> Self:
|
||||||
|
if self.shutdown_drain_seconds + self.graceful_shutdown_timeout_seconds >= 8:
|
||||||
|
raise ValueError(
|
||||||
|
"Drain и graceful shutdown должны суммарно занимать менее 8 секунд"
|
||||||
|
)
|
||||||
|
if self.database_url is not None:
|
||||||
|
if not self.database_url.get_secret_value().startswith(ASYNC_DRIVER):
|
||||||
|
raise ValueError(f"DATABASE_URL должен начинаться с {ASYNC_DRIVER}://")
|
||||||
|
return self
|
||||||
|
|
||||||
|
if not (self.db_host and self.db_name and self.db_user and self.db_password):
|
||||||
|
raise ValueError(
|
||||||
|
"Нужны DATABASE_URL либо DB_HOST, DB_NAME, DB_USER и секрет db_password"
|
||||||
|
)
|
||||||
|
return self
|
||||||
|
|
||||||
|
def get_database_url(self) -> str:
|
||||||
|
if self.database_url is not None:
|
||||||
|
return self.database_url.get_secret_value()
|
||||||
|
|
||||||
|
assert self.db_password is not None
|
||||||
|
url = URL.create(
|
||||||
|
ASYNC_DRIVER,
|
||||||
|
username=self.db_user,
|
||||||
|
password=self.db_password.get_secret_value(),
|
||||||
|
host=self.db_host,
|
||||||
|
port=self.db_port,
|
||||||
|
database=self.db_name,
|
||||||
|
)
|
||||||
|
return url.render_as_string(hide_password=False)
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
from sqlalchemy.ext.asyncio import (
|
||||||
|
AsyncEngine,
|
||||||
|
AsyncSession,
|
||||||
|
async_sessionmaker,
|
||||||
|
create_async_engine,
|
||||||
|
)
|
||||||
|
|
||||||
|
from app.core.config import Settings
|
||||||
|
|
||||||
|
|
||||||
|
def create_db_engine(settings: Settings) -> AsyncEngine:
|
||||||
|
return create_async_engine(
|
||||||
|
settings.get_database_url(),
|
||||||
|
pool_size=settings.db_pool_size,
|
||||||
|
max_overflow=settings.db_max_overflow,
|
||||||
|
pool_timeout=settings.db_pool_timeout_seconds,
|
||||||
|
pool_pre_ping=True,
|
||||||
|
connect_args={
|
||||||
|
"timeout": settings.db_connect_timeout_seconds,
|
||||||
|
"server_settings": {
|
||||||
|
"statement_timeout": str(settings.db_statement_timeout_ms)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def create_session_factory(engine: AsyncEngine) -> async_sessionmaker[AsyncSession]:
|
||||||
|
return async_sessionmaker(bind=engine, expire_on_commit=False, autoflush=False)
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
"""Качество вариантов и пределы отдельного процесса обработки."""
|
||||||
|
|
||||||
|
from pydantic import Field
|
||||||
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||||
|
|
||||||
|
|
||||||
|
class ImageSettings(BaseSettings):
|
||||||
|
model_config = SettingsConfigDict(
|
||||||
|
env_prefix="IMAGE_", env_file=".env", extra="ignore"
|
||||||
|
)
|
||||||
|
|
||||||
|
jpeg_quality: int = Field(default=85, ge=1, le=95)
|
||||||
|
webp_quality: int = Field(default=80, ge=1, le=100)
|
||||||
|
webp_method: int = Field(default=4, ge=0, le=6)
|
||||||
|
memory_mib: int = Field(default=768, ge=256, le=1024)
|
||||||
|
cpu_seconds: int = Field(default=20, ge=1, le=120)
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
"""Одноразовый Linux-процесс декодирования с пределами памяти и CPU"""
|
||||||
|
|
||||||
|
from pathlib import Path
|
||||||
|
import resource
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from app.domain.image_validation import decode_image
|
||||||
|
from app.errors.exceptions import IMAGE_WORKER_ERRORS, image_worker_error_code
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> int:
|
||||||
|
resource.setrlimit(resource.RLIMIT_AS, (512 * 1024 * 1024, 512 * 1024 * 1024))
|
||||||
|
resource.setrlimit(resource.RLIMIT_CPU, (10, 10))
|
||||||
|
path, content_type, maximum = sys.argv[1:]
|
||||||
|
status = 0
|
||||||
|
try:
|
||||||
|
decode_image(Path(path), content_type, int(maximum))
|
||||||
|
except IMAGE_WORKER_ERRORS as error:
|
||||||
|
status = image_worker_error_code(error)
|
||||||
|
return status
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
raise SystemExit(main())
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
"""Идентификатор API-реплики и заголовок X-Instance-Id."""
|
||||||
|
|
||||||
|
import os
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
|
||||||
|
from starlette.requests import Request
|
||||||
|
from starlette.responses import Response
|
||||||
|
|
||||||
|
INSTANCE_ID_HEADER = "X-Instance-Id"
|
||||||
|
|
||||||
|
|
||||||
|
def resolve_instance_id(configured: str | None) -> str:
|
||||||
|
"""Выбирает INSTANCE_ID, HOSTNAME или UUID при старте приложения."""
|
||||||
|
if configured and configured.strip():
|
||||||
|
return configured.strip()
|
||||||
|
|
||||||
|
hostname = os.getenv("HOSTNAME", "").strip()
|
||||||
|
if hostname:
|
||||||
|
return hostname
|
||||||
|
|
||||||
|
return str(uuid4())
|
||||||
|
|
||||||
|
|
||||||
|
class InstanceIdMiddleware(BaseHTTPMiddleware):
|
||||||
|
"""Добавляет сохранённый идентификатор реплики в ответ."""
|
||||||
|
|
||||||
|
def __init__(self, app, instance_id: str) -> None:
|
||||||
|
super().__init__(app)
|
||||||
|
self.instance_id = instance_id
|
||||||
|
|
||||||
|
async def dispatch(
|
||||||
|
self,
|
||||||
|
request: Request,
|
||||||
|
call_next: RequestResponseEndpoint,
|
||||||
|
) -> Response:
|
||||||
|
response = await call_next(request)
|
||||||
|
response.headers[INSTANCE_ID_HEADER] = self.instance_id
|
||||||
|
|
||||||
|
return response
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
"""Операторский интерфейс failed registry. Доступ требует credentials стенда."""
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import asyncio
|
||||||
|
import json
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from app.core.background import log_background_error
|
||||||
|
from app.core.config import Settings
|
||||||
|
from app.core.database import create_db_engine, create_session_factory
|
||||||
|
from app.core.jobs import JobSettings
|
||||||
|
from app.domain.job_admin import (
|
||||||
|
REPLAY_REASONS,
|
||||||
|
history,
|
||||||
|
list_failed,
|
||||||
|
replay_failed_job,
|
||||||
|
show_job,
|
||||||
|
)
|
||||||
|
from app.errors.exceptions import JobOperationError
|
||||||
|
from app.storage.s3 import create_s3_storage
|
||||||
|
|
||||||
|
|
||||||
|
async def execute(args):
|
||||||
|
settings = Settings()
|
||||||
|
engine = create_db_engine(settings)
|
||||||
|
storage = None
|
||||||
|
try:
|
||||||
|
factory = create_session_factory(engine)
|
||||||
|
async with factory() as session:
|
||||||
|
if args.command == "list-failed":
|
||||||
|
result = await list_failed(session, args.limit)
|
||||||
|
elif args.command == "show":
|
||||||
|
result = await show_job(session, args.job_id)
|
||||||
|
elif args.command == "history":
|
||||||
|
result = await history(session, args.job_id)
|
||||||
|
else:
|
||||||
|
storage = create_s3_storage(settings.s3)
|
||||||
|
result = await replay_failed_job(
|
||||||
|
session,
|
||||||
|
storage,
|
||||||
|
JobSettings(),
|
||||||
|
args.job_id,
|
||||||
|
request_id=args.request_id,
|
||||||
|
actor=args.actor,
|
||||||
|
reason=args.reason,
|
||||||
|
)
|
||||||
|
print(json.dumps(result, default=str), flush=True)
|
||||||
|
finally:
|
||||||
|
if storage is not None:
|
||||||
|
await storage.aclose()
|
||||||
|
await engine.dispose()
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description="Реестр failed и явный replay с аудитом"
|
||||||
|
)
|
||||||
|
commands = parser.add_subparsers(dest="command", required=True)
|
||||||
|
listing = commands.add_parser("list-failed")
|
||||||
|
listing.add_argument(
|
||||||
|
"--limit", type=int, choices=range(1, 501), default=50, metavar="1..500"
|
||||||
|
)
|
||||||
|
for name in ("show", "history", "replay"):
|
||||||
|
command = commands.add_parser(name)
|
||||||
|
command.add_argument("job_id", type=UUID)
|
||||||
|
if name == "replay":
|
||||||
|
command.add_argument("--request-id", type=UUID, required=True)
|
||||||
|
command.add_argument("--actor", required=True)
|
||||||
|
command.add_argument("--reason", choices=REPLAY_REASONS, required=True)
|
||||||
|
args = parser.parse_args()
|
||||||
|
try:
|
||||||
|
asyncio.run(execute(args))
|
||||||
|
except JobOperationError as error:
|
||||||
|
print(json.dumps({"error_code": error.code}), flush=True)
|
||||||
|
raise SystemExit(2) from None
|
||||||
|
except Exception as error:
|
||||||
|
log_background_error("Job administration failed", error)
|
||||||
|
raise SystemExit(1) from None
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -0,0 +1,170 @@
|
|||||||
|
"""Отдельные consumers с ограниченной конкуренцией; API не выполняет resize."""
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import asyncio
|
||||||
|
from dataclasses import dataclass
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
import signal
|
||||||
|
from uuid import UUID, uuid4
|
||||||
|
|
||||||
|
from PIL import features
|
||||||
|
from pydantic import ValidationError
|
||||||
|
|
||||||
|
from app.core.background import (
|
||||||
|
TRANSIENT_JOB_ERRORS,
|
||||||
|
log_background_error,
|
||||||
|
shutdown_event,
|
||||||
|
wait_or_stop,
|
||||||
|
)
|
||||||
|
from app.core.config import Settings
|
||||||
|
from app.core.database import create_db_engine, create_session_factory
|
||||||
|
from app.core.jobs import JobSettings
|
||||||
|
from app.domain.attachment_jobs import process_attachment_job
|
||||||
|
from app.errors.exceptions import ProcessingError
|
||||||
|
from app.schemas.job import JobMessage
|
||||||
|
from app.storage.broker import JobBroker
|
||||||
|
from app.storage.s3 import create_s3_storage
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class Faults:
|
||||||
|
"""Только явные CLI-флаги; обычный worker не включает инъекции сбоев."""
|
||||||
|
|
||||||
|
crash_after_object: UUID | None = None
|
||||||
|
crash_before_ack: UUID | None = None
|
||||||
|
fail_transient: UUID | None = None
|
||||||
|
fail_permanent: UUID | None = None
|
||||||
|
object_name: str = "optimized"
|
||||||
|
|
||||||
|
def kill(self, attachment_id: UUID, phase: str) -> None:
|
||||||
|
print(
|
||||||
|
json.dumps({"fault": phase, "attachment_id": str(attachment_id)}),
|
||||||
|
flush=True,
|
||||||
|
)
|
||||||
|
os.kill(os.getpid(), signal.SIGKILL)
|
||||||
|
|
||||||
|
def after_object(self, attachment_id: UUID, name: str) -> None:
|
||||||
|
if attachment_id == self.crash_after_object and name == self.object_name:
|
||||||
|
self.kill(attachment_id, "after_object:" + name)
|
||||||
|
|
||||||
|
def before_ack(self, attachment_id: UUID) -> None:
|
||||||
|
if attachment_id == self.crash_before_ack:
|
||||||
|
self.kill(attachment_id, "before_ack")
|
||||||
|
|
||||||
|
def before_processing(self, attachment_id: UUID) -> None:
|
||||||
|
if attachment_id == self.fail_transient:
|
||||||
|
raise TimeoutError("injected transient failure")
|
||||||
|
if attachment_id == self.fail_permanent:
|
||||||
|
raise ProcessingError("injected_permanent")
|
||||||
|
|
||||||
|
|
||||||
|
async def consume_connected(
|
||||||
|
factory,
|
||||||
|
storage,
|
||||||
|
broker: JobBroker,
|
||||||
|
settings: JobSettings,
|
||||||
|
stop: asyncio.Event,
|
||||||
|
faults: Faults,
|
||||||
|
) -> None:
|
||||||
|
consumer = uuid4().hex
|
||||||
|
while not stop.is_set():
|
||||||
|
try:
|
||||||
|
await broker.ensure_ready()
|
||||||
|
delivery = await broker.receive(consumer)
|
||||||
|
if delivery is None:
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
message = JobMessage.model_validate_json(delivery.payload)
|
||||||
|
except ValidationError:
|
||||||
|
await broker.reject_invalid(delivery.id)
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
async with factory() as session:
|
||||||
|
acknowledge = await process_attachment_job(
|
||||||
|
session,
|
||||||
|
storage,
|
||||||
|
message,
|
||||||
|
settings,
|
||||||
|
after_object=faults.after_object,
|
||||||
|
before_processing=faults.before_processing,
|
||||||
|
)
|
||||||
|
except ProcessingError:
|
||||||
|
await broker.reject_invalid(delivery.id)
|
||||||
|
else:
|
||||||
|
if acknowledge:
|
||||||
|
faults.before_ack(message.attachment_id)
|
||||||
|
await broker.acknowledge_job(delivery.id)
|
||||||
|
else:
|
||||||
|
await broker.defer_job(delivery.id)
|
||||||
|
except TRANSIENT_JOB_ERRORS as error:
|
||||||
|
log_background_error("Worker delivery deferred", error)
|
||||||
|
await broker.aclose()
|
||||||
|
await wait_or_stop(stop, settings.publisher_poll_seconds)
|
||||||
|
|
||||||
|
|
||||||
|
async def consume(factory, storage, settings, stop, faults) -> None:
|
||||||
|
broker = JobBroker(settings)
|
||||||
|
try:
|
||||||
|
await consume_connected(factory, storage, broker, settings, stop, faults)
|
||||||
|
finally:
|
||||||
|
await broker.aclose()
|
||||||
|
|
||||||
|
|
||||||
|
async def run_worker(faults: Faults) -> None:
|
||||||
|
settings = Settings()
|
||||||
|
jobs = JobSettings()
|
||||||
|
logging.basicConfig(level=settings.log_level)
|
||||||
|
if not features.check("webp"):
|
||||||
|
logging.error("Pillow requires WebP support for the image worker")
|
||||||
|
raise RuntimeError("WebP codec is unavailable")
|
||||||
|
stop = shutdown_event()
|
||||||
|
engine = create_db_engine(settings)
|
||||||
|
storage = None
|
||||||
|
try:
|
||||||
|
storage = create_s3_storage(settings.s3)
|
||||||
|
factory = create_session_factory(engine)
|
||||||
|
async with asyncio.TaskGroup() as group:
|
||||||
|
tasks = [
|
||||||
|
group.create_task(consume(factory, storage, jobs, stop, faults))
|
||||||
|
for _ in range(jobs.worker_concurrency)
|
||||||
|
]
|
||||||
|
await stop.wait()
|
||||||
|
# Отмена убивает resize-процесс, дожидается SDK и не отправляет ack.
|
||||||
|
for task in tasks:
|
||||||
|
task.cancel()
|
||||||
|
finally:
|
||||||
|
if storage is not None:
|
||||||
|
await storage.aclose()
|
||||||
|
await engine.dispose()
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description="Image worker с явными fault-injection hooks"
|
||||||
|
)
|
||||||
|
modes = parser.add_mutually_exclusive_group()
|
||||||
|
for flag in (
|
||||||
|
"crash-after-object",
|
||||||
|
"crash-before-ack",
|
||||||
|
"fail-transient",
|
||||||
|
"fail-permanent",
|
||||||
|
):
|
||||||
|
modes.add_argument("--" + flag, type=UUID, metavar="ATTACHMENT_ID")
|
||||||
|
parser.add_argument(
|
||||||
|
"--object-name",
|
||||||
|
choices=("original", "thumbnail", "crop", "optimized"),
|
||||||
|
default="optimized",
|
||||||
|
)
|
||||||
|
args = parser.parse_args()
|
||||||
|
faults = Faults(**vars(args))
|
||||||
|
try:
|
||||||
|
asyncio.run(run_worker(faults))
|
||||||
|
except Exception as error:
|
||||||
|
log_background_error("Worker stopped", error)
|
||||||
|
raise SystemExit(1) from None
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
"""Настройки очереди из .env; credentials брокера читаются отдельно из файла."""
|
||||||
|
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Self
|
||||||
|
|
||||||
|
from pydantic import Field, model_validator
|
||||||
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||||
|
|
||||||
|
|
||||||
|
class JobSettings(BaseSettings):
|
||||||
|
model_config = SettingsConfigDict(env_file=".env", extra="ignore")
|
||||||
|
|
||||||
|
broker_host: str = "broker"
|
||||||
|
broker_port: int = Field(default=5672, ge=1, le=65535)
|
||||||
|
broker_password_file: Path = Path("/run/secrets/broker_password")
|
||||||
|
broker_queue: str = "attachment-jobs"
|
||||||
|
broker_user: str = "messenger"
|
||||||
|
broker_vhost: str = "/"
|
||||||
|
worker_concurrency: int = Field(default=2, ge=1, le=4)
|
||||||
|
job_max_attempts: int = Field(default=5, ge=1, le=100)
|
||||||
|
job_timeout_seconds: int = Field(default=60, ge=10, le=300)
|
||||||
|
job_lease_seconds: int = Field(default=90, ge=20, le=600)
|
||||||
|
job_retry_seconds: int = Field(default=5, ge=1, le=300)
|
||||||
|
job_retry_max_seconds: int = Field(default=120, ge=1, le=3600)
|
||||||
|
failed_staging_retention_seconds: int = Field(default=604800, ge=300)
|
||||||
|
publisher_poll_seconds: float = Field(default=1, gt=0, le=10)
|
||||||
|
staging_retention_seconds: int = Field(default=86400, ge=300)
|
||||||
|
|
||||||
|
@model_validator(mode="after")
|
||||||
|
def validate_lease(self) -> Self:
|
||||||
|
if self.job_lease_seconds < self.job_timeout_seconds + 30:
|
||||||
|
raise ValueError(
|
||||||
|
"Lease должен превышать timeout задания минимум на 30 секунд"
|
||||||
|
)
|
||||||
|
if self.job_retry_max_seconds < self.job_retry_seconds:
|
||||||
|
raise ValueError("Retry maximum must be at least the base delay")
|
||||||
|
return self
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
"""Готовность реплики и мягкое завершение сервера."""
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
from types import FrameType
|
||||||
|
|
||||||
|
import uvicorn
|
||||||
|
|
||||||
|
|
||||||
|
class Lifecycle:
|
||||||
|
"""Хранит состояние завершения реплики."""
|
||||||
|
|
||||||
|
def __init__(self) -> None:
|
||||||
|
self._draining = False
|
||||||
|
|
||||||
|
@property
|
||||||
|
def draining(self) -> bool:
|
||||||
|
return self._draining
|
||||||
|
|
||||||
|
def start_draining(self) -> None:
|
||||||
|
"""Отключает приём новых бизнес-запросов."""
|
||||||
|
self._draining = True
|
||||||
|
|
||||||
|
|
||||||
|
class GracefulServer(uvicorn.Server):
|
||||||
|
"""Оставляет время для исключения завершающейся реплики."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self, config: uvicorn.Config, lifecycle: Lifecycle, drain_seconds: float
|
||||||
|
) -> None:
|
||||||
|
super().__init__(config)
|
||||||
|
self.lifecycle = lifecycle
|
||||||
|
self.drain_seconds = drain_seconds
|
||||||
|
self._drain_task: asyncio.Task[None] | None = None
|
||||||
|
|
||||||
|
def handle_exit(self, sig: int, frame: FrameType | None) -> None:
|
||||||
|
"""Первый сигнал запускает drain, повторный ускоряет остановку."""
|
||||||
|
if self._drain_task is not None:
|
||||||
|
if not self._drain_task.done():
|
||||||
|
self._drain_task.cancel()
|
||||||
|
super().handle_exit(sig, frame)
|
||||||
|
return
|
||||||
|
self.lifecycle.start_draining()
|
||||||
|
self._drain_task = asyncio.get_running_loop().create_task(
|
||||||
|
self._finish_draining(sig, frame)
|
||||||
|
)
|
||||||
|
|
||||||
|
async def _finish_draining(self, sig: int, frame: FrameType | None) -> None:
|
||||||
|
"""Передаёт остановку Uvicorn после drain-периода."""
|
||||||
|
await asyncio.sleep(self.drain_seconds)
|
||||||
|
super().handle_exit(sig, frame)
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
"""Доставка намерений из PostgreSQL; crash hook включается только явно через CLI."""
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import asyncio
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
import signal
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from app.core.background import (
|
||||||
|
TRANSIENT_JOB_ERRORS,
|
||||||
|
log_background_error,
|
||||||
|
shutdown_event,
|
||||||
|
wait_or_stop,
|
||||||
|
)
|
||||||
|
from app.core.config import Settings
|
||||||
|
from app.core.database import create_db_engine, create_session_factory
|
||||||
|
from app.core.jobs import JobSettings
|
||||||
|
from app.domain.attachment_jobs import publish_next_job, recover_expired_jobs
|
||||||
|
from app.storage.broker import JobBroker
|
||||||
|
|
||||||
|
|
||||||
|
def crash_before_publish(attachment_id: UUID) -> None:
|
||||||
|
"""Реальный SIGKILL после чтения принятой job, до publisher confirm и отметки publish."""
|
||||||
|
print(
|
||||||
|
json.dumps({"fault": "before_publish", "attachment_id": str(attachment_id)}),
|
||||||
|
flush=True,
|
||||||
|
)
|
||||||
|
os.kill(os.getpid(), signal.SIGKILL)
|
||||||
|
|
||||||
|
|
||||||
|
async def run_publisher(crash_attachment_id: UUID | None = None) -> None:
|
||||||
|
settings = Settings()
|
||||||
|
jobs = JobSettings()
|
||||||
|
logging.basicConfig(level=settings.log_level)
|
||||||
|
stop = shutdown_event()
|
||||||
|
engine = create_db_engine(settings)
|
||||||
|
broker = None
|
||||||
|
try:
|
||||||
|
broker = JobBroker(jobs)
|
||||||
|
factory = create_session_factory(engine)
|
||||||
|
while not stop.is_set():
|
||||||
|
published = False
|
||||||
|
try:
|
||||||
|
await broker.ensure_ready()
|
||||||
|
async with factory() as session:
|
||||||
|
await recover_expired_jobs(session, jobs)
|
||||||
|
published = await publish_next_job(
|
||||||
|
session,
|
||||||
|
broker,
|
||||||
|
attachment_id=crash_attachment_id,
|
||||||
|
before_publish=(
|
||||||
|
crash_before_publish
|
||||||
|
if crash_attachment_id is not None
|
||||||
|
else None
|
||||||
|
),
|
||||||
|
)
|
||||||
|
except TRANSIENT_JOB_ERRORS as error:
|
||||||
|
log_background_error("Publisher deferred", error)
|
||||||
|
if not published:
|
||||||
|
await wait_or_stop(stop, jobs.publisher_poll_seconds)
|
||||||
|
finally:
|
||||||
|
if broker is not None:
|
||||||
|
await broker.aclose()
|
||||||
|
await engine.dispose()
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description="Публикация устойчивых jobs в RabbitMQ"
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--crash-before-publish",
|
||||||
|
type=UUID,
|
||||||
|
metavar="ATTACHMENT_ID",
|
||||||
|
help="аварийно завершить этот процесс перед публикацией только указанного вложения",
|
||||||
|
)
|
||||||
|
args = parser.parse_args()
|
||||||
|
try:
|
||||||
|
asyncio.run(run_publisher(args.crash_before_publish))
|
||||||
|
except Exception as error:
|
||||||
|
log_background_error("Publisher stopped", error)
|
||||||
|
raise SystemExit(1) from None
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
from redis.asyncio import BlockingConnectionPool, Redis
|
||||||
|
from redis.asyncio.retry import Retry
|
||||||
|
from redis.backoff import NoBackoff
|
||||||
|
|
||||||
|
from app.core.config import Settings
|
||||||
|
|
||||||
|
|
||||||
|
def create_redis_client(settings: Settings) -> Redis:
|
||||||
|
password = (
|
||||||
|
settings.redis_password.get_secret_value() if settings.redis_password else None
|
||||||
|
)
|
||||||
|
pool = BlockingConnectionPool(
|
||||||
|
host=settings.redis_host,
|
||||||
|
port=settings.redis_port,
|
||||||
|
db=settings.redis_db,
|
||||||
|
password=password,
|
||||||
|
max_connections=settings.redis_max_connections,
|
||||||
|
timeout=settings.redis_pool_timeout_seconds,
|
||||||
|
socket_connect_timeout=settings.redis_connect_timeout_seconds,
|
||||||
|
socket_timeout=settings.redis_socket_timeout_seconds,
|
||||||
|
retry=Retry(NoBackoff(), retries=1),
|
||||||
|
decode_responses=True,
|
||||||
|
)
|
||||||
|
return Redis.from_pool(pool)
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
from collections.abc import AsyncIterator
|
||||||
|
|
||||||
|
from fastapi import Request
|
||||||
|
|
||||||
|
from app.errors.exceptions import rate_limited, unavailable
|
||||||
|
|
||||||
|
|
||||||
|
class RequestCapacity:
|
||||||
|
def __init__(self, max_in_flight: int) -> None:
|
||||||
|
self._max_in_flight = max_in_flight
|
||||||
|
self._in_flight = 0
|
||||||
|
|
||||||
|
@property
|
||||||
|
def in_flight(self) -> int:
|
||||||
|
return self._in_flight
|
||||||
|
|
||||||
|
def try_acquire(self) -> bool:
|
||||||
|
if self._in_flight >= self._max_in_flight:
|
||||||
|
return False
|
||||||
|
self._in_flight += 1
|
||||||
|
return True
|
||||||
|
|
||||||
|
def release(self) -> None:
|
||||||
|
self._in_flight -= 1
|
||||||
|
|
||||||
|
|
||||||
|
def create_request_capacity(max_in_flight: int) -> RequestCapacity:
|
||||||
|
return RequestCapacity(max_in_flight)
|
||||||
|
|
||||||
|
|
||||||
|
async def acquire_request_slot(request: Request) -> AsyncIterator[None]:
|
||||||
|
if request.app.state.lifecycle.draining:
|
||||||
|
raise unavailable("Реплика завершает работу")
|
||||||
|
capacity: RequestCapacity = request.app.state.request_capacity
|
||||||
|
|
||||||
|
if not capacity.try_acquire():
|
||||||
|
raise rate_limited()
|
||||||
|
|
||||||
|
try:
|
||||||
|
yield
|
||||||
|
finally:
|
||||||
|
capacity.release()
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
|
||||||
|
from starlette.requests import Request
|
||||||
|
from starlette.responses import Response
|
||||||
|
|
||||||
|
REQUEST_ID_HEADER = "X-Request-Id"
|
||||||
|
|
||||||
|
|
||||||
|
class RequestIdMiddleware(BaseHTTPMiddleware):
|
||||||
|
async def dispatch(
|
||||||
|
self,
|
||||||
|
request: Request,
|
||||||
|
call_next: RequestResponseEndpoint,
|
||||||
|
) -> Response:
|
||||||
|
request_id = str(uuid4())
|
||||||
|
request.state.request_id = request_id
|
||||||
|
|
||||||
|
response = await call_next(request)
|
||||||
|
response.headers[REQUEST_ID_HEADER] = request_id
|
||||||
|
|
||||||
|
return response
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
"""Конфигурация MinIO; значения credentials читаются только из файлов."""
|
||||||
|
|
||||||
|
from pathlib import Path
|
||||||
|
from urllib.parse import urlsplit
|
||||||
|
|
||||||
|
from pydantic import Field, model_validator
|
||||||
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||||
|
|
||||||
|
|
||||||
|
class S3Settings(BaseSettings):
|
||||||
|
endpoint: str = "http://minio:9000"
|
||||||
|
region: str = "us-east-1"
|
||||||
|
bucket: str = "messenger-attachments"
|
||||||
|
access_key_id_file: Path = Path("/run/secrets/s3_access_key_id")
|
||||||
|
secret_access_key_file: Path = Path("/run/secrets/s3_secret_access_key")
|
||||||
|
connect_timeout_seconds: float = Field(default=2, gt=0, le=5)
|
||||||
|
read_timeout_seconds: float = Field(default=10, gt=0, le=15)
|
||||||
|
upload_timeout_seconds: float = Field(default=30, gt=0, le=60)
|
||||||
|
download_timeout_seconds: float = Field(default=60, gt=0, le=120)
|
||||||
|
image_timeout_seconds: float = Field(default=10, gt=0, le=15)
|
||||||
|
max_upload_bytes: int = Field(default=10_485_760, ge=1, le=10_485_760)
|
||||||
|
max_image_pixels: int = Field(default=40_000_000, ge=1, le=40_000_000)
|
||||||
|
chunk_bytes: int = Field(default=65_536, ge=1024, le=262_144)
|
||||||
|
max_concurrent_uploads: int = Field(default=2, ge=1, le=2)
|
||||||
|
cleanup_grace_seconds: int = Field(default=86_400, ge=300)
|
||||||
|
cleanup_interval_seconds: int = Field(default=300, ge=1)
|
||||||
|
|
||||||
|
model_config = SettingsConfigDict(
|
||||||
|
env_prefix="S3_", env_file=".env", extra="ignore", frozen=True
|
||||||
|
)
|
||||||
|
|
||||||
|
@model_validator(mode="after")
|
||||||
|
def validate_endpoint(self):
|
||||||
|
url = urlsplit(self.endpoint)
|
||||||
|
if (
|
||||||
|
url.scheme not in {"http", "https"}
|
||||||
|
or not url.hostname
|
||||||
|
or url.username
|
||||||
|
or url.password
|
||||||
|
or url.path not in {"", "/"}
|
||||||
|
or url.query
|
||||||
|
or url.fragment
|
||||||
|
):
|
||||||
|
raise ValueError(
|
||||||
|
"S3_ENDPOINT должен быть http(s) адресом без credentials/пути"
|
||||||
|
)
|
||||||
|
return self
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
"""Хеширование паролей (Argon2id) и выпуск непрозрачных токенов сессий."""
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import hashlib
|
||||||
|
import os
|
||||||
|
import secrets
|
||||||
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
|
|
||||||
|
from argon2 import PasswordHasher
|
||||||
|
from argon2.exceptions import InvalidHashError, VerificationError
|
||||||
|
|
||||||
|
TOKEN_BYTES = 32
|
||||||
|
|
||||||
|
_hasher = PasswordHasher()
|
||||||
|
_executor = ThreadPoolExecutor(
|
||||||
|
max_workers=max(1, min(2, (os.cpu_count() or 1) // _hasher.parallelism)),
|
||||||
|
thread_name_prefix="argon2",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _hash_password_sync(password: str) -> str:
|
||||||
|
"""Блокирующий расчёт Argon2id-хеша со случайной солью в формате PHC."""
|
||||||
|
return _hasher.hash(password)
|
||||||
|
|
||||||
|
|
||||||
|
def _verify_password_sync(password: str, password_hash: str) -> bool:
|
||||||
|
"""Блокирующая проверка пароля по PHC-строке; False при несовпадении или повреждённом хеше."""
|
||||||
|
try:
|
||||||
|
return _hasher.verify(password_hash, password)
|
||||||
|
except (VerificationError, InvalidHashError):
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
_DUMMY_HASH = _hash_password_sync(secrets.token_urlsafe(TOKEN_BYTES))
|
||||||
|
|
||||||
|
|
||||||
|
async def hash_password(password: str) -> str:
|
||||||
|
"""Хеширует пароль в пуле потоков, не блокируя event loop."""
|
||||||
|
loop = asyncio.get_running_loop()
|
||||||
|
return await loop.run_in_executor(_executor, _hash_password_sync, password)
|
||||||
|
|
||||||
|
|
||||||
|
async def verify_password(password: str, password_hash: str) -> bool:
|
||||||
|
"""Проверяет пароль в пуле потоков, не блокируя event loop."""
|
||||||
|
loop = asyncio.get_running_loop()
|
||||||
|
return await loop.run_in_executor(
|
||||||
|
_executor, _verify_password_sync, password, password_hash
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def dummy_verify_password(password: str) -> None:
|
||||||
|
"""Проверка против фиктивного хеша: время ответа не выдаёт отсутствие пользователя."""
|
||||||
|
await verify_password(password, _DUMMY_HASH)
|
||||||
|
|
||||||
|
|
||||||
|
def generate_token() -> str:
|
||||||
|
"""Непрозрачный токен сессии: 32 случайных байта в base64url."""
|
||||||
|
return secrets.token_urlsafe(TOKEN_BYTES)
|
||||||
|
|
||||||
|
|
||||||
|
def hash_token(token: str) -> str:
|
||||||
|
"""SHA-256 токена в hex: детерминированный ключ для хранилища сессий."""
|
||||||
|
return hashlib.sha256(token.encode()).hexdigest()
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
"""Один pipeline изображений в Linux-процессе с лимитами памяти и CPU."""
|
||||||
|
|
||||||
|
from pathlib import Path
|
||||||
|
import resource
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from app.core.image_processing import ImageSettings
|
||||||
|
from app.domain.image_pipeline import create_variants
|
||||||
|
from app.errors.exceptions import IMAGE_WORKER_ERRORS, pipeline_worker_error_code
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> int:
|
||||||
|
settings = ImageSettings()
|
||||||
|
memory = settings.memory_mib * 1024 * 1024
|
||||||
|
resource.setrlimit(resource.RLIMIT_AS, (memory, memory))
|
||||||
|
resource.setrlimit(
|
||||||
|
resource.RLIMIT_CPU, (settings.cpu_seconds, settings.cpu_seconds)
|
||||||
|
)
|
||||||
|
source, directory, metadata, content_type, maximum = sys.argv[1:]
|
||||||
|
code = 0
|
||||||
|
try:
|
||||||
|
result = create_variants(
|
||||||
|
Path(source), Path(directory), content_type, int(maximum), settings
|
||||||
|
)
|
||||||
|
Path(metadata).write_text(result.model_dump_json())
|
||||||
|
except IMAGE_WORKER_ERRORS as error:
|
||||||
|
code = pipeline_worker_error_code(error)
|
||||||
|
return code
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
raise SystemExit(main())
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
"""Бизнес-логика: проверки доступа, транзакции и ошибки API."""
|
||||||
@@ -0,0 +1,265 @@
|
|||||||
|
"""Приём, durable публикация, попытки и фиксация результата обработки."""
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
from collections.abc import Callable
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from pathlib import Path
|
||||||
|
from tempfile import TemporaryDirectory
|
||||||
|
from uuid import UUID, uuid4
|
||||||
|
|
||||||
|
from minio.error import MinioException
|
||||||
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
from urllib3.exceptions import HTTPError as StorageHTTPError
|
||||||
|
|
||||||
|
from app.core.jobs import JobSettings
|
||||||
|
from app.domain.job_retry import classify_failure, retry_delay
|
||||||
|
from app.domain.job_files import fetch_original, render_variants
|
||||||
|
from app.errors.exceptions import ProcessingError
|
||||||
|
from app.schemas.attachment import VARIANT_FILENAMES
|
||||||
|
from app.schemas.job import JobMessage
|
||||||
|
from app.storage import jobs, outbox
|
||||||
|
from app.storage.broker import JobBroker
|
||||||
|
from app.storage.jobs import JobWork
|
||||||
|
from app.storage.s3 import MinioStorage
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class Claim:
|
||||||
|
work: JobWork | None = None
|
||||||
|
acknowledge: bool = False
|
||||||
|
|
||||||
|
|
||||||
|
async def enqueue_attachment_job(
|
||||||
|
session: AsyncSession, attachment_id: UUID, staging_key: str
|
||||||
|
) -> UUID:
|
||||||
|
"""Включить намерение обработки в транзакцию queued metadata; без commit."""
|
||||||
|
return await jobs.create_job(session, attachment_id, staging_key)
|
||||||
|
|
||||||
|
|
||||||
|
async def publish_next_job(
|
||||||
|
session: AsyncSession,
|
||||||
|
broker: JobBroker,
|
||||||
|
*,
|
||||||
|
attachment_id: UUID | None = None,
|
||||||
|
before_publish: Callable[[UUID], None] | None = None,
|
||||||
|
) -> bool:
|
||||||
|
"""Отметка publish следует за durable publisher confirm; crash допускает тот же job-дубль."""
|
||||||
|
job = await outbox.next_unpublished_job(session, attachment_id=attachment_id)
|
||||||
|
published = job is not None
|
||||||
|
if job is not None:
|
||||||
|
if before_publish is not None:
|
||||||
|
before_publish(job.attachment_id)
|
||||||
|
await broker.publish_job(
|
||||||
|
JobMessage(
|
||||||
|
job_id=job.id,
|
||||||
|
attachment_id=job.attachment_id,
|
||||||
|
pipeline_version=job.pipeline_version,
|
||||||
|
attempt=job.attempt + 1,
|
||||||
|
created_at=job.created_at,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
outbox.mark_published(job)
|
||||||
|
await session.commit()
|
||||||
|
return published
|
||||||
|
|
||||||
|
|
||||||
|
async def claim_job(
|
||||||
|
session: AsyncSession, message: JobMessage, settings: JobSettings
|
||||||
|
) -> Claim:
|
||||||
|
"""Дубликат не захватывает живую аренду и не откатывает terminal-состояние."""
|
||||||
|
row = await jobs.lock_job(session, message.job_id)
|
||||||
|
result = Claim()
|
||||||
|
if row is None:
|
||||||
|
raise ProcessingError("unknown_job")
|
||||||
|
job, attachment = row
|
||||||
|
if (
|
||||||
|
message.attachment_id != attachment.id
|
||||||
|
or message.pipeline_version != job.pipeline_version
|
||||||
|
):
|
||||||
|
raise ProcessingError("invalid_job")
|
||||||
|
now = await jobs.database_time(session)
|
||||||
|
if job.attempt_limit is None:
|
||||||
|
job.attempt_limit = settings.job_max_attempts
|
||||||
|
if attachment.status in {"ready", "failed"}:
|
||||||
|
result = Claim(acknowledge=True)
|
||||||
|
elif message.attempt <= job.attempt and attachment.status == "queued":
|
||||||
|
# Предыдущая попытка уже устойчиво заменена новой в PostgreSQL.
|
||||||
|
result = Claim(acknowledge=True)
|
||||||
|
elif message.attempt < job.attempt:
|
||||||
|
result = Claim(acknowledge=True)
|
||||||
|
elif message.attempt > job.attempt + 1:
|
||||||
|
raise ProcessingError("invalid_attempt")
|
||||||
|
elif attachment.status == "queued" and job.next_attempt_at <= now:
|
||||||
|
if (
|
||||||
|
job.attempt >= (job.attempt_limit or settings.job_max_attempts)
|
||||||
|
or job.staging_key is None
|
||||||
|
):
|
||||||
|
jobs.record_failure(
|
||||||
|
job, attachment, now, "retry_exhausted", terminal=True, retry_seconds=0
|
||||||
|
)
|
||||||
|
result = Claim(acknowledge=True)
|
||||||
|
else:
|
||||||
|
result = Claim(
|
||||||
|
work=jobs.start_job(
|
||||||
|
job, attachment, uuid4(), now, settings.job_lease_seconds
|
||||||
|
)
|
||||||
|
)
|
||||||
|
await session.commit()
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
async def save_failure(
|
||||||
|
session: AsyncSession,
|
||||||
|
work: JobWork,
|
||||||
|
settings: JobSettings,
|
||||||
|
code: str,
|
||||||
|
*,
|
||||||
|
permanent: bool,
|
||||||
|
) -> None:
|
||||||
|
# Неоднозначный COMMIT проверяется повторным чтением: ready сохраняется.
|
||||||
|
await session.rollback()
|
||||||
|
row = await jobs.lock_job(session, work.job_id)
|
||||||
|
if row is not None:
|
||||||
|
job, attachment = row
|
||||||
|
if attachment.status == "processing" and job.lease_token == work.lease_token:
|
||||||
|
exhausted = job.attempt >= (job.attempt_limit or settings.job_max_attempts)
|
||||||
|
jobs.record_failure(
|
||||||
|
job,
|
||||||
|
attachment,
|
||||||
|
await jobs.database_time(session),
|
||||||
|
"retry_exhausted" if exhausted and not permanent else code,
|
||||||
|
terminal=permanent or exhausted,
|
||||||
|
retry_seconds=retry_delay(
|
||||||
|
settings, job.attempt - job.cycle_start_attempt
|
||||||
|
),
|
||||||
|
)
|
||||||
|
job.last_error_code = code
|
||||||
|
job.last_error_permanent = permanent
|
||||||
|
await session.commit()
|
||||||
|
|
||||||
|
|
||||||
|
async def build_result(
|
||||||
|
session: AsyncSession,
|
||||||
|
storage: MinioStorage,
|
||||||
|
work: JobWork,
|
||||||
|
after_object: Callable[[UUID, str], None] | None = None,
|
||||||
|
) -> bool:
|
||||||
|
"""Фиксированные ключи и блокировка job защищают PUT от устаревшей попытки."""
|
||||||
|
with TemporaryDirectory(prefix="messenger-job-") as directory:
|
||||||
|
source = Path(directory) / "original"
|
||||||
|
await fetch_original(storage, work, source)
|
||||||
|
result = await render_variants(storage, work, source, Path(directory))
|
||||||
|
row = await jobs.lock_job(session, work.job_id)
|
||||||
|
if row is None:
|
||||||
|
await session.rollback()
|
||||||
|
return True
|
||||||
|
job, attachment = row
|
||||||
|
now = await jobs.database_time(session)
|
||||||
|
if attachment.status != "processing" or job.lease_token != work.lease_token:
|
||||||
|
await session.rollback()
|
||||||
|
return True
|
||||||
|
if job.lease_until is None or job.lease_until <= now:
|
||||||
|
await session.rollback()
|
||||||
|
return False
|
||||||
|
# Блокировка остаётся до завершения PUT/COMMIT; reaper использует SKIP LOCKED.
|
||||||
|
# При отмене S3 adapter дожидается SDK-потока до освобождения блокировки.
|
||||||
|
await storage.put_file(
|
||||||
|
work.original_key,
|
||||||
|
source,
|
||||||
|
size_bytes=work.size_bytes,
|
||||||
|
content_type=work.content_type,
|
||||||
|
sha256=work.sha256,
|
||||||
|
)
|
||||||
|
if after_object is not None:
|
||||||
|
after_object(work.attachment_id, "original")
|
||||||
|
for variant in result.variants:
|
||||||
|
await storage.put_file(
|
||||||
|
work.variant_key(variant.name),
|
||||||
|
Path(directory) / VARIANT_FILENAMES[variant.name],
|
||||||
|
size_bytes=variant.size_bytes,
|
||||||
|
content_type=variant.content_type,
|
||||||
|
sha256=variant.sha256,
|
||||||
|
)
|
||||||
|
if after_object is not None:
|
||||||
|
after_object(work.attachment_id, variant.name)
|
||||||
|
jobs.complete_job(
|
||||||
|
job,
|
||||||
|
attachment,
|
||||||
|
work,
|
||||||
|
[variant.model_dump() for variant in result.variants],
|
||||||
|
await jobs.database_time(session),
|
||||||
|
)
|
||||||
|
await session.commit()
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
async def process_attachment_job(
|
||||||
|
session: AsyncSession,
|
||||||
|
storage: MinioStorage,
|
||||||
|
message: JobMessage,
|
||||||
|
settings: JobSettings,
|
||||||
|
*,
|
||||||
|
after_object: Callable[[UUID, str], None] | None = None,
|
||||||
|
before_processing: Callable[[UUID], None] | None = None,
|
||||||
|
) -> bool:
|
||||||
|
claim = await claim_job(session, message, settings)
|
||||||
|
acknowledge = claim.acknowledge
|
||||||
|
if claim.work is not None:
|
||||||
|
work = claim.work
|
||||||
|
try:
|
||||||
|
async with asyncio.timeout(settings.job_timeout_seconds):
|
||||||
|
if before_processing is not None:
|
||||||
|
before_processing(work.attachment_id)
|
||||||
|
acknowledge = await build_result(session, storage, work, after_object)
|
||||||
|
except (
|
||||||
|
ProcessingError,
|
||||||
|
MinioException,
|
||||||
|
StorageHTTPError,
|
||||||
|
OSError,
|
||||||
|
TimeoutError,
|
||||||
|
) as error:
|
||||||
|
code, permanent = classify_failure(error)
|
||||||
|
await save_failure(session, work, settings, code, permanent=permanent)
|
||||||
|
acknowledge = True
|
||||||
|
# Ошибка БД/неизвестная ошибка или отмена не подтверждают доставку.
|
||||||
|
return acknowledge
|
||||||
|
|
||||||
|
|
||||||
|
async def recover_expired_jobs(session: AsyncSession, settings: JobSettings) -> int:
|
||||||
|
rows = await jobs.expired_jobs(session)
|
||||||
|
now = await jobs.database_time(session)
|
||||||
|
for job, attachment in rows:
|
||||||
|
if job.attempt_limit is None:
|
||||||
|
job.attempt_limit = settings.job_max_attempts
|
||||||
|
exhausted = job.attempt >= (job.attempt_limit or settings.job_max_attempts)
|
||||||
|
jobs.record_failure(
|
||||||
|
job,
|
||||||
|
attachment,
|
||||||
|
now,
|
||||||
|
"retry_exhausted" if exhausted else "worker_timeout",
|
||||||
|
terminal=exhausted,
|
||||||
|
retry_seconds=retry_delay(settings, job.attempt - job.cycle_start_attempt),
|
||||||
|
)
|
||||||
|
job.last_error_code = "worker_timeout"
|
||||||
|
job.last_error_permanent = False
|
||||||
|
await session.commit()
|
||||||
|
return len(rows)
|
||||||
|
|
||||||
|
|
||||||
|
async def cleanup_staging(
|
||||||
|
session: AsyncSession, storage: MinioStorage, settings: JobSettings, *, apply: bool
|
||||||
|
) -> int:
|
||||||
|
rows = await jobs.terminal_staging(
|
||||||
|
session,
|
||||||
|
settings.staging_retention_seconds,
|
||||||
|
settings.failed_staging_retention_seconds,
|
||||||
|
)
|
||||||
|
if apply:
|
||||||
|
for job, attachment in rows:
|
||||||
|
if job.staging_key != attachment.object_key:
|
||||||
|
await storage.delete_object(job.staging_key)
|
||||||
|
jobs.clear_staging(job)
|
||||||
|
await session.commit()
|
||||||
|
else:
|
||||||
|
await session.rollback()
|
||||||
|
return len(rows)
|
||||||
@@ -0,0 +1,340 @@
|
|||||||
|
"""Авторизация, приём оригинала и согласованность PostgreSQL/MinIO."""
|
||||||
|
|
||||||
|
from collections.abc import AsyncIterator, Iterator
|
||||||
|
from contextlib import contextmanager, suppress
|
||||||
|
from dataclasses import dataclass
|
||||||
|
import hashlib
|
||||||
|
from pathlib import Path
|
||||||
|
from tempfile import TemporaryDirectory
|
||||||
|
from uuid import UUID, uuid4
|
||||||
|
|
||||||
|
from anyio import CancelScope
|
||||||
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
|
from app.core.request_capacity import RequestCapacity
|
||||||
|
from app.domain.chats import ensure_chat_member
|
||||||
|
from app.domain.attachment_jobs import enqueue_attachment_job
|
||||||
|
from app.domain.images import validate_filename, validate_image
|
||||||
|
from app.errors.exceptions import (
|
||||||
|
conflict,
|
||||||
|
invalid_request,
|
||||||
|
not_found,
|
||||||
|
payload_too_large,
|
||||||
|
rate_limited,
|
||||||
|
unavailable,
|
||||||
|
unsupported_media_type,
|
||||||
|
)
|
||||||
|
from app.errors.handlers import (
|
||||||
|
RECOVERABLE_STORAGE_ERRORS,
|
||||||
|
log_recoverable_error,
|
||||||
|
operation_timeout,
|
||||||
|
)
|
||||||
|
from app.schemas.attachment import Attachment, VARIANT_FILENAMES
|
||||||
|
from app.schemas.user import User
|
||||||
|
from app.storage import attachments as repository
|
||||||
|
from app.storage.s3 import MinioStorage
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class CleanupResult:
|
||||||
|
examined: int
|
||||||
|
candidates: int
|
||||||
|
deleted: int
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class OriginalFile:
|
||||||
|
path: Path
|
||||||
|
size_bytes: int
|
||||||
|
sha256: str
|
||||||
|
|
||||||
|
|
||||||
|
def require_storage(storage: MinioStorage | None) -> MinioStorage:
|
||||||
|
if storage is None:
|
||||||
|
raise unavailable("Объектное хранилище не настроено")
|
||||||
|
return storage
|
||||||
|
|
||||||
|
|
||||||
|
def validate_declared_size(value: str | None, maximum: int) -> None:
|
||||||
|
if value is not None:
|
||||||
|
if not value.isascii() or not value.isdecimal() or len(value) > 20:
|
||||||
|
raise invalid_request("Некорректный Content-Length")
|
||||||
|
if int(value) > maximum:
|
||||||
|
raise payload_too_large()
|
||||||
|
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def upload_slot(capacity: RequestCapacity) -> Iterator[None]:
|
||||||
|
if not capacity.try_acquire():
|
||||||
|
raise rate_limited()
|
||||||
|
try:
|
||||||
|
yield
|
||||||
|
finally:
|
||||||
|
capacity.release()
|
||||||
|
|
||||||
|
|
||||||
|
async def receive_attachment(
|
||||||
|
session: AsyncSession,
|
||||||
|
storage: MinioStorage | None,
|
||||||
|
user: User,
|
||||||
|
chat_id: UUID,
|
||||||
|
*,
|
||||||
|
capacity: RequestCapacity,
|
||||||
|
filename: str,
|
||||||
|
content_type: str,
|
||||||
|
declared_length: str | None,
|
||||||
|
chunks: AsyncIterator[bytes],
|
||||||
|
) -> Attachment:
|
||||||
|
storage = require_storage(storage)
|
||||||
|
validate_declared_size(declared_length, storage.settings.max_upload_bytes)
|
||||||
|
timeout_error = invalid_request("Истекло время загрузки")
|
||||||
|
with upload_slot(capacity):
|
||||||
|
async with operation_timeout(
|
||||||
|
storage.settings.upload_timeout_seconds, timeout_error
|
||||||
|
):
|
||||||
|
result = await upload_attachment(
|
||||||
|
session,
|
||||||
|
storage,
|
||||||
|
user,
|
||||||
|
chat_id,
|
||||||
|
filename=filename,
|
||||||
|
content_type=content_type,
|
||||||
|
chunks=chunks,
|
||||||
|
)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
async def read_original(
|
||||||
|
chunks: AsyncIterator[bytes],
|
||||||
|
path: Path,
|
||||||
|
maximum: int,
|
||||||
|
chunk_bytes: int,
|
||||||
|
) -> OriginalFile:
|
||||||
|
checksum = hashlib.sha256()
|
||||||
|
size = 0
|
||||||
|
with path.open("wb") as target:
|
||||||
|
async for chunk in chunks:
|
||||||
|
size += len(chunk)
|
||||||
|
if size > maximum:
|
||||||
|
raise payload_too_large()
|
||||||
|
checksum.update(chunk)
|
||||||
|
view = memoryview(chunk)
|
||||||
|
for offset in range(0, len(view), chunk_bytes):
|
||||||
|
target.write(view[offset : offset + chunk_bytes])
|
||||||
|
if size == 0:
|
||||||
|
raise unsupported_media_type("Пустой файл")
|
||||||
|
return OriginalFile(path, size, checksum.hexdigest())
|
||||||
|
|
||||||
|
|
||||||
|
async def compensate_upload(
|
||||||
|
session: AsyncSession, storage: MinioStorage, id: UUID
|
||||||
|
) -> None:
|
||||||
|
"""Неоднозначный COMMIT сначала проверяется в БД: готовый объект не удаляется."""
|
||||||
|
await session.rollback()
|
||||||
|
intent = await repository.lock_upload(session, id)
|
||||||
|
if intent is not None:
|
||||||
|
if await repository.get_by_key(session, intent.object_key) is None:
|
||||||
|
await storage.delete_object(intent.object_key)
|
||||||
|
await repository.finish_upload(session, id)
|
||||||
|
await session.commit()
|
||||||
|
|
||||||
|
|
||||||
|
async def recover_upload(
|
||||||
|
session: AsyncSession, storage: MinioStorage, id: UUID
|
||||||
|
) -> None:
|
||||||
|
"""Локальное восстановление после ошибки; журнал остаётся при отказе компенсации."""
|
||||||
|
try:
|
||||||
|
await compensate_upload(session, storage, id)
|
||||||
|
except RECOVERABLE_STORAGE_ERRORS as error:
|
||||||
|
log_recoverable_error("Upload compensation", error)
|
||||||
|
# Ошибка повторного rollback не должна скрыть исходную ошибку или отмену.
|
||||||
|
with suppress(*RECOVERABLE_STORAGE_ERRORS):
|
||||||
|
await session.rollback()
|
||||||
|
|
||||||
|
|
||||||
|
async def stage_attachment(
|
||||||
|
session: AsyncSession,
|
||||||
|
storage: MinioStorage,
|
||||||
|
user: User,
|
||||||
|
chat_id: UUID,
|
||||||
|
*,
|
||||||
|
original: OriginalFile,
|
||||||
|
filename: str,
|
||||||
|
content_type: str,
|
||||||
|
) -> Attachment:
|
||||||
|
"""Сохранить исходник в staging и атомарно принять metadata/job."""
|
||||||
|
id = uuid4()
|
||||||
|
key = f"staging/{uuid4().hex}"
|
||||||
|
# Журнал устойчив до PUT, включая аварию процесса и неоднозначный COMMIT.
|
||||||
|
await repository.reserve_upload(session, id, key)
|
||||||
|
await session.commit()
|
||||||
|
try:
|
||||||
|
if await repository.lock_upload(session, id) is None:
|
||||||
|
raise unavailable("Загрузка отменена до записи оригинала")
|
||||||
|
await storage.put_file(
|
||||||
|
key,
|
||||||
|
original.path,
|
||||||
|
size_bytes=original.size_bytes,
|
||||||
|
content_type=content_type,
|
||||||
|
sha256=original.sha256,
|
||||||
|
)
|
||||||
|
record = await repository.create_attachment(
|
||||||
|
session,
|
||||||
|
id=id,
|
||||||
|
chat_id=chat_id,
|
||||||
|
owner_id=user.id,
|
||||||
|
object_key=f"originals/{id.hex}/v1",
|
||||||
|
status="queued",
|
||||||
|
filename=filename,
|
||||||
|
content_type=content_type,
|
||||||
|
size_bytes=original.size_bytes,
|
||||||
|
sha256=original.sha256,
|
||||||
|
)
|
||||||
|
await enqueue_attachment_job(session, id, key)
|
||||||
|
await repository.finish_upload(session, id)
|
||||||
|
# Снимок queued строится до COMMIT, даже если worker очень быстрый.
|
||||||
|
result = Attachment.model_validate(record)
|
||||||
|
await session.commit()
|
||||||
|
except BaseException:
|
||||||
|
# Отмена тоже требует дождаться SDK и компенсировать незавершённый PUT.
|
||||||
|
with CancelScope(shield=True):
|
||||||
|
await recover_upload(session, storage, id)
|
||||||
|
raise
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
async def upload_attachment(
|
||||||
|
session: AsyncSession,
|
||||||
|
storage: MinioStorage,
|
||||||
|
user: User,
|
||||||
|
chat_id: UUID,
|
||||||
|
*,
|
||||||
|
filename: str,
|
||||||
|
content_type: str,
|
||||||
|
chunks: AsyncIterator[bytes],
|
||||||
|
) -> Attachment:
|
||||||
|
await ensure_chat_member(session, user, chat_id)
|
||||||
|
validate_filename(filename)
|
||||||
|
if content_type not in {"image/png", "image/jpeg"}:
|
||||||
|
raise unsupported_media_type("Принимаются только image/png и image/jpeg")
|
||||||
|
# Не держим соединение PostgreSQL во время чтения медленного клиента.
|
||||||
|
await session.rollback()
|
||||||
|
settings = storage.settings
|
||||||
|
with TemporaryDirectory(prefix="messenger-upload-") as directory:
|
||||||
|
original = await read_original(
|
||||||
|
chunks,
|
||||||
|
Path(directory) / "original",
|
||||||
|
settings.max_upload_bytes,
|
||||||
|
settings.chunk_bytes,
|
||||||
|
)
|
||||||
|
await validate_image(original.path, content_type, settings)
|
||||||
|
result = await stage_attachment(
|
||||||
|
session,
|
||||||
|
storage,
|
||||||
|
user,
|
||||||
|
chat_id,
|
||||||
|
original=original,
|
||||||
|
filename=filename,
|
||||||
|
content_type=content_type,
|
||||||
|
)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
async def get_attachment(
|
||||||
|
session: AsyncSession, user: User, attachment_id: UUID
|
||||||
|
) -> Attachment:
|
||||||
|
record = await repository.get_attachment(session, attachment_id)
|
||||||
|
if record is None:
|
||||||
|
raise not_found()
|
||||||
|
await ensure_chat_member(session, user, record.chat_id)
|
||||||
|
return Attachment.model_validate(record)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class DownloadInfo:
|
||||||
|
filename: str
|
||||||
|
content_type: str
|
||||||
|
size_bytes: int
|
||||||
|
|
||||||
|
|
||||||
|
async def download_attachment(
|
||||||
|
session: AsyncSession,
|
||||||
|
storage: MinioStorage | None,
|
||||||
|
user: User,
|
||||||
|
attachment_id: UUID,
|
||||||
|
*,
|
||||||
|
variant: str = "original",
|
||||||
|
) -> tuple[DownloadInfo, AsyncIterator[bytes]]:
|
||||||
|
result = await get_attachment(session, user, attachment_id)
|
||||||
|
if result.status != "ready":
|
||||||
|
raise conflict("Вложение пока недоступно для скачивания")
|
||||||
|
storage = require_storage(storage)
|
||||||
|
record = await repository.get_attachment(session, attachment_id)
|
||||||
|
if record is None:
|
||||||
|
raise not_found()
|
||||||
|
if variant == "original":
|
||||||
|
key = record.object_key
|
||||||
|
info = DownloadInfo(result.filename, result.content_type, result.size_bytes)
|
||||||
|
else:
|
||||||
|
selected = next(
|
||||||
|
(item for item in result.variants if item.name == variant), None
|
||||||
|
)
|
||||||
|
keys = {
|
||||||
|
"thumbnail": record.thumbnail_key,
|
||||||
|
"crop": record.crop_key,
|
||||||
|
"optimized": record.optimized_key,
|
||||||
|
}
|
||||||
|
key = keys.get(variant)
|
||||||
|
if selected is None or key is None:
|
||||||
|
raise not_found()
|
||||||
|
info = DownloadInfo(
|
||||||
|
VARIANT_FILENAMES[variant], selected.content_type, selected.size_bytes
|
||||||
|
)
|
||||||
|
await session.rollback()
|
||||||
|
stored = await storage.stat_object(key)
|
||||||
|
if stored.size_bytes != info.size_bytes or stored.content_type != info.content_type:
|
||||||
|
raise unavailable("Файл временно недоступен")
|
||||||
|
stream = await storage.open_object(key)
|
||||||
|
return info, stream
|
||||||
|
|
||||||
|
|
||||||
|
async def validate_message_attachments(
|
||||||
|
session: AsyncSession,
|
||||||
|
user: User,
|
||||||
|
chat_id: UUID,
|
||||||
|
attachment_ids: list[UUID],
|
||||||
|
) -> None:
|
||||||
|
await ensure_chat_member(session, user, chat_id)
|
||||||
|
records = await repository.get_for_chat(session, chat_id, attachment_ids)
|
||||||
|
if len(records) != len(attachment_ids):
|
||||||
|
raise not_found()
|
||||||
|
if any(record.status == "failed" for record in records):
|
||||||
|
raise conflict("Обработка вложения завершилась ошибкой")
|
||||||
|
|
||||||
|
|
||||||
|
async def reconcile_attachments(
|
||||||
|
session: AsyncSession,
|
||||||
|
storage: MinioStorage,
|
||||||
|
*,
|
||||||
|
batch_size: int = 20,
|
||||||
|
dry_run: bool = True,
|
||||||
|
) -> CleanupResult:
|
||||||
|
if not 1 <= batch_size <= 100:
|
||||||
|
raise invalid_request("batch_size должен быть от 1 до 100")
|
||||||
|
intents = await repository.stale_uploads(
|
||||||
|
session, storage.settings.cleanup_grace_seconds, batch_size
|
||||||
|
)
|
||||||
|
candidates = deleted = 0
|
||||||
|
for intent in intents:
|
||||||
|
if await repository.get_by_key(session, intent.object_key) is None:
|
||||||
|
candidates += 1
|
||||||
|
if not dry_run:
|
||||||
|
await storage.delete_object(intent.object_key)
|
||||||
|
deleted += 1
|
||||||
|
if not dry_run:
|
||||||
|
await repository.finish_upload(session, intent.id)
|
||||||
|
if dry_run:
|
||||||
|
await session.rollback()
|
||||||
|
else:
|
||||||
|
await session.commit()
|
||||||
|
return CleanupResult(len(intents), candidates, deleted)
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
import re
|
||||||
|
from datetime import UTC, datetime, timedelta
|
||||||
|
|
||||||
|
from redis.asyncio import Redis
|
||||||
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
|
from app.core import security
|
||||||
|
from app.domain.users import to_user_schema
|
||||||
|
from app.errors.exceptions import unauthorized_basic, unauthorized_bearer
|
||||||
|
from app.schemas.session import AuthSession
|
||||||
|
from app.schemas.user import (
|
||||||
|
PASSWORD_MAX_LENGTH,
|
||||||
|
PASSWORD_MIN_LENGTH,
|
||||||
|
USERNAME_PATTERN,
|
||||||
|
User,
|
||||||
|
)
|
||||||
|
from app.storage import sessions as sessions_storage
|
||||||
|
from app.storage import users as users_storage
|
||||||
|
|
||||||
|
USERNAME_RE = re.compile(USERNAME_PATTERN)
|
||||||
|
|
||||||
|
|
||||||
|
async def create_session(
|
||||||
|
session: AsyncSession,
|
||||||
|
redis: Redis,
|
||||||
|
username: str,
|
||||||
|
password: str,
|
||||||
|
ttl_seconds: int,
|
||||||
|
) -> AuthSession:
|
||||||
|
"""Проверяет учётные данные и выдаёт новый Bearer-токен."""
|
||||||
|
if (
|
||||||
|
USERNAME_RE.fullmatch(username) is None
|
||||||
|
or not PASSWORD_MIN_LENGTH <= len(password) <= PASSWORD_MAX_LENGTH
|
||||||
|
or not password.strip()
|
||||||
|
):
|
||||||
|
raise unauthorized_basic()
|
||||||
|
|
||||||
|
user = await users_storage.get_user_by_username(session, username)
|
||||||
|
await session.commit()
|
||||||
|
|
||||||
|
if user is None:
|
||||||
|
await security.dummy_verify_password(password)
|
||||||
|
raise unauthorized_basic()
|
||||||
|
|
||||||
|
if not await security.verify_password(password, user.password_hash):
|
||||||
|
raise unauthorized_basic()
|
||||||
|
|
||||||
|
token = security.generate_token()
|
||||||
|
expires_at = datetime.now(UTC) + timedelta(seconds=ttl_seconds)
|
||||||
|
await sessions_storage.create_session(
|
||||||
|
redis,
|
||||||
|
token_hash=security.hash_token(token),
|
||||||
|
user_id=user.id,
|
||||||
|
expires_at=expires_at,
|
||||||
|
)
|
||||||
|
auth_session = AuthSession(
|
||||||
|
token=token, expires_at=expires_at, user=to_user_schema(user)
|
||||||
|
)
|
||||||
|
return auth_session
|
||||||
|
|
||||||
|
|
||||||
|
async def authenticate_token(session: AsyncSession, redis: Redis, token: str) -> User:
|
||||||
|
"""Возвращает владельца действующей сессии или бросает 401."""
|
||||||
|
user_id = await sessions_storage.get_session_user_id(
|
||||||
|
redis, security.hash_token(token)
|
||||||
|
)
|
||||||
|
if user_id is None:
|
||||||
|
raise unauthorized_bearer()
|
||||||
|
|
||||||
|
user = await users_storage.get_user_by_id(session, user_id)
|
||||||
|
if user is None:
|
||||||
|
raise unauthorized_bearer()
|
||||||
|
|
||||||
|
current_user = to_user_schema(user)
|
||||||
|
return current_user
|
||||||
|
|
||||||
|
|
||||||
|
async def revoke_session(redis: Redis, token: str) -> None:
|
||||||
|
"""Отзывает текущую сессию; повторный отзыв даёт 401."""
|
||||||
|
is_deleted = await sessions_storage.delete_session(
|
||||||
|
redis, security.hash_token(token)
|
||||||
|
)
|
||||||
|
if not is_deleted:
|
||||||
|
raise unauthorized_bearer()
|
||||||
@@ -0,0 +1,104 @@
|
|||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from sqlalchemy.exc import IntegrityError
|
||||||
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
|
from app.domain.pagination import (
|
||||||
|
KIND_CHATS,
|
||||||
|
build_next_cursor,
|
||||||
|
decode_cursor,
|
||||||
|
split_page,
|
||||||
|
)
|
||||||
|
from app.errors.exceptions import invalid_request, not_found
|
||||||
|
from app.models.chat import Chat as ChatModel
|
||||||
|
from app.schemas.chat import Chat, ChatPage, CreateChat
|
||||||
|
from app.schemas.user import User
|
||||||
|
from app.storage import chats as chats_storage
|
||||||
|
from app.storage import users as users_storage
|
||||||
|
from app.storage.types import Position
|
||||||
|
|
||||||
|
UNKNOWN_MEMBER_MESSAGE = "member_ids содержит неизвестного пользователя"
|
||||||
|
|
||||||
|
|
||||||
|
def to_chat_schema(chat: ChatModel) -> Chat:
|
||||||
|
"""Преобразует ORM-модель чата с загруженными участниками в схему."""
|
||||||
|
chat_schema = Chat(
|
||||||
|
id=chat.id,
|
||||||
|
title=chat.title,
|
||||||
|
member_ids=[member.user_id for member in chat.members],
|
||||||
|
created_at=chat.created_at,
|
||||||
|
)
|
||||||
|
return chat_schema
|
||||||
|
|
||||||
|
|
||||||
|
def validate_new_chat(creator: User, new_chat: CreateChat) -> None:
|
||||||
|
"""Проверяет состав участников без обращения к базе."""
|
||||||
|
member_ids = new_chat.member_ids
|
||||||
|
|
||||||
|
if creator.id not in member_ids:
|
||||||
|
raise invalid_request("Создатель чата должен входить в member_ids")
|
||||||
|
|
||||||
|
if len(set(member_ids)) != len(member_ids):
|
||||||
|
raise invalid_request("member_ids содержит дубликаты")
|
||||||
|
|
||||||
|
|
||||||
|
async def create_chat(
|
||||||
|
session: AsyncSession, creator: User, new_chat: CreateChat
|
||||||
|
) -> Chat:
|
||||||
|
"""Создаёт чат с участниками атомарно; неверный состав даёт 400."""
|
||||||
|
validate_new_chat(creator, new_chat)
|
||||||
|
|
||||||
|
existing_ids = await users_storage.get_existing_user_ids(
|
||||||
|
session, new_chat.member_ids
|
||||||
|
)
|
||||||
|
if existing_ids != set(new_chat.member_ids):
|
||||||
|
raise invalid_request(UNKNOWN_MEMBER_MESSAGE)
|
||||||
|
|
||||||
|
try:
|
||||||
|
chat = await chats_storage.create_chat(
|
||||||
|
session, title=new_chat.title, member_ids=new_chat.member_ids
|
||||||
|
)
|
||||||
|
await session.commit()
|
||||||
|
except IntegrityError:
|
||||||
|
await session.rollback()
|
||||||
|
raise invalid_request(UNKNOWN_MEMBER_MESSAGE) from None
|
||||||
|
|
||||||
|
created_chat = to_chat_schema(chat)
|
||||||
|
return created_chat
|
||||||
|
|
||||||
|
|
||||||
|
async def get_chat(session: AsyncSession, user: User, chat_id: UUID) -> Chat:
|
||||||
|
"""Возвращает чат участнику; чужой и несуществующий чат дают 404."""
|
||||||
|
chat = await chats_storage.get_chat_for_member(session, chat_id, user.id)
|
||||||
|
if chat is None:
|
||||||
|
raise not_found()
|
||||||
|
|
||||||
|
found_chat = to_chat_schema(chat)
|
||||||
|
return found_chat
|
||||||
|
|
||||||
|
|
||||||
|
async def ensure_chat_member(session: AsyncSession, user: User, chat_id: UUID) -> None:
|
||||||
|
"""Бросает 404, если пользователь не состоит в чате или чата нет."""
|
||||||
|
if not await chats_storage.is_chat_member(session, chat_id, user.id):
|
||||||
|
raise not_found()
|
||||||
|
|
||||||
|
|
||||||
|
async def list_chats(
|
||||||
|
session: AsyncSession,
|
||||||
|
user: User,
|
||||||
|
cursor: str | None,
|
||||||
|
limit: int,
|
||||||
|
) -> ChatPage:
|
||||||
|
"""Возвращает страницу чатов пользователя по (created_at, id)."""
|
||||||
|
after: Position | None = None
|
||||||
|
if cursor is not None:
|
||||||
|
after = decode_cursor(cursor, KIND_CHATS, user.id)
|
||||||
|
|
||||||
|
rows = await chats_storage.list_chats_for_user(session, user.id, after, limit + 1)
|
||||||
|
page_rows, has_next = split_page(rows, limit)
|
||||||
|
items = [to_chat_schema(chat) for chat in page_rows]
|
||||||
|
page = ChatPage(
|
||||||
|
items=items,
|
||||||
|
next_cursor=build_next_cursor(KIND_CHATS, user.id, items, has_next),
|
||||||
|
)
|
||||||
|
return page
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
"""Условия готовности реплики и ограниченная проверка зависимостей."""
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
from redis.asyncio import Redis
|
||||||
|
from sqlalchemy.ext.asyncio import AsyncEngine
|
||||||
|
|
||||||
|
from app.core.lifecycle import Lifecycle
|
||||||
|
from app.errors.exceptions import unavailable
|
||||||
|
from app.errors.handlers import operation_timeout
|
||||||
|
from app.schemas.base import Health
|
||||||
|
from app.storage.health import check_database, check_redis
|
||||||
|
from app.storage.s3 import MinioStorage
|
||||||
|
|
||||||
|
|
||||||
|
def liveness() -> Health:
|
||||||
|
return Health(status="ok")
|
||||||
|
|
||||||
|
|
||||||
|
async def readiness(
|
||||||
|
engine: AsyncEngine,
|
||||||
|
redis: Redis,
|
||||||
|
storage: MinioStorage | None,
|
||||||
|
lifecycle: Lifecycle,
|
||||||
|
timeout_seconds: float,
|
||||||
|
) -> Health:
|
||||||
|
if lifecycle.draining:
|
||||||
|
raise unavailable()
|
||||||
|
checks = [check_database(engine), check_redis(redis)]
|
||||||
|
if storage is not None:
|
||||||
|
checks.append(storage.check_available())
|
||||||
|
async with operation_timeout(timeout_seconds, unavailable()):
|
||||||
|
dependencies = await asyncio.gather(*checks, return_exceptions=True)
|
||||||
|
if not all(result is True for result in dependencies):
|
||||||
|
raise unavailable()
|
||||||
|
return Health(status="ok")
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
"""Атомарная отправка сообщения с защитой от повторов."""
|
||||||
|
|
||||||
|
import hashlib
|
||||||
|
import json
|
||||||
|
import re
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
|
from app.domain.chats import ensure_chat_member
|
||||||
|
from app.domain.attachments import validate_message_attachments
|
||||||
|
from app.errors.exceptions import conflict, invalid_request, unavailable
|
||||||
|
from app.schemas.message import CreateMessage, Message
|
||||||
|
from app.schemas.user import User
|
||||||
|
from app.storage import idempotency as key_storage
|
||||||
|
from app.storage import messages as message_storage
|
||||||
|
from app.storage import attachments as attachment_storage
|
||||||
|
|
||||||
|
IDEMPOTENCY_KEY_PATTERN = re.compile(r"[A-Za-z0-9._:-]{1,128}")
|
||||||
|
|
||||||
|
|
||||||
|
def validate_idempotency_key(key: str | None) -> str:
|
||||||
|
"""Проверяет обязательный ASCII-ключ запроса."""
|
||||||
|
if key is None or IDEMPOTENCY_KEY_PATTERN.fullmatch(key) is None:
|
||||||
|
raise invalid_request("Требуется корректный Idempotency-Key")
|
||||||
|
return key
|
||||||
|
|
||||||
|
|
||||||
|
def request_fingerprint(new_message: CreateMessage) -> str:
|
||||||
|
"""Хеширует нормализованное содержимое запроса."""
|
||||||
|
body = new_message.model_dump(mode="json")
|
||||||
|
# Сохраняет прежние отпечатки текстовых запросов и равенство absent / [].
|
||||||
|
if not body["attachment_ids"]:
|
||||||
|
body.pop("attachment_ids")
|
||||||
|
canonical = json.dumps(body, sort_keys=True, ensure_ascii=False)
|
||||||
|
return hashlib.sha256(canonical.encode()).hexdigest()
|
||||||
|
|
||||||
|
|
||||||
|
async def send_message_idempotent(
|
||||||
|
session: AsyncSession,
|
||||||
|
sender: User,
|
||||||
|
chat_id: UUID,
|
||||||
|
new_message: CreateMessage,
|
||||||
|
idempotency_key: str,
|
||||||
|
max_messages: int,
|
||||||
|
ttl_seconds: int,
|
||||||
|
) -> Message:
|
||||||
|
"""Создаёт сообщение или возвращает результат прежнего запроса."""
|
||||||
|
await ensure_chat_member(session, sender, chat_id)
|
||||||
|
scope = (sender.id, "POST", f"/api/v1/chats/{chat_id}/messages", idempotency_key)
|
||||||
|
fingerprint = request_fingerprint(new_message)
|
||||||
|
|
||||||
|
await key_storage.lock_key(session, scope)
|
||||||
|
previous = await key_storage.get_active_key(session, scope, ttl_seconds)
|
||||||
|
if previous is not None:
|
||||||
|
if previous.request_hash != fingerprint:
|
||||||
|
raise conflict("Idempotency-Key уже использован для другого сообщения")
|
||||||
|
message = await message_storage.get_message(session, previous.message_id)
|
||||||
|
else:
|
||||||
|
await validate_message_attachments(
|
||||||
|
session, sender, chat_id, new_message.attachment_ids
|
||||||
|
)
|
||||||
|
if not await message_storage.reserve_message_slot(session, max_messages):
|
||||||
|
raise unavailable("Достигнут лимит количества сообщений")
|
||||||
|
message = await message_storage.create_message(
|
||||||
|
session, chat_id=chat_id, sender_id=sender.id, text=new_message.text
|
||||||
|
)
|
||||||
|
await attachment_storage.bind_attachments(
|
||||||
|
session, message.id, new_message.attachment_ids
|
||||||
|
)
|
||||||
|
await key_storage.save_result(session, scope, fingerprint, message.id)
|
||||||
|
|
||||||
|
links = await attachment_storage.message_attachment_ids(session, [message.id])
|
||||||
|
result = Message.model_validate(message).model_copy(
|
||||||
|
update={"attachment_ids": links[message.id]}
|
||||||
|
)
|
||||||
|
await session.commit()
|
||||||
|
return result
|
||||||
@@ -0,0 +1,105 @@
|
|||||||
|
"""Thumbnail, crop и WebP; один ограниченный процесс, последовательная обработка."""
|
||||||
|
|
||||||
|
import hashlib
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from PIL import Image, ImageOps
|
||||||
|
|
||||||
|
from app.core.image_processing import ImageSettings
|
||||||
|
from app.domain.image_validation import decode_image
|
||||||
|
from app.schemas.attachment import PipelineResult, Variant
|
||||||
|
|
||||||
|
|
||||||
|
def fitted_size(size: tuple[int, int], maximum: int) -> tuple[int, int]:
|
||||||
|
"""Вписать без увеличения, округляя вниз, минимум один пиксель."""
|
||||||
|
longest = max(size)
|
||||||
|
result = size
|
||||||
|
if longest > maximum:
|
||||||
|
result = (
|
||||||
|
max(1, size[0] * maximum // longest),
|
||||||
|
max(1, size[1] * maximum // longest),
|
||||||
|
)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def describe_variant(
|
||||||
|
name: str, content_type: str, path: Path, size: tuple[int, int]
|
||||||
|
) -> Variant:
|
||||||
|
checksum = hashlib.sha256()
|
||||||
|
with path.open("rb") as source:
|
||||||
|
for chunk in iter(lambda: source.read(65536), b""):
|
||||||
|
checksum.update(chunk)
|
||||||
|
return Variant(
|
||||||
|
name=name,
|
||||||
|
content_type=content_type,
|
||||||
|
size_bytes=path.stat().st_size,
|
||||||
|
width=size[0],
|
||||||
|
height=size[1],
|
||||||
|
sha256=checksum.hexdigest(),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def save_jpeg(image: Image.Image, destination: Path, quality: int) -> None:
|
||||||
|
"""Белая подложка и новый image без EXIF/ICC/XMP/текстовых метаданных."""
|
||||||
|
with Image.new("RGB", image.size, "white") as clean:
|
||||||
|
with image.getchannel("A") as alpha:
|
||||||
|
clean.paste(image, mask=alpha)
|
||||||
|
clean.save(destination, format="JPEG", quality=quality, exif=b"")
|
||||||
|
|
||||||
|
|
||||||
|
def create_thumbnail(image: Image.Image, destination: Path, quality: int) -> Variant:
|
||||||
|
size = fitted_size(image.size, 256)
|
||||||
|
with image.resize(size, Image.Resampling.LANCZOS) as resized:
|
||||||
|
save_jpeg(resized, destination, quality)
|
||||||
|
return describe_variant("thumbnail", "image/jpeg", destination, size)
|
||||||
|
|
||||||
|
|
||||||
|
def create_crop(image: Image.Image, destination: Path, quality: int) -> Variant:
|
||||||
|
"""Центрированный JPEG 128×128; маленький исходник допускает увеличение."""
|
||||||
|
with ImageOps.fit(
|
||||||
|
image, (128, 128), method=Image.Resampling.LANCZOS, centering=(0.5, 0.5)
|
||||||
|
) as crop:
|
||||||
|
save_jpeg(crop, destination, quality)
|
||||||
|
return describe_variant("crop", "image/jpeg", destination, (128, 128))
|
||||||
|
|
||||||
|
|
||||||
|
def create_optimized(
|
||||||
|
image: Image.Image, destination: Path, settings: ImageSettings
|
||||||
|
) -> Variant:
|
||||||
|
"""WebP ≤1280×1280 без увеличения; alpha сохраняется, метаданные удаляются."""
|
||||||
|
size = fitted_size(image.size, 1280)
|
||||||
|
with image.resize(size, Image.Resampling.LANCZOS) as resized:
|
||||||
|
with Image.new("RGBA", size) as clean:
|
||||||
|
clean.paste(resized)
|
||||||
|
clean.save(
|
||||||
|
destination,
|
||||||
|
format="WEBP",
|
||||||
|
lossless=False,
|
||||||
|
quality=settings.webp_quality,
|
||||||
|
method=settings.webp_method,
|
||||||
|
exif=b"",
|
||||||
|
icc_profile=b"",
|
||||||
|
xmp=b"",
|
||||||
|
)
|
||||||
|
return describe_variant("optimized", "image/webp", destination, size)
|
||||||
|
|
||||||
|
|
||||||
|
def create_variants(
|
||||||
|
source: Path,
|
||||||
|
directory: Path,
|
||||||
|
content_type: str,
|
||||||
|
max_pixels: int,
|
||||||
|
settings: ImageSettings,
|
||||||
|
) -> PipelineResult:
|
||||||
|
decode_image(source, content_type, max_pixels)
|
||||||
|
with Image.open(source, formats=["PNG", "JPEG"]) as image:
|
||||||
|
ImageOps.exif_transpose(image, in_place=True)
|
||||||
|
with image.convert("RGBA") as pixels:
|
||||||
|
variants = [
|
||||||
|
create_thumbnail(
|
||||||
|
pixels, directory / "thumbnail.jpg", settings.jpeg_quality
|
||||||
|
),
|
||||||
|
create_crop(pixels, directory / "crop.jpg", settings.jpeg_quality),
|
||||||
|
create_optimized(pixels, directory / "optimized.webp", settings),
|
||||||
|
]
|
||||||
|
return PipelineResult(variants=variants)
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
"""Синхронная проверка изображения; вызывается только из ограниченного процесса."""
|
||||||
|
|
||||||
|
from pathlib import Path
|
||||||
|
import warnings
|
||||||
|
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
from app.errors.exceptions import payload_too_large, unsupported_media_type
|
||||||
|
|
||||||
|
|
||||||
|
def decode_image(path: Path, content_type: str, max_pixels: int) -> None:
|
||||||
|
Image.MAX_IMAGE_PIXELS = max_pixels
|
||||||
|
warnings.simplefilter("error", Image.DecompressionBombWarning)
|
||||||
|
with Image.open(path, formats=["PNG", "JPEG"]) as image:
|
||||||
|
if image.width * image.height > max_pixels:
|
||||||
|
raise payload_too_large("Превышен лимит пикселей изображения")
|
||||||
|
image_format = image.format
|
||||||
|
if image_format is None or Image.MIME.get(image_format) != content_type:
|
||||||
|
raise unsupported_media_type("Содержимое не соответствует Content-Type")
|
||||||
|
image.verify()
|
||||||
|
# Для JPEG одной verify() недостаточно: декодировать весь оригинал.
|
||||||
|
with Image.open(path, formats=["PNG", "JPEG"]) as image:
|
||||||
|
image.load()
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
"""Проверка имени и запуск ограниченного декодера."""
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
from pathlib import Path
|
||||||
|
import sys
|
||||||
|
import unicodedata
|
||||||
|
|
||||||
|
from anyio import CancelScope
|
||||||
|
|
||||||
|
from app.core.s3 import S3Settings
|
||||||
|
from app.errors.exceptions import (
|
||||||
|
image_validation_error,
|
||||||
|
invalid_request,
|
||||||
|
payload_too_large,
|
||||||
|
)
|
||||||
|
from app.errors.handlers import operation_timeout
|
||||||
|
|
||||||
|
|
||||||
|
def validate_filename(filename: str) -> None:
|
||||||
|
if not 1 <= len(filename) <= 255 or any(
|
||||||
|
char in "/\\" or unicodedata.category(char) in {"Cc", "Cf", "Cs"}
|
||||||
|
for char in filename
|
||||||
|
):
|
||||||
|
raise invalid_request("Недопустимое имя файла")
|
||||||
|
|
||||||
|
|
||||||
|
async def validate_image(path: Path, content_type: str, settings: S3Settings) -> None:
|
||||||
|
process = await asyncio.create_subprocess_exec(
|
||||||
|
sys.executable,
|
||||||
|
"-m",
|
||||||
|
"app.core.image_worker",
|
||||||
|
str(path),
|
||||||
|
content_type,
|
||||||
|
str(settings.max_image_pixels),
|
||||||
|
stdout=asyncio.subprocess.DEVNULL,
|
||||||
|
stderr=asyncio.subprocess.DEVNULL,
|
||||||
|
)
|
||||||
|
error = payload_too_large("Превышен бюджет проверки изображения")
|
||||||
|
try:
|
||||||
|
async with operation_timeout(settings.image_timeout_seconds, error):
|
||||||
|
code = await process.wait()
|
||||||
|
finally:
|
||||||
|
if process.returncode is None:
|
||||||
|
process.kill()
|
||||||
|
with CancelScope(shield=True):
|
||||||
|
await process.wait()
|
||||||
|
if code != 0:
|
||||||
|
raise image_validation_error(code)
|
||||||
@@ -0,0 +1,161 @@
|
|||||||
|
"""Replay только failed с проверенным исходником и ограниченным новым бюджетом."""
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import re
|
||||||
|
from pathlib import Path
|
||||||
|
from tempfile import TemporaryDirectory
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
from minio.error import S3Error
|
||||||
|
|
||||||
|
from app.domain.job_files import fetch_original
|
||||||
|
from app.errors.exceptions import JobOperationError, ProcessingError
|
||||||
|
from app.storage import job_admin, jobs
|
||||||
|
from app.storage.jobs import JobWork
|
||||||
|
|
||||||
|
REPLAY_REASONS = ("dependency_restored", "configuration_fixed", "manual_review")
|
||||||
|
|
||||||
|
|
||||||
|
def snapshot(job, attachment):
|
||||||
|
return dict(
|
||||||
|
job_id=str(job.id),
|
||||||
|
attachment_id=str(attachment.id),
|
||||||
|
status=attachment.status,
|
||||||
|
attempt=job.attempt,
|
||||||
|
attempt_limit=job.attempt_limit,
|
||||||
|
cycle_start_attempt=job.cycle_start_attempt,
|
||||||
|
error_code=attachment.error_code,
|
||||||
|
last_error_code=job.last_error_code,
|
||||||
|
last_error_permanent=job.last_error_permanent,
|
||||||
|
staging_retained=job.staging_key is not None,
|
||||||
|
next_attempt_at=job.next_attempt_at,
|
||||||
|
lease_until=job.lease_until,
|
||||||
|
finished_at=job.finished_at,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def list_failed(session, limit):
|
||||||
|
return [
|
||||||
|
snapshot(job, attachment)
|
||||||
|
for job, attachment in await job_admin.failed_jobs(session, limit)
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
async def show_job(session, job_id):
|
||||||
|
row = await jobs.lock_job(session, job_id)
|
||||||
|
if row is None:
|
||||||
|
raise JobOperationError("unknown_job")
|
||||||
|
result = snapshot(*row)
|
||||||
|
await session.rollback()
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
async def history(session, job_id):
|
||||||
|
return [
|
||||||
|
dict(
|
||||||
|
request_id=str(row.id),
|
||||||
|
actor=row.actor,
|
||||||
|
reason=row.reason,
|
||||||
|
previous_attempt=row.previous_attempt,
|
||||||
|
attempt_limit=row.attempt_limit,
|
||||||
|
previous_error_code=row.previous_error_code,
|
||||||
|
created_at=row.created_at,
|
||||||
|
)
|
||||||
|
for row in await job_admin.replay_history(session, job_id)
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def same_request(record, job_id, actor, reason):
|
||||||
|
if record.job_id != job_id or record.actor != actor or record.reason != reason:
|
||||||
|
raise JobOperationError("request_conflict")
|
||||||
|
return {
|
||||||
|
"request_id": str(record.id),
|
||||||
|
"job_id": str(job_id),
|
||||||
|
"already_applied": True,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def validate_replay_request(actor: str, reason: str) -> None:
|
||||||
|
"""Проверить данные оператора для записи аудита."""
|
||||||
|
if (
|
||||||
|
re.fullmatch(r"[A-Za-z0-9._-]{1,64}", actor) is None
|
||||||
|
or reason not in REPLAY_REASONS
|
||||||
|
):
|
||||||
|
raise JobOperationError("invalid_audit_fields")
|
||||||
|
|
||||||
|
|
||||||
|
def validate_replay_job(job, attachment) -> None:
|
||||||
|
"""Разрешить новый replay только для failed с сохранённым исходником."""
|
||||||
|
if attachment.status != "failed":
|
||||||
|
raise JobOperationError("job_not_failed")
|
||||||
|
if job.staging_key is None:
|
||||||
|
raise JobOperationError("source_expired")
|
||||||
|
|
||||||
|
|
||||||
|
async def verify_replay_source(storage, settings, job, attachment) -> None:
|
||||||
|
"""Проверить размер и SHA-256 исходника в S3 под блокировкой job."""
|
||||||
|
work = JobWork(
|
||||||
|
job.id,
|
||||||
|
attachment.id,
|
||||||
|
uuid4(),
|
||||||
|
job.pipeline_version,
|
||||||
|
job.staging_key,
|
||||||
|
attachment.content_type,
|
||||||
|
attachment.size_bytes,
|
||||||
|
attachment.sha256,
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
async with asyncio.timeout(settings.job_timeout_seconds):
|
||||||
|
with TemporaryDirectory(prefix="replay-source-") as directory:
|
||||||
|
await fetch_original(storage, work, Path(directory) / "original")
|
||||||
|
except ProcessingError as error:
|
||||||
|
raise JobOperationError(error.code) from None
|
||||||
|
except S3Error as error:
|
||||||
|
if error.code in {"NoSuchKey", "NoSuchObject"}:
|
||||||
|
raise JobOperationError("source_missing") from None
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
async def replay_failed_job(
|
||||||
|
session, storage, settings, job_id, *, request_id, actor, reason
|
||||||
|
):
|
||||||
|
"""Организовать replay; блокировка, rollback и commit остаются здесь."""
|
||||||
|
validate_replay_request(actor, reason)
|
||||||
|
row = await jobs.lock_job(session, job_id)
|
||||||
|
if row is None:
|
||||||
|
raise JobOperationError("unknown_job")
|
||||||
|
record = await job_admin.replay_record(session, request_id)
|
||||||
|
if record is not None:
|
||||||
|
result = same_request(record, job_id, actor, reason)
|
||||||
|
await session.rollback()
|
||||||
|
return result
|
||||||
|
job, attachment = row
|
||||||
|
validate_replay_job(job, attachment)
|
||||||
|
# Блокировка сохраняется во время проверки S3, аудита и изменения job.
|
||||||
|
await verify_replay_source(storage, settings, job, attachment)
|
||||||
|
limit = job.attempt + settings.job_max_attempts
|
||||||
|
inserted = await job_admin.insert_replay(
|
||||||
|
session,
|
||||||
|
id=request_id,
|
||||||
|
job_id=job_id,
|
||||||
|
actor=actor,
|
||||||
|
reason=reason,
|
||||||
|
previous_attempt=job.attempt,
|
||||||
|
attempt_limit=limit,
|
||||||
|
previous_error_code=job.last_error_code,
|
||||||
|
)
|
||||||
|
if not inserted:
|
||||||
|
result = same_request(
|
||||||
|
await job_admin.replay_record(session, request_id), job_id, actor, reason
|
||||||
|
)
|
||||||
|
await session.rollback()
|
||||||
|
return result
|
||||||
|
job_admin.reset_failed(job, attachment, await jobs.database_time(session), limit)
|
||||||
|
await session.commit()
|
||||||
|
return {
|
||||||
|
"request_id": str(request_id),
|
||||||
|
"job_id": str(job_id),
|
||||||
|
"already_applied": False,
|
||||||
|
"status": "queued",
|
||||||
|
"attempt_limit": limit,
|
||||||
|
}
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
"""Получение исходника и запуск изолированного процесса обработки вариантов."""
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import errno
|
||||||
|
import hashlib
|
||||||
|
from pathlib import Path
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from anyio import CancelScope
|
||||||
|
|
||||||
|
from app.errors.exceptions import ProcessingError
|
||||||
|
from app.schemas.attachment import PipelineResult
|
||||||
|
from app.storage.jobs import JobWork
|
||||||
|
from app.storage.s3 import MinioStorage
|
||||||
|
|
||||||
|
|
||||||
|
async def fetch_original(
|
||||||
|
storage: MinioStorage, work: JobWork, destination: Path
|
||||||
|
) -> None:
|
||||||
|
if work.size_bytes > storage.settings.max_upload_bytes:
|
||||||
|
raise ProcessingError("image_limits")
|
||||||
|
checksum = hashlib.sha256()
|
||||||
|
size = 0
|
||||||
|
stream = await storage.open_object(work.staging_key)
|
||||||
|
try:
|
||||||
|
with destination.open("wb") as target:
|
||||||
|
async for chunk in stream:
|
||||||
|
size += len(chunk)
|
||||||
|
if size > work.size_bytes:
|
||||||
|
raise ProcessingError("source_mismatch")
|
||||||
|
target.write(chunk)
|
||||||
|
checksum.update(chunk)
|
||||||
|
finally:
|
||||||
|
await stream.aclose()
|
||||||
|
if size != work.size_bytes or checksum.hexdigest() != work.sha256:
|
||||||
|
raise ProcessingError("source_mismatch")
|
||||||
|
|
||||||
|
|
||||||
|
async def render_variants(
|
||||||
|
storage: MinioStorage, work: JobWork, source: Path, directory: Path
|
||||||
|
) -> PipelineResult:
|
||||||
|
metadata = directory / "variants.json"
|
||||||
|
process = await asyncio.create_subprocess_exec(
|
||||||
|
sys.executable,
|
||||||
|
"-m",
|
||||||
|
"app.core.thumbnail_worker",
|
||||||
|
str(source),
|
||||||
|
str(directory),
|
||||||
|
str(metadata),
|
||||||
|
work.content_type,
|
||||||
|
str(storage.settings.max_image_pixels),
|
||||||
|
stdout=asyncio.subprocess.DEVNULL,
|
||||||
|
stderr=asyncio.subprocess.DEVNULL,
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
code = await process.wait()
|
||||||
|
finally:
|
||||||
|
if process.returncode is None:
|
||||||
|
process.kill()
|
||||||
|
with CancelScope(shield=True):
|
||||||
|
await process.wait()
|
||||||
|
if code == 2:
|
||||||
|
raise ProcessingError("invalid_image")
|
||||||
|
if code == 3 or code < 0:
|
||||||
|
raise ProcessingError("image_limits")
|
||||||
|
if code == 4:
|
||||||
|
raise OSError(errno.EIO, "Pipeline filesystem unavailable")
|
||||||
|
if code == 5:
|
||||||
|
raise ProcessingError("processing_configuration")
|
||||||
|
if code != 0:
|
||||||
|
raise RuntimeError("Image pipeline subprocess failed")
|
||||||
|
return PipelineResult.model_validate_json(metadata.read_text())
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
"""Ограниченный exponential backoff: jitter сохраняется вместе с новой попыткой."""
|
||||||
|
|
||||||
|
import errno
|
||||||
|
import random
|
||||||
|
|
||||||
|
from minio.error import S3Error
|
||||||
|
|
||||||
|
from app.core.jobs import JobSettings
|
||||||
|
from app.errors.exceptions import ProcessingError
|
||||||
|
|
||||||
|
|
||||||
|
def retry_delay(settings: JobSettings, cycle_attempt: int) -> float:
|
||||||
|
ceiling = min(
|
||||||
|
settings.job_retry_max_seconds,
|
||||||
|
settings.job_retry_seconds * 2 ** min(30, max(0, cycle_attempt - 1)),
|
||||||
|
)
|
||||||
|
return random.uniform(ceiling / 2, ceiling)
|
||||||
|
|
||||||
|
|
||||||
|
def classify_failure(error: Exception) -> tuple[str, bool]:
|
||||||
|
if isinstance(error, ProcessingError):
|
||||||
|
return error.code, True
|
||||||
|
if isinstance(error, S3Error):
|
||||||
|
if error.code in {"NoSuchKey", "NoSuchObject"}:
|
||||||
|
return "source_missing", True
|
||||||
|
if error.code in {
|
||||||
|
"AccessDenied",
|
||||||
|
"InvalidAccessKeyId",
|
||||||
|
"SignatureDoesNotMatch",
|
||||||
|
"NoSuchBucket",
|
||||||
|
}:
|
||||||
|
return "storage_configuration", True
|
||||||
|
if error.code in {
|
||||||
|
"InternalError",
|
||||||
|
"SlowDown",
|
||||||
|
"ServiceUnavailable",
|
||||||
|
"RequestTimeout",
|
||||||
|
"Throttling",
|
||||||
|
}:
|
||||||
|
return "storage_unavailable", False
|
||||||
|
return "storage_rejected", True
|
||||||
|
if isinstance(error, TimeoutError):
|
||||||
|
return "processing_timeout", False
|
||||||
|
if isinstance(error, OSError) and error.errno in {
|
||||||
|
errno.EACCES,
|
||||||
|
errno.EPERM,
|
||||||
|
errno.EROFS,
|
||||||
|
errno.ENOENT,
|
||||||
|
}:
|
||||||
|
return "processing_configuration", True
|
||||||
|
return "processing_unavailable", False
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
|
from app.domain.chats import ensure_chat_member
|
||||||
|
from app.domain.pagination import (
|
||||||
|
KIND_MESSAGES,
|
||||||
|
build_next_cursor,
|
||||||
|
decode_cursor,
|
||||||
|
split_page,
|
||||||
|
)
|
||||||
|
from app.schemas.message import Message, MessagePage
|
||||||
|
from app.schemas.user import User
|
||||||
|
from app.storage import messages as messages_storage
|
||||||
|
from app.storage import attachments as attachments_storage
|
||||||
|
from app.storage.types import Position
|
||||||
|
|
||||||
|
|
||||||
|
async def list_messages(
|
||||||
|
session: AsyncSession,
|
||||||
|
user: User,
|
||||||
|
chat_id: UUID,
|
||||||
|
cursor: str | None,
|
||||||
|
limit: int,
|
||||||
|
) -> MessagePage:
|
||||||
|
"""Возвращает страницу истории чата по (created_at, id)."""
|
||||||
|
after: Position | None = None
|
||||||
|
if cursor is not None:
|
||||||
|
after = decode_cursor(cursor, KIND_MESSAGES, chat_id)
|
||||||
|
|
||||||
|
await ensure_chat_member(session, user, chat_id)
|
||||||
|
|
||||||
|
rows = await messages_storage.list_messages(session, chat_id, after, limit + 1)
|
||||||
|
page_rows, has_next = split_page(rows, limit)
|
||||||
|
links = await attachments_storage.message_attachment_ids(
|
||||||
|
session, [message.id for message in page_rows]
|
||||||
|
)
|
||||||
|
items = [
|
||||||
|
Message.model_validate(message).model_copy(
|
||||||
|
update={"attachment_ids": links[message.id]},
|
||||||
|
)
|
||||||
|
for message in page_rows
|
||||||
|
]
|
||||||
|
page = MessagePage(
|
||||||
|
items=items,
|
||||||
|
next_cursor=build_next_cursor(KIND_MESSAGES, chat_id, items, has_next),
|
||||||
|
)
|
||||||
|
return page
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
import base64
|
||||||
|
import json
|
||||||
|
from collections.abc import Sequence
|
||||||
|
from datetime import datetime
|
||||||
|
from typing import Protocol, TypeVar
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from app.errors.exceptions import invalid_request
|
||||||
|
from app.storage.types import Position
|
||||||
|
|
||||||
|
KIND_CHATS = "chats"
|
||||||
|
KIND_MESSAGES = "messages"
|
||||||
|
|
||||||
|
T = TypeVar("T")
|
||||||
|
|
||||||
|
|
||||||
|
class Positioned(Protocol):
|
||||||
|
"""Объект с позицией в порядке (created_at, id)."""
|
||||||
|
|
||||||
|
@property
|
||||||
|
def id(self) -> UUID: ...
|
||||||
|
|
||||||
|
@property
|
||||||
|
def created_at(self) -> datetime: ...
|
||||||
|
|
||||||
|
|
||||||
|
def encode_cursor(
|
||||||
|
kind: str,
|
||||||
|
resource_id: UUID,
|
||||||
|
created_at: datetime,
|
||||||
|
item_id: UUID,
|
||||||
|
) -> str:
|
||||||
|
"""Кодирует позицию и ресурс, к которому она привязана, в непрозрачный cursor."""
|
||||||
|
payload = {
|
||||||
|
"kind": kind,
|
||||||
|
"resource_id": str(resource_id),
|
||||||
|
"created_at": created_at.isoformat(),
|
||||||
|
"item_id": str(item_id),
|
||||||
|
}
|
||||||
|
raw = json.dumps(payload).encode()
|
||||||
|
return base64.urlsafe_b64encode(raw).decode()
|
||||||
|
|
||||||
|
|
||||||
|
def decode_cursor(cursor: str, kind: str, resource_id: UUID) -> Position:
|
||||||
|
"""Возвращает позицию из cursor; битый или чужой cursor даёт 400."""
|
||||||
|
try:
|
||||||
|
payload = json.loads(base64.urlsafe_b64decode(cursor))
|
||||||
|
cursor_kind = payload["kind"]
|
||||||
|
cursor_resource_id = UUID(payload["resource_id"])
|
||||||
|
created_at = datetime.fromisoformat(payload["created_at"])
|
||||||
|
item_id = UUID(payload["item_id"])
|
||||||
|
except (ValueError, KeyError, TypeError, AttributeError):
|
||||||
|
raise invalid_request("Некорректный cursor") from None
|
||||||
|
|
||||||
|
if (
|
||||||
|
cursor_kind != kind
|
||||||
|
or cursor_resource_id != resource_id
|
||||||
|
or created_at.tzinfo is None
|
||||||
|
):
|
||||||
|
raise invalid_request("Некорректный cursor")
|
||||||
|
|
||||||
|
return created_at, item_id
|
||||||
|
|
||||||
|
|
||||||
|
def split_page(rows: list[T], limit: int) -> tuple[list[T], bool]:
|
||||||
|
"""Отделяет страницу от лишней строки limit + 1, по которой видно продолжение."""
|
||||||
|
has_next = len(rows) > limit
|
||||||
|
items = rows[:limit]
|
||||||
|
return items, has_next
|
||||||
|
|
||||||
|
|
||||||
|
def build_next_cursor(
|
||||||
|
kind: str,
|
||||||
|
resource_id: UUID,
|
||||||
|
items: Sequence[Positioned],
|
||||||
|
has_next: bool,
|
||||||
|
) -> str | None:
|
||||||
|
"""Возвращает cursor на последний элемент страницы или None, если она последняя."""
|
||||||
|
if not has_next:
|
||||||
|
return None
|
||||||
|
|
||||||
|
last_item = items[-1]
|
||||||
|
return encode_cursor(kind, resource_id, last_item.created_at, last_item.id)
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
from sqlalchemy.exc import IntegrityError
|
||||||
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
|
from app.core import security
|
||||||
|
from app.errors.exceptions import conflict
|
||||||
|
from app.models.user import User as UserModel
|
||||||
|
from app.schemas.user import CreateUser, User
|
||||||
|
from app.storage import users as users_storage
|
||||||
|
|
||||||
|
USERNAME_TAKEN_MESSAGE = "Имя пользователя уже занято"
|
||||||
|
|
||||||
|
|
||||||
|
def to_user_schema(user: UserModel) -> User:
|
||||||
|
"""Преобразует ORM-модель в публичную схему без password_hash."""
|
||||||
|
user_schema = User.model_validate(user)
|
||||||
|
return user_schema
|
||||||
|
|
||||||
|
|
||||||
|
async def register_user(session: AsyncSession, new_user: CreateUser) -> User:
|
||||||
|
"""Регистрирует пользователя; занятый username даёт 409."""
|
||||||
|
existing_user = await users_storage.get_user_by_username(session, new_user.username)
|
||||||
|
await session.commit()
|
||||||
|
if existing_user is not None:
|
||||||
|
raise conflict(USERNAME_TAKEN_MESSAGE)
|
||||||
|
|
||||||
|
password_hash = await security.hash_password(new_user.password)
|
||||||
|
|
||||||
|
try:
|
||||||
|
user = await users_storage.create_user(
|
||||||
|
session,
|
||||||
|
username=new_user.username,
|
||||||
|
display_name=new_user.display_name,
|
||||||
|
password_hash=password_hash,
|
||||||
|
)
|
||||||
|
await session.commit()
|
||||||
|
except IntegrityError:
|
||||||
|
await session.rollback()
|
||||||
|
raise conflict(USERNAME_TAKEN_MESSAGE) from None
|
||||||
|
|
||||||
|
created_user = to_user_schema(user)
|
||||||
|
return created_user
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
"""HTTP-параметры и упаковка ответов; сценарии находятся в domain."""
|
||||||
|
|
||||||
|
from typing import Annotated
|
||||||
|
from urllib.parse import quote
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from fastapi import APIRouter, Query, Request
|
||||||
|
from fastapi.responses import StreamingResponse
|
||||||
|
from starlette.background import BackgroundTask
|
||||||
|
|
||||||
|
from app.domain import attachments
|
||||||
|
from app.endpoints.dependencies import CurrentUser, DbSession, S3Storage, UploadCapacity
|
||||||
|
from app.schemas.attachment import Attachment
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/api/v1", tags=["attachments"])
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/chats/{chat_id}/attachments", status_code=202)
|
||||||
|
async def upload(
|
||||||
|
request: Request,
|
||||||
|
chat_id: UUID,
|
||||||
|
user: CurrentUser,
|
||||||
|
session: DbSession,
|
||||||
|
storage: S3Storage,
|
||||||
|
capacity: UploadCapacity,
|
||||||
|
filename: Annotated[str, Query(min_length=1, max_length=255)],
|
||||||
|
) -> Attachment:
|
||||||
|
return await attachments.receive_attachment(
|
||||||
|
session,
|
||||||
|
storage,
|
||||||
|
user,
|
||||||
|
chat_id,
|
||||||
|
capacity=capacity,
|
||||||
|
filename=filename,
|
||||||
|
content_type=request.headers.get("content-type", "").lower(),
|
||||||
|
declared_length=request.headers.get("content-length"),
|
||||||
|
chunks=request.stream(),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/attachments/{attachment_id}")
|
||||||
|
async def metadata(
|
||||||
|
attachment_id: UUID, user: CurrentUser, session: DbSession
|
||||||
|
) -> Attachment:
|
||||||
|
return await attachments.get_attachment(session, user, attachment_id)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/attachments/{attachment_id}/content")
|
||||||
|
async def content(
|
||||||
|
attachment_id: UUID,
|
||||||
|
user: CurrentUser,
|
||||||
|
session: DbSession,
|
||||||
|
storage: S3Storage,
|
||||||
|
variant: Annotated[str, Query()] = "original",
|
||||||
|
) -> StreamingResponse:
|
||||||
|
record, stream = await attachments.download_attachment(
|
||||||
|
session,
|
||||||
|
storage,
|
||||||
|
user,
|
||||||
|
attachment_id,
|
||||||
|
variant=variant,
|
||||||
|
)
|
||||||
|
return StreamingResponse(
|
||||||
|
stream,
|
||||||
|
media_type=record.content_type,
|
||||||
|
headers={
|
||||||
|
"Content-Length": str(record.size_bytes),
|
||||||
|
"Content-Disposition": "attachment; filename*=UTF-8''"
|
||||||
|
+ quote(record.filename, safe=""),
|
||||||
|
"X-Content-Type-Options": "nosniff",
|
||||||
|
"Cache-Control": "private, no-store",
|
||||||
|
},
|
||||||
|
background=BackgroundTask(stream.aclose),
|
||||||
|
)
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
from fastapi import APIRouter, Response
|
||||||
|
|
||||||
|
from app.domain.auth import create_session, revoke_session
|
||||||
|
from app.endpoints.dependencies import (
|
||||||
|
AppSettings,
|
||||||
|
BasicCredentials,
|
||||||
|
BearerToken,
|
||||||
|
DbSession,
|
||||||
|
RedisClient,
|
||||||
|
)
|
||||||
|
from app.schemas.session import AuthSession
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/api/v1/auth", tags=["auth"])
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/sessions", status_code=201)
|
||||||
|
async def login(
|
||||||
|
credentials: BasicCredentials,
|
||||||
|
session: DbSession,
|
||||||
|
redis: RedisClient,
|
||||||
|
settings: AppSettings,
|
||||||
|
) -> AuthSession:
|
||||||
|
username, password = credentials
|
||||||
|
return await create_session(
|
||||||
|
session, redis, username, password, settings.session_ttl_seconds
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.delete("/sessions/current", status_code=204)
|
||||||
|
async def logout(token: BearerToken, redis: RedisClient) -> Response:
|
||||||
|
await revoke_session(redis, token)
|
||||||
|
return Response(status_code=204)
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
from typing import Annotated
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from fastapi import APIRouter, Query
|
||||||
|
|
||||||
|
from app.domain.chats import create_chat, get_chat, list_chats
|
||||||
|
from app.endpoints.dependencies import CurrentUser, DbSession
|
||||||
|
from app.schemas.chat import Chat, ChatPage, CreateChat
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/api/v1/chats", tags=["chats"])
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("", status_code=201)
|
||||||
|
async def create_new_chat(
|
||||||
|
new_chat: CreateChat, creator: CurrentUser, session: DbSession
|
||||||
|
) -> Chat:
|
||||||
|
return await create_chat(session, creator, new_chat)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("")
|
||||||
|
async def list_user_chats(
|
||||||
|
user: CurrentUser,
|
||||||
|
session: DbSession,
|
||||||
|
limit: Annotated[int, Query(ge=1, le=100)] = 50,
|
||||||
|
cursor: Annotated[str | None, Query(min_length=1)] = None,
|
||||||
|
) -> ChatPage:
|
||||||
|
return await list_chats(session, user, cursor, limit)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/{chat_id}")
|
||||||
|
async def get_chat_by_id(chat_id: UUID, user: CurrentUser, session: DbSession) -> Chat:
|
||||||
|
return await get_chat(session, user, chat_id)
|
||||||
@@ -0,0 +1,112 @@
|
|||||||
|
import base64
|
||||||
|
import binascii
|
||||||
|
from collections.abc import AsyncIterator
|
||||||
|
from typing import Annotated
|
||||||
|
|
||||||
|
from fastapi import Depends, Request, Security
|
||||||
|
from fastapi.openapi.models import HTTPBase, HTTPBearer
|
||||||
|
from fastapi.security.base import SecurityBase
|
||||||
|
from redis.asyncio import Redis
|
||||||
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
|
from app.core.config import Settings
|
||||||
|
from app.core.request_capacity import RequestCapacity
|
||||||
|
from app.domain.auth import authenticate_token
|
||||||
|
from app.errors.exceptions import unauthorized_basic, unauthorized_bearer
|
||||||
|
from app.schemas.user import User
|
||||||
|
from app.storage.s3 import MinioStorage
|
||||||
|
|
||||||
|
|
||||||
|
async def get_settings(request: Request) -> Settings:
|
||||||
|
settings: Settings = request.app.state.settings
|
||||||
|
return settings
|
||||||
|
|
||||||
|
|
||||||
|
async def get_db(request: Request) -> AsyncIterator[AsyncSession]:
|
||||||
|
"""Одна AsyncSession на HTTP-запрос; незакоммиченная транзакция откатывается."""
|
||||||
|
session_factory = request.app.state.session_factory
|
||||||
|
async with session_factory() as session:
|
||||||
|
yield session
|
||||||
|
|
||||||
|
|
||||||
|
async def get_redis(request: Request) -> Redis:
|
||||||
|
redis: Redis = request.app.state.redis
|
||||||
|
return redis
|
||||||
|
|
||||||
|
|
||||||
|
def get_s3(request: Request) -> MinioStorage | None:
|
||||||
|
return request.app.state.s3
|
||||||
|
|
||||||
|
|
||||||
|
def get_upload_capacity(request: Request) -> RequestCapacity:
|
||||||
|
return request.app.state.upload_capacity
|
||||||
|
|
||||||
|
|
||||||
|
class AuthorizationHeader(SecurityBase):
|
||||||
|
"""Отдаёт сырой заголовок Authorization и описывает схему для OpenAPI."""
|
||||||
|
|
||||||
|
def __init__(self, model: HTTPBase, scheme_name: str) -> None:
|
||||||
|
self.model = model
|
||||||
|
self.scheme_name = scheme_name
|
||||||
|
|
||||||
|
async def __call__(self, request: Request) -> str | None:
|
||||||
|
return request.headers.get("Authorization")
|
||||||
|
|
||||||
|
|
||||||
|
basic_auth = AuthorizationHeader(HTTPBase(scheme="basic"), "BasicAuth")
|
||||||
|
bearer_auth = AuthorizationHeader(HTTPBearer(), "BearerAuth")
|
||||||
|
|
||||||
|
|
||||||
|
def _split_authorization(authorization: str | None) -> tuple[str, str] | None:
|
||||||
|
if authorization is None:
|
||||||
|
return None
|
||||||
|
scheme, _, value = authorization.partition(" ")
|
||||||
|
if not value.strip():
|
||||||
|
return None
|
||||||
|
return scheme.lower(), value.strip()
|
||||||
|
|
||||||
|
|
||||||
|
async def get_bearer_token(
|
||||||
|
authorization: Annotated[str | None, Security(bearer_auth)],
|
||||||
|
) -> str:
|
||||||
|
parts = _split_authorization(authorization)
|
||||||
|
if parts is None or parts[0] != "bearer":
|
||||||
|
raise unauthorized_bearer()
|
||||||
|
return parts[1]
|
||||||
|
|
||||||
|
|
||||||
|
async def get_basic_credentials(
|
||||||
|
authorization: Annotated[str | None, Security(basic_auth)],
|
||||||
|
) -> tuple[str, str]:
|
||||||
|
"""(username, password) из `Authorization: Basic`; пароль может содержать ':'."""
|
||||||
|
parts = _split_authorization(authorization)
|
||||||
|
if parts is None or parts[0] != "basic":
|
||||||
|
raise unauthorized_basic()
|
||||||
|
|
||||||
|
try:
|
||||||
|
decoded = base64.b64decode(parts[1], validate=True).decode("utf-8")
|
||||||
|
except (binascii.Error, UnicodeDecodeError, ValueError):
|
||||||
|
raise unauthorized_basic() from None
|
||||||
|
|
||||||
|
username, separator, password = decoded.partition(":")
|
||||||
|
if not separator or not username:
|
||||||
|
raise unauthorized_basic()
|
||||||
|
return username, password
|
||||||
|
|
||||||
|
|
||||||
|
DbSession = Annotated[AsyncSession, Depends(get_db)]
|
||||||
|
RedisClient = Annotated[Redis, Depends(get_redis)]
|
||||||
|
AppSettings = Annotated[Settings, Depends(get_settings)]
|
||||||
|
S3Storage = Annotated[MinioStorage | None, Depends(get_s3)]
|
||||||
|
UploadCapacity = Annotated[RequestCapacity, Depends(get_upload_capacity)]
|
||||||
|
BearerToken = Annotated[str, Depends(get_bearer_token)]
|
||||||
|
BasicCredentials = Annotated[tuple[str, str], Depends(get_basic_credentials)]
|
||||||
|
|
||||||
|
|
||||||
|
async def get_current_user(
|
||||||
|
token: BearerToken, session: DbSession, redis: RedisClient
|
||||||
|
) -> User:
|
||||||
|
return await authenticate_token(session, redis, token)
|
||||||
|
|
||||||
|
|
||||||
|
CurrentUser = Annotated[User, Depends(get_current_user)]
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
from fastapi import APIRouter, Request
|
||||||
|
|
||||||
|
from app.domain import health
|
||||||
|
from app.schemas.base import Health
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/health", tags=["health"])
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/live")
|
||||||
|
async def liveness() -> Health:
|
||||||
|
return health.liveness()
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/ready")
|
||||||
|
async def readiness(request: Request) -> Health:
|
||||||
|
state = request.app.state
|
||||||
|
return await health.readiness(
|
||||||
|
state.engine,
|
||||||
|
state.redis,
|
||||||
|
state.s3,
|
||||||
|
state.lifecycle,
|
||||||
|
state.settings.readiness_timeout_seconds,
|
||||||
|
)
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
from typing import Annotated
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from fastapi import APIRouter, Header, Query
|
||||||
|
|
||||||
|
from app.domain.idempotency import send_message_idempotent, validate_idempotency_key
|
||||||
|
from app.domain.messages import list_messages
|
||||||
|
from app.endpoints.dependencies import AppSettings, CurrentUser, DbSession
|
||||||
|
from app.schemas.message import CreateMessage, Message, MessagePage
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/api/v1/chats", tags=["messages"])
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/{chat_id}/messages", status_code=201)
|
||||||
|
async def send_new_message(
|
||||||
|
chat_id: UUID,
|
||||||
|
new_message: CreateMessage,
|
||||||
|
sender: CurrentUser,
|
||||||
|
session: DbSession,
|
||||||
|
settings: AppSettings,
|
||||||
|
idempotency_key: Annotated[str | None, Header(alias="Idempotency-Key")] = None,
|
||||||
|
) -> Message:
|
||||||
|
return await send_message_idempotent(
|
||||||
|
session,
|
||||||
|
sender,
|
||||||
|
chat_id,
|
||||||
|
new_message,
|
||||||
|
validate_idempotency_key(idempotency_key),
|
||||||
|
settings.max_messages,
|
||||||
|
settings.idempotency_ttl_seconds,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/{chat_id}/messages")
|
||||||
|
async def get_history_messages(
|
||||||
|
chat_id: UUID,
|
||||||
|
user: CurrentUser,
|
||||||
|
session: DbSession,
|
||||||
|
limit: Annotated[int, Query(ge=1, le=100)] = 50,
|
||||||
|
cursor: Annotated[str | None, Query(min_length=1)] = None,
|
||||||
|
) -> MessagePage:
|
||||||
|
return await list_messages(session, user, chat_id, cursor, limit)
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
from fastapi import APIRouter
|
||||||
|
|
||||||
|
from app.domain.users import register_user
|
||||||
|
from app.endpoints.dependencies import CurrentUser, DbSession
|
||||||
|
from app.schemas.user import CreateUser, User
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/api/v1/users", tags=["users"])
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("", status_code=201)
|
||||||
|
async def create_user(new_user: CreateUser, session: DbSession) -> User:
|
||||||
|
return await register_user(session, new_user)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/me")
|
||||||
|
async def get_me(user: CurrentUser) -> User:
|
||||||
|
return user
|
||||||
@@ -0,0 +1,145 @@
|
|||||||
|
import errno
|
||||||
|
|
||||||
|
from collections.abc import Mapping
|
||||||
|
|
||||||
|
from PIL import Image, UnidentifiedImageError
|
||||||
|
|
||||||
|
BASIC_CHALLENGE = 'Basic realm="messenger", charset="UTF-8"'
|
||||||
|
BEARER_CHALLENGE = 'Bearer realm="messenger"'
|
||||||
|
|
||||||
|
|
||||||
|
class ApiError(Exception):
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
status_code: int,
|
||||||
|
code: str,
|
||||||
|
message: str,
|
||||||
|
headers: Mapping[str, str] | None = None,
|
||||||
|
) -> None:
|
||||||
|
super().__init__(message)
|
||||||
|
self.status_code = status_code
|
||||||
|
self.code = code
|
||||||
|
self.message = message
|
||||||
|
self.headers = headers
|
||||||
|
|
||||||
|
|
||||||
|
def invalid_request(message: str = "Неверный запрос") -> ApiError:
|
||||||
|
"""Создаёт ошибку некорректного или недопустимого запроса."""
|
||||||
|
return ApiError(400, "invalid_request", message)
|
||||||
|
|
||||||
|
|
||||||
|
def unauthorized(
|
||||||
|
message: str = "Нет действительной аутентификации",
|
||||||
|
www_authenticate: str | None = None,
|
||||||
|
) -> ApiError:
|
||||||
|
"""Создаёт ошибку отсутствующей или недействительной аутентификации."""
|
||||||
|
headers = {"WWW-Authenticate": www_authenticate} if www_authenticate else None
|
||||||
|
return ApiError(401, "unauthorized", message, headers)
|
||||||
|
|
||||||
|
|
||||||
|
def unauthorized_basic(message: str = "Неверные учётные данные") -> ApiError:
|
||||||
|
"""401 для POST /auth/sessions (одинаковый для неверного username и пароля)."""
|
||||||
|
return unauthorized(message, BASIC_CHALLENGE)
|
||||||
|
|
||||||
|
|
||||||
|
def unauthorized_bearer(message: str = "Нет действительной сессии") -> ApiError:
|
||||||
|
"""401 для бизнес-endpoint: нет, истёк, отозван или неизвестен токен."""
|
||||||
|
return unauthorized(message, BEARER_CHALLENGE)
|
||||||
|
|
||||||
|
|
||||||
|
def not_found(message: str = "Объект не найден") -> ApiError:
|
||||||
|
"""Создаёт ошибку, указывающую на отсутствие запрошенного ресурса."""
|
||||||
|
return ApiError(404, "not_found", message)
|
||||||
|
|
||||||
|
|
||||||
|
def conflict(message: str = "Конфликт") -> ApiError:
|
||||||
|
"""Создаёт ошибку конфликта с текущим состоянием ресурса."""
|
||||||
|
return ApiError(409, "conflict", message)
|
||||||
|
|
||||||
|
|
||||||
|
def payload_too_large(message: str = "Слишком большое тело запроса") -> ApiError:
|
||||||
|
"""Создаёт ошибку, указывающую на превышение допустимого размера запроса."""
|
||||||
|
return ApiError(413, "payload_too_large", message)
|
||||||
|
|
||||||
|
|
||||||
|
def unsupported_media_type(
|
||||||
|
message: str = "Требуется Content-Type: application/json",
|
||||||
|
) -> ApiError:
|
||||||
|
"""Создаёт ошибку неподдерживаемого формата или типа содержимого запроса."""
|
||||||
|
return ApiError(415, "unsupported_media_type", message)
|
||||||
|
|
||||||
|
|
||||||
|
def rate_limited(retry_after: int = 1) -> ApiError:
|
||||||
|
"""Создаёт ошибку превышения допустимой частоты запросов."""
|
||||||
|
return ApiError(
|
||||||
|
429,
|
||||||
|
"rate_limited",
|
||||||
|
"Слишком много запросов",
|
||||||
|
{"Retry-After": str(max(1, retry_after))},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def unavailable(message: str = "Сервис временно недоступен") -> ApiError:
|
||||||
|
"""Создаёт ошибку временной недоступности сервиса."""
|
||||||
|
return ApiError(503, "unavailable", message)
|
||||||
|
|
||||||
|
|
||||||
|
def internal_error() -> ApiError:
|
||||||
|
"""Создаёт ошибку внутренней ошибки сервера."""
|
||||||
|
return ApiError(500, "internal_error", "Внутренняя ошибка сервера")
|
||||||
|
|
||||||
|
|
||||||
|
IMAGE_WORKER_ERRORS = (
|
||||||
|
ApiError,
|
||||||
|
Image.DecompressionBombError,
|
||||||
|
Image.DecompressionBombWarning,
|
||||||
|
MemoryError,
|
||||||
|
UnidentifiedImageError,
|
||||||
|
OSError,
|
||||||
|
ValueError,
|
||||||
|
SyntaxError,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def image_worker_error_code(error: Exception) -> int:
|
||||||
|
code = 2
|
||||||
|
resource_error = isinstance(
|
||||||
|
error,
|
||||||
|
(Image.DecompressionBombError, Image.DecompressionBombWarning, MemoryError),
|
||||||
|
)
|
||||||
|
declared_limit = isinstance(error, ApiError) and error.status_code == 413
|
||||||
|
if resource_error or declared_limit:
|
||||||
|
code = 3
|
||||||
|
return code
|
||||||
|
|
||||||
|
|
||||||
|
def image_validation_error(code: int) -> ApiError:
|
||||||
|
error = unsupported_media_type("Повреждённое изображение или несовпадение MIME")
|
||||||
|
if code == 3 or code < 0:
|
||||||
|
error = payload_too_large("Превышен лимит пикселей или ресурсов изображения")
|
||||||
|
return error
|
||||||
|
|
||||||
|
|
||||||
|
class ProcessingError(Exception):
|
||||||
|
"""Постоянная ошибка pipeline с безопасным машинным кодом."""
|
||||||
|
|
||||||
|
def __init__(self, code: str) -> None:
|
||||||
|
super().__init__(code)
|
||||||
|
self.code = code
|
||||||
|
|
||||||
|
|
||||||
|
class JobOperationError(Exception):
|
||||||
|
"""Безопасный код отказа операторской команды."""
|
||||||
|
|
||||||
|
def __init__(self, code: str):
|
||||||
|
self.code = code
|
||||||
|
super().__init__(code)
|
||||||
|
|
||||||
|
|
||||||
|
def pipeline_worker_error_code(error: Exception) -> int:
|
||||||
|
"""Отделить ошибки файловой системы от ошибки декодирования изображения."""
|
||||||
|
if isinstance(error, OSError) and error.errno is not None:
|
||||||
|
if error.errno in {errno.EACCES, errno.EPERM, errno.EROFS, errno.ENOENT}:
|
||||||
|
return 5
|
||||||
|
return 4
|
||||||
|
return image_worker_error_code(error)
|
||||||
@@ -0,0 +1,238 @@
|
|||||||
|
import asyncio
|
||||||
|
import logging
|
||||||
|
from contextlib import asynccontextmanager
|
||||||
|
from collections.abc import Mapping
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
from fastapi import FastAPI, Request
|
||||||
|
from fastapi.exceptions import RequestValidationError
|
||||||
|
from fastapi.responses import JSONResponse
|
||||||
|
from minio.error import MinioException
|
||||||
|
from redis.exceptions import ConnectionError as RedisConnectionError
|
||||||
|
from redis.exceptions import TimeoutError as RedisTimeoutError
|
||||||
|
from sqlalchemy.exc import DBAPIError, InterfaceError, OperationalError, SQLAlchemyError
|
||||||
|
from sqlalchemy.exc import TimeoutError as PoolTimeoutError
|
||||||
|
from starlette.exceptions import HTTPException as StarletteHTTPException
|
||||||
|
from starlette.requests import ClientDisconnect
|
||||||
|
from urllib3.exceptions import HTTPError as StorageHTTPError
|
||||||
|
|
||||||
|
from app.core.instance import INSTANCE_ID_HEADER
|
||||||
|
from app.core.request_id import REQUEST_ID_HEADER
|
||||||
|
from app.errors.exceptions import (
|
||||||
|
ApiError,
|
||||||
|
internal_error,
|
||||||
|
invalid_request,
|
||||||
|
not_found,
|
||||||
|
unavailable,
|
||||||
|
)
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
TRANSIENT_SQLSTATE_CLASSES = ("08", "40", "53", "57")
|
||||||
|
TRANSIENT_SQLSTATES = frozenset({"55P03"})
|
||||||
|
RECOVERABLE_STORAGE_ERRORS = (
|
||||||
|
ApiError,
|
||||||
|
SQLAlchemyError,
|
||||||
|
MinioException,
|
||||||
|
StorageHTTPError,
|
||||||
|
OSError,
|
||||||
|
TimeoutError,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@asynccontextmanager
|
||||||
|
async def operation_timeout(seconds: float, error: ApiError):
|
||||||
|
"""Единое преобразование таймаутов; владелец операции задаёт ожидаемую ошибку."""
|
||||||
|
try:
|
||||||
|
async with asyncio.timeout(seconds):
|
||||||
|
yield
|
||||||
|
except TimeoutError:
|
||||||
|
raise error from None
|
||||||
|
|
||||||
|
|
||||||
|
async def client_disconnect_handler(request: Request, exc: Exception) -> JSONResponse:
|
||||||
|
return await api_error_handler(
|
||||||
|
request, invalid_request("Загрузка прервана клиентом")
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def log_recoverable_error(operation: str, error: Exception) -> None:
|
||||||
|
"""Согласованный безопасный лог восстановления без содержимого SDK-ошибки."""
|
||||||
|
logger.warning("%s deferred: %s", operation, type(error).__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def _get_request_id(request: Request) -> str:
|
||||||
|
request_id = getattr(request.state, "request_id", None)
|
||||||
|
|
||||||
|
if request_id is None:
|
||||||
|
request_id = str(uuid4())
|
||||||
|
request.state.request_id = request_id
|
||||||
|
|
||||||
|
return request_id
|
||||||
|
|
||||||
|
|
||||||
|
def _build_response(
|
||||||
|
request: Request,
|
||||||
|
status_code: int,
|
||||||
|
code: str,
|
||||||
|
message: str,
|
||||||
|
headers: Mapping[str, str] | None = None,
|
||||||
|
) -> JSONResponse:
|
||||||
|
request_id = _get_request_id(request)
|
||||||
|
|
||||||
|
response_headers = {
|
||||||
|
**(headers or {}),
|
||||||
|
REQUEST_ID_HEADER: request_id,
|
||||||
|
}
|
||||||
|
instance_id = getattr(request.app.state, "instance_id", None)
|
||||||
|
if instance_id is not None:
|
||||||
|
response_headers[INSTANCE_ID_HEADER] = instance_id
|
||||||
|
|
||||||
|
body = {
|
||||||
|
"error": {
|
||||||
|
"code": code,
|
||||||
|
"message": message,
|
||||||
|
"request_id": request_id,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return JSONResponse(
|
||||||
|
status_code=status_code,
|
||||||
|
content=body,
|
||||||
|
headers=response_headers,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def api_error_handler(
|
||||||
|
request: Request,
|
||||||
|
exc: Exception,
|
||||||
|
) -> JSONResponse:
|
||||||
|
assert isinstance(exc, ApiError)
|
||||||
|
|
||||||
|
return _build_response(
|
||||||
|
request,
|
||||||
|
exc.status_code,
|
||||||
|
exc.code,
|
||||||
|
exc.message,
|
||||||
|
exc.headers,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def routing_error_handler(
|
||||||
|
request: Request,
|
||||||
|
exc: Exception,
|
||||||
|
) -> JSONResponse:
|
||||||
|
assert isinstance(exc, StarletteHTTPException)
|
||||||
|
|
||||||
|
error = invalid_request()
|
||||||
|
if exc.status_code == 404:
|
||||||
|
error = not_found()
|
||||||
|
return _build_response(
|
||||||
|
request,
|
||||||
|
exc.status_code,
|
||||||
|
error.code,
|
||||||
|
error.message,
|
||||||
|
exc.headers,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def validation_error_handler(
|
||||||
|
request: Request,
|
||||||
|
exc: Exception,
|
||||||
|
) -> JSONResponse:
|
||||||
|
assert isinstance(exc, RequestValidationError)
|
||||||
|
|
||||||
|
fields = sorted({".".join(map(str, error["loc"])) for error in exc.errors()})
|
||||||
|
message = "Неверный запрос"
|
||||||
|
|
||||||
|
if fields:
|
||||||
|
message = f"{message}: {', '.join(fields)}"
|
||||||
|
|
||||||
|
return _build_response(
|
||||||
|
request,
|
||||||
|
400,
|
||||||
|
"invalid_request",
|
||||||
|
message,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def dependency_unavailable_handler(
|
||||||
|
request: Request,
|
||||||
|
exc: Exception,
|
||||||
|
) -> JSONResponse:
|
||||||
|
"""PostgreSQL/Redis/S3 недоступны либо исчерпан пул: 503 без внутренних деталей."""
|
||||||
|
request_id = _get_request_id(request)
|
||||||
|
logger.error(
|
||||||
|
"Dependency unavailable request_id=%s: %s",
|
||||||
|
request_id,
|
||||||
|
type(exc).__name__,
|
||||||
|
)
|
||||||
|
|
||||||
|
error = unavailable()
|
||||||
|
return _build_response(
|
||||||
|
request,
|
||||||
|
error.status_code,
|
||||||
|
error.code,
|
||||||
|
error.message,
|
||||||
|
{"Retry-After": "1"},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _is_transient_database_error(exc: DBAPIError) -> bool:
|
||||||
|
sqlstate = getattr(exc.orig, "sqlstate", None) or ""
|
||||||
|
return (
|
||||||
|
sqlstate.startswith(TRANSIENT_SQLSTATE_CLASSES)
|
||||||
|
or sqlstate in TRANSIENT_SQLSTATES
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def database_error_handler(
|
||||||
|
request: Request,
|
||||||
|
exc: Exception,
|
||||||
|
) -> JSONResponse:
|
||||||
|
"""Временные ошибки PostgreSQL (таймаут запроса, блокировки, нехватка ресурсов) — 503."""
|
||||||
|
assert isinstance(exc, DBAPIError)
|
||||||
|
|
||||||
|
handler = unhandled_error_handler
|
||||||
|
if _is_transient_database_error(exc):
|
||||||
|
handler = dependency_unavailable_handler
|
||||||
|
return await handler(request, exc)
|
||||||
|
|
||||||
|
|
||||||
|
async def unhandled_error_handler(
|
||||||
|
request: Request,
|
||||||
|
exc: Exception,
|
||||||
|
) -> JSONResponse:
|
||||||
|
request_id = _get_request_id(request)
|
||||||
|
|
||||||
|
logger.error(
|
||||||
|
"Unhandled error request_id=%s",
|
||||||
|
request_id,
|
||||||
|
exc_info=exc,
|
||||||
|
)
|
||||||
|
|
||||||
|
error = internal_error()
|
||||||
|
|
||||||
|
return _build_response(
|
||||||
|
request,
|
||||||
|
error.status_code,
|
||||||
|
error.code,
|
||||||
|
error.message,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def register_exception_handlers(app: FastAPI) -> None:
|
||||||
|
app.add_exception_handler(ApiError, api_error_handler)
|
||||||
|
app.add_exception_handler(ClientDisconnect, client_disconnect_handler)
|
||||||
|
app.add_exception_handler(MinioException, dependency_unavailable_handler)
|
||||||
|
app.add_exception_handler(StorageHTTPError, dependency_unavailable_handler)
|
||||||
|
app.add_exception_handler(StarletteHTTPException, routing_error_handler)
|
||||||
|
app.add_exception_handler(RequestValidationError, validation_error_handler)
|
||||||
|
app.add_exception_handler(OperationalError, dependency_unavailable_handler)
|
||||||
|
app.add_exception_handler(InterfaceError, dependency_unavailable_handler)
|
||||||
|
app.add_exception_handler(PoolTimeoutError, dependency_unavailable_handler)
|
||||||
|
app.add_exception_handler(DBAPIError, database_error_handler)
|
||||||
|
app.add_exception_handler(RedisConnectionError, dependency_unavailable_handler)
|
||||||
|
app.add_exception_handler(RedisTimeoutError, dependency_unavailable_handler)
|
||||||
|
app.add_exception_handler(OSError, dependency_unavailable_handler)
|
||||||
|
app.add_exception_handler(Exception, unhandled_error_handler)
|
||||||
+94
@@ -0,0 +1,94 @@
|
|||||||
|
import logging
|
||||||
|
from collections.abc import AsyncGenerator
|
||||||
|
from contextlib import asynccontextmanager
|
||||||
|
|
||||||
|
import uvicorn
|
||||||
|
from fastapi import Depends, FastAPI
|
||||||
|
|
||||||
|
from app.core.config import Settings
|
||||||
|
from app.core.database import create_db_engine, create_session_factory
|
||||||
|
from app.core.instance import InstanceIdMiddleware, resolve_instance_id
|
||||||
|
from app.core.lifecycle import GracefulServer, Lifecycle
|
||||||
|
from app.core.redis import create_redis_client
|
||||||
|
from app.core.request_capacity import acquire_request_slot, create_request_capacity
|
||||||
|
from app.core.request_id import RequestIdMiddleware
|
||||||
|
from app.endpoints.auth import router as auth_router
|
||||||
|
from app.endpoints.attachments import router as attachments_router
|
||||||
|
from app.endpoints.chats import router as chats_router
|
||||||
|
from app.endpoints.health import router as health_router
|
||||||
|
from app.endpoints.messages import router as messages_router
|
||||||
|
from app.endpoints.users import router as users_router
|
||||||
|
from app.errors.handlers import register_exception_handlers
|
||||||
|
from app.storage.s3 import create_s3_storage
|
||||||
|
|
||||||
|
|
||||||
|
def configure_logging(settings: Settings) -> None:
|
||||||
|
logging.basicConfig(
|
||||||
|
level=settings.log_level,
|
||||||
|
format="%(asctime)s %(levelname)s %(name)s %(message)s",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def create_app(settings: Settings | None = None) -> FastAPI:
|
||||||
|
settings = settings or Settings() # type: ignore
|
||||||
|
configure_logging(settings)
|
||||||
|
|
||||||
|
engine = create_db_engine(settings)
|
||||||
|
redis = create_redis_client(settings)
|
||||||
|
s3 = create_s3_storage(settings.s3) if settings.s3_enabled else None
|
||||||
|
|
||||||
|
@asynccontextmanager
|
||||||
|
async def lifespan(_: FastAPI) -> AsyncGenerator[None]:
|
||||||
|
yield
|
||||||
|
if s3 is not None:
|
||||||
|
await s3.aclose()
|
||||||
|
await redis.aclose()
|
||||||
|
await engine.dispose()
|
||||||
|
|
||||||
|
application = FastAPI(title="Messenger", version="1.5.0", lifespan=lifespan)
|
||||||
|
|
||||||
|
application.state.settings = settings
|
||||||
|
application.state.engine = engine
|
||||||
|
application.state.session_factory = create_session_factory(engine)
|
||||||
|
application.state.redis = redis
|
||||||
|
application.state.s3 = s3
|
||||||
|
application.state.upload_capacity = create_request_capacity(
|
||||||
|
settings.s3.max_concurrent_uploads
|
||||||
|
)
|
||||||
|
application.state.request_capacity = create_request_capacity(settings.max_in_flight)
|
||||||
|
application.state.instance_id = resolve_instance_id(settings.instance_id)
|
||||||
|
application.state.lifecycle = Lifecycle()
|
||||||
|
application.add_middleware(RequestIdMiddleware)
|
||||||
|
application.add_middleware(
|
||||||
|
InstanceIdMiddleware, instance_id=application.state.instance_id
|
||||||
|
)
|
||||||
|
register_exception_handlers(application)
|
||||||
|
|
||||||
|
limited = [Depends(acquire_request_slot)]
|
||||||
|
application.include_router(health_router)
|
||||||
|
application.include_router(users_router, dependencies=limited)
|
||||||
|
application.include_router(auth_router, dependencies=limited)
|
||||||
|
application.include_router(chats_router, dependencies=limited)
|
||||||
|
application.include_router(messages_router, dependencies=limited)
|
||||||
|
application.include_router(attachments_router, dependencies=limited)
|
||||||
|
|
||||||
|
return application
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
settings = Settings() # type: ignore
|
||||||
|
application = create_app(settings)
|
||||||
|
config = uvicorn.Config(
|
||||||
|
application,
|
||||||
|
host=settings.app_host,
|
||||||
|
port=settings.app_port,
|
||||||
|
log_level=settings.log_level.lower(),
|
||||||
|
timeout_graceful_shutdown=settings.graceful_shutdown_timeout_seconds,
|
||||||
|
)
|
||||||
|
GracefulServer(
|
||||||
|
config, application.state.lifecycle, settings.shutdown_drain_seconds
|
||||||
|
).run()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
from app.models.job_replay import JobReplay
|
||||||
|
from app.models.base import Base
|
||||||
|
from app.models.job import AttachmentJob
|
||||||
|
from app.models.attachment import Attachment, MessageAttachment, UploadIntent
|
||||||
|
from app.models.chat import Chat
|
||||||
|
from app.models.chat_member import ChatMember
|
||||||
|
from app.models.counter import Counter
|
||||||
|
from app.models.idempotency_key import IdempotencyKey
|
||||||
|
from app.models.message import Message
|
||||||
|
from app.models.user import User
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
"JobReplay",
|
||||||
|
"AttachmentJob",
|
||||||
|
"Base",
|
||||||
|
"Attachment",
|
||||||
|
"MessageAttachment",
|
||||||
|
"UploadIntent",
|
||||||
|
"Chat",
|
||||||
|
"ChatMember",
|
||||||
|
"Counter",
|
||||||
|
"IdempotencyKey",
|
||||||
|
"Message",
|
||||||
|
"User",
|
||||||
|
]
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
"""Метаданные оригиналов и журнал операций до записи в S3."""
|
||||||
|
|
||||||
|
import uuid
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
from sqlalchemy import CheckConstraint, ForeignKey, Index, String, func, text
|
||||||
|
from sqlalchemy.dialects.postgresql import JSONB
|
||||||
|
from sqlalchemy.orm import Mapped, mapped_column
|
||||||
|
|
||||||
|
from app.models.base import Base
|
||||||
|
|
||||||
|
|
||||||
|
class Attachment(Base):
|
||||||
|
__tablename__ = "attachments"
|
||||||
|
__table_args__ = (
|
||||||
|
CheckConstraint("size_bytes BETWEEN 1 AND 10485760", name="size_bytes"),
|
||||||
|
CheckConstraint(
|
||||||
|
"content_type IN ('image/png', 'image/jpeg')", name="content_type"
|
||||||
|
),
|
||||||
|
CheckConstraint("sha256 ~ '^[a-f0-9]{64}$'", name="sha256"),
|
||||||
|
CheckConstraint(
|
||||||
|
"status IN ('queued', 'processing', 'ready', 'failed')", name="status"
|
||||||
|
),
|
||||||
|
CheckConstraint(
|
||||||
|
"(status = 'failed' AND error_code IS NOT NULL) OR (status <> 'failed' AND error_code IS NULL)",
|
||||||
|
name="error_code",
|
||||||
|
),
|
||||||
|
Index("ix_attachments_chat_id", "chat_id"),
|
||||||
|
)
|
||||||
|
|
||||||
|
id: Mapped[uuid.UUID] = mapped_column(primary_key=True, default=uuid.uuid4)
|
||||||
|
chat_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("chats.id"))
|
||||||
|
owner_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("users.id"))
|
||||||
|
object_key: Mapped[str] = mapped_column(String(128), unique=True)
|
||||||
|
filename: Mapped[str] = mapped_column(String(255))
|
||||||
|
content_type: Mapped[str] = mapped_column(String(32))
|
||||||
|
size_bytes: Mapped[int]
|
||||||
|
sha256: Mapped[str] = mapped_column(String(64))
|
||||||
|
status: Mapped[str] = mapped_column(String(16), server_default="queued")
|
||||||
|
error_code: Mapped[str | None] = mapped_column(String(64))
|
||||||
|
thumbnail_key: Mapped[str | None] = mapped_column(String(128))
|
||||||
|
crop_key: Mapped[str | None] = mapped_column(String(128))
|
||||||
|
optimized_key: Mapped[str | None] = mapped_column(String(128))
|
||||||
|
variants: Mapped[list[dict]] = mapped_column(
|
||||||
|
JSONB, default=list, server_default=text("'[]'::jsonb")
|
||||||
|
)
|
||||||
|
created_at: Mapped[datetime] = mapped_column(server_default=func.clock_timestamp())
|
||||||
|
updated_at: Mapped[datetime] = mapped_column(server_default=func.clock_timestamp())
|
||||||
|
|
||||||
|
|
||||||
|
class MessageAttachment(Base):
|
||||||
|
__tablename__ = "message_attachments"
|
||||||
|
|
||||||
|
message_id: Mapped[uuid.UUID] = mapped_column(
|
||||||
|
ForeignKey("messages.id", ondelete="CASCADE"), primary_key=True
|
||||||
|
)
|
||||||
|
attachment_id: Mapped[uuid.UUID] = mapped_column(
|
||||||
|
ForeignKey("attachments.id"), primary_key=True
|
||||||
|
)
|
||||||
|
position: Mapped[int]
|
||||||
|
|
||||||
|
|
||||||
|
class UploadIntent(Base):
|
||||||
|
"""Фиксируется до PUT; удаляется атомарно с принятием queued metadata/job."""
|
||||||
|
|
||||||
|
__tablename__ = "upload_intents"
|
||||||
|
__table_args__ = (Index("ix_upload_intents_created_at", "created_at"),)
|
||||||
|
|
||||||
|
id: Mapped[uuid.UUID] = mapped_column(primary_key=True)
|
||||||
|
object_key: Mapped[str] = mapped_column(String(128), unique=True)
|
||||||
|
created_at: Mapped[datetime] = mapped_column(server_default=func.clock_timestamp())
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
import uuid
|
||||||
|
from datetime import datetime
|
||||||
|
from typing import Any, ClassVar
|
||||||
|
|
||||||
|
from sqlalchemy import DateTime, MetaData
|
||||||
|
from sqlalchemy.dialects.postgresql import UUID as PG_UUID
|
||||||
|
from sqlalchemy.orm import DeclarativeBase
|
||||||
|
from sqlalchemy.types import TypeEngine
|
||||||
|
|
||||||
|
NAMING_CONVENTION = {
|
||||||
|
"ix": "ix_%(column_0_label)s",
|
||||||
|
"uq": "uq_%(table_name)s_%(column_0_name)s",
|
||||||
|
"ck": "ck_%(table_name)s_%(constraint_name)s",
|
||||||
|
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
|
||||||
|
"pk": "pk_%(table_name)s",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class Base(DeclarativeBase):
|
||||||
|
"""Базовый класс ORM-моделей."""
|
||||||
|
|
||||||
|
metadata = MetaData(naming_convention=NAMING_CONVENTION)
|
||||||
|
|
||||||
|
type_annotation_map: ClassVar[dict[Any, TypeEngine[Any]]] = {
|
||||||
|
uuid.UUID: PG_UUID(as_uuid=True),
|
||||||
|
datetime: DateTime(timezone=True),
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
import uuid
|
||||||
|
from datetime import datetime
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
from sqlalchemy import String, func
|
||||||
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||||
|
|
||||||
|
from app.models.base import Base
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from app.models.chat_member import ChatMember
|
||||||
|
from app.models.message import Message
|
||||||
|
|
||||||
|
|
||||||
|
class Chat(Base):
|
||||||
|
"""Чат с фиксированным составом участников."""
|
||||||
|
|
||||||
|
__tablename__ = "chats"
|
||||||
|
|
||||||
|
id: Mapped[uuid.UUID] = mapped_column(primary_key=True, default=uuid.uuid4)
|
||||||
|
title: Mapped[str] = mapped_column(String(100))
|
||||||
|
created_at: Mapped[datetime] = mapped_column(server_default=func.now())
|
||||||
|
|
||||||
|
members: Mapped[list["ChatMember"]] = relationship(
|
||||||
|
back_populates="chat",
|
||||||
|
cascade="all, delete-orphan",
|
||||||
|
passive_deletes=True,
|
||||||
|
lazy="selectin",
|
||||||
|
)
|
||||||
|
messages: Mapped[list["Message"]] = relationship(
|
||||||
|
back_populates="chat", lazy="raise"
|
||||||
|
)
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
import uuid
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
from sqlalchemy import ForeignKey, Index
|
||||||
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||||
|
|
||||||
|
from app.models.base import Base
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from app.models.chat import Chat
|
||||||
|
from app.models.user import User
|
||||||
|
|
||||||
|
|
||||||
|
class ChatMember(Base):
|
||||||
|
"""Членство пользователя в чате."""
|
||||||
|
|
||||||
|
__tablename__ = "chat_members"
|
||||||
|
__table_args__ = (Index("ix_chat_members_user_id_chat_id", "user_id", "chat_id"),)
|
||||||
|
|
||||||
|
chat_id: Mapped[uuid.UUID] = mapped_column(
|
||||||
|
ForeignKey("chats.id", ondelete="CASCADE"), primary_key=True
|
||||||
|
)
|
||||||
|
user_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("users.id"), primary_key=True)
|
||||||
|
|
||||||
|
chat: Mapped["Chat"] = relationship(back_populates="members", lazy="raise")
|
||||||
|
user: Mapped["User"] = relationship(back_populates="memberships", lazy="raise")
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
from sqlalchemy import BigInteger, String
|
||||||
|
from sqlalchemy.orm import Mapped, mapped_column
|
||||||
|
|
||||||
|
from app.models.base import Base
|
||||||
|
|
||||||
|
|
||||||
|
class Counter(Base):
|
||||||
|
"""Именованный счётчик с атомарным обновлением."""
|
||||||
|
|
||||||
|
__tablename__ = "counters"
|
||||||
|
|
||||||
|
name: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||||
|
value: Mapped[int] = mapped_column(BigInteger)
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
"""Сохранённый результат отправки сообщения."""
|
||||||
|
|
||||||
|
import uuid
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
from sqlalchemy import ForeignKey, Index, String, func
|
||||||
|
from sqlalchemy.orm import Mapped, mapped_column
|
||||||
|
|
||||||
|
from app.models.base import Base
|
||||||
|
|
||||||
|
|
||||||
|
class IdempotencyKey(Base):
|
||||||
|
"""Ключ запроса и связанное сообщение."""
|
||||||
|
|
||||||
|
__tablename__ = "idempotency_keys"
|
||||||
|
__table_args__ = (Index("ix_idempotency_keys_created_at", "created_at"),)
|
||||||
|
|
||||||
|
user_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("users.id"), primary_key=True)
|
||||||
|
method: Mapped[str] = mapped_column(String(8), primary_key=True)
|
||||||
|
path: Mapped[str] = mapped_column(String(255), primary_key=True)
|
||||||
|
key: Mapped[str] = mapped_column(String(128), primary_key=True)
|
||||||
|
request_hash: Mapped[str] = mapped_column(String(64))
|
||||||
|
message_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("messages.id"))
|
||||||
|
created_at: Mapped[datetime] = mapped_column(server_default=func.clock_timestamp())
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
"""Устойчивая работа и намерение публикации; один pipeline на вложение."""
|
||||||
|
|
||||||
|
from datetime import datetime
|
||||||
|
from uuid import UUID, uuid4
|
||||||
|
|
||||||
|
from sqlalchemy import CheckConstraint, ForeignKey, Index, String, func
|
||||||
|
from sqlalchemy.orm import Mapped, mapped_column
|
||||||
|
|
||||||
|
from app.models.base import Base
|
||||||
|
|
||||||
|
|
||||||
|
class AttachmentJob(Base):
|
||||||
|
__tablename__ = "attachment_jobs"
|
||||||
|
__table_args__ = (
|
||||||
|
CheckConstraint("pipeline_version = 1", name="pipeline_version"),
|
||||||
|
CheckConstraint("attempt >= 0 AND published_attempt >= 0", name="attempt"),
|
||||||
|
Index("ix_attachment_jobs_due", "next_attempt_at"),
|
||||||
|
Index("ix_attachment_jobs_lease", "lease_until"),
|
||||||
|
)
|
||||||
|
|
||||||
|
id: Mapped[UUID] = mapped_column(primary_key=True, default=uuid4)
|
||||||
|
attachment_id: Mapped[UUID] = mapped_column(
|
||||||
|
ForeignKey("attachments.id"),
|
||||||
|
unique=True,
|
||||||
|
)
|
||||||
|
staging_key: Mapped[str | None] = mapped_column(String(128))
|
||||||
|
pipeline_version: Mapped[int] = mapped_column(server_default="1")
|
||||||
|
attempt: Mapped[int] = mapped_column(server_default="0")
|
||||||
|
cycle_start_attempt: Mapped[int] = mapped_column(server_default="0")
|
||||||
|
attempt_limit: Mapped[int | None]
|
||||||
|
last_error_permanent: Mapped[bool | None]
|
||||||
|
published_attempt: Mapped[int] = mapped_column(server_default="0")
|
||||||
|
next_attempt_at: Mapped[datetime] = mapped_column(
|
||||||
|
server_default=func.clock_timestamp()
|
||||||
|
)
|
||||||
|
lease_token: Mapped[UUID | None]
|
||||||
|
lease_until: Mapped[datetime | None]
|
||||||
|
last_error_code: Mapped[str | None] = mapped_column(String(64))
|
||||||
|
finished_at: Mapped[datetime | None]
|
||||||
|
created_at: Mapped[datetime] = mapped_column(server_default=func.clock_timestamp())
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
"""Аудит явного повторного запуска failed; request id обеспечивает идемпотентность."""
|
||||||
|
|
||||||
|
from datetime import datetime
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from sqlalchemy import ForeignKey, String, func
|
||||||
|
from sqlalchemy.orm import Mapped, mapped_column
|
||||||
|
|
||||||
|
from app.models.base import Base
|
||||||
|
|
||||||
|
|
||||||
|
class JobReplay(Base):
|
||||||
|
__tablename__ = "job_replays"
|
||||||
|
|
||||||
|
id: Mapped[UUID] = mapped_column(primary_key=True)
|
||||||
|
job_id: Mapped[UUID] = mapped_column(ForeignKey("attachment_jobs.id"), index=True)
|
||||||
|
actor: Mapped[str] = mapped_column(String(64))
|
||||||
|
reason: Mapped[str] = mapped_column(String(32))
|
||||||
|
previous_attempt: Mapped[int]
|
||||||
|
attempt_limit: Mapped[int]
|
||||||
|
previous_error_code: Mapped[str | None] = mapped_column(String(64))
|
||||||
|
created_at: Mapped[datetime] = mapped_column(server_default=func.clock_timestamp())
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
import uuid
|
||||||
|
from datetime import datetime
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
from sqlalchemy import ForeignKey, Index, Text, func
|
||||||
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||||
|
|
||||||
|
from app.models.base import Base
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from app.models.chat import Chat
|
||||||
|
from app.models.user import User
|
||||||
|
|
||||||
|
|
||||||
|
class Message(Base):
|
||||||
|
"""Сообщение в чате."""
|
||||||
|
|
||||||
|
__tablename__ = "messages"
|
||||||
|
__table_args__ = (
|
||||||
|
Index("ix_messages_chat_id_created_at_id", "chat_id", "created_at", "id"),
|
||||||
|
)
|
||||||
|
|
||||||
|
id: Mapped[uuid.UUID] = mapped_column(primary_key=True, default=uuid.uuid4)
|
||||||
|
chat_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("chats.id"))
|
||||||
|
sender_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("users.id"))
|
||||||
|
text: Mapped[str] = mapped_column(Text)
|
||||||
|
created_at: Mapped[datetime] = mapped_column(server_default=func.clock_timestamp())
|
||||||
|
|
||||||
|
chat: Mapped["Chat"] = relationship(back_populates="messages", lazy="raise")
|
||||||
|
sender: Mapped["User"] = relationship(back_populates="messages", lazy="raise")
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
import uuid
|
||||||
|
from datetime import datetime
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
from sqlalchemy import String, func
|
||||||
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||||
|
|
||||||
|
from app.models.base import Base
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from app.models.chat_member import ChatMember
|
||||||
|
from app.models.message import Message
|
||||||
|
|
||||||
|
|
||||||
|
class User(Base):
|
||||||
|
"""Пользователь и его учётные данные."""
|
||||||
|
|
||||||
|
__tablename__ = "users"
|
||||||
|
|
||||||
|
id: Mapped[uuid.UUID] = mapped_column(primary_key=True, default=uuid.uuid4)
|
||||||
|
username: Mapped[str] = mapped_column(String(32), unique=True)
|
||||||
|
display_name: Mapped[str] = mapped_column(String(100))
|
||||||
|
password_hash: Mapped[str] = mapped_column(String(255))
|
||||||
|
created_at: Mapped[datetime] = mapped_column(server_default=func.now())
|
||||||
|
|
||||||
|
memberships: Mapped[list["ChatMember"]] = relationship(
|
||||||
|
back_populates="user", lazy="raise"
|
||||||
|
)
|
||||||
|
messages: Mapped[list["Message"]] = relationship(
|
||||||
|
back_populates="sender", lazy="raise"
|
||||||
|
)
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
from app.schemas.chat import Chat
|
||||||
|
from app.schemas.message import Message
|
||||||
|
from app.schemas.session import AuthSession
|
||||||
|
from app.schemas.user import User
|
||||||
|
|
||||||
|
__all__ = ["AuthSession", "Chat", "Message", "User"]
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
"""Публичные состояния и варианты; внутренние ключи S3 не сериализуются."""
|
||||||
|
|
||||||
|
from datetime import datetime
|
||||||
|
from typing import Literal
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
||||||
|
|
||||||
|
from app.schemas.base import Base
|
||||||
|
|
||||||
|
|
||||||
|
class Variant(BaseModel):
|
||||||
|
model_config = ConfigDict(extra="forbid", from_attributes=True)
|
||||||
|
|
||||||
|
name: Literal["thumbnail", "crop", "optimized"]
|
||||||
|
content_type: Literal["image/jpeg", "image/png", "image/webp"]
|
||||||
|
size_bytes: int = Field(ge=1)
|
||||||
|
width: int = Field(ge=1)
|
||||||
|
height: int = Field(ge=1)
|
||||||
|
sha256: str = Field(pattern=r"^[a-f0-9]{64}$")
|
||||||
|
|
||||||
|
|
||||||
|
class Attachment(Base):
|
||||||
|
chat_id: UUID
|
||||||
|
owner_id: UUID
|
||||||
|
filename: str = Field(min_length=1, max_length=255)
|
||||||
|
content_type: Literal["image/png", "image/jpeg"]
|
||||||
|
size_bytes: int = Field(ge=1, le=10_485_760)
|
||||||
|
sha256: str = Field(pattern=r"^[a-f0-9]{64}$")
|
||||||
|
status: Literal["queued", "processing", "ready", "failed"]
|
||||||
|
variants: list[Variant] = Field(default_factory=list)
|
||||||
|
error_code: str | None = None
|
||||||
|
created_at: datetime
|
||||||
|
updated_at: datetime
|
||||||
|
|
||||||
|
|
||||||
|
VARIANT_FILENAMES = {
|
||||||
|
"thumbnail": "thumbnail.jpg",
|
||||||
|
"crop": "crop.jpg",
|
||||||
|
"optimized": "optimized.webp",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class PipelineResult(BaseModel):
|
||||||
|
model_config = ConfigDict(extra="forbid")
|
||||||
|
variants: list[Variant] = Field(min_length=3, max_length=3)
|
||||||
|
|
||||||
|
@field_validator("variants")
|
||||||
|
@classmethod
|
||||||
|
def require_all_variants(cls, variants: list[Variant]) -> list[Variant]:
|
||||||
|
if {item.name for item in variants} != set(VARIANT_FILENAMES):
|
||||||
|
raise ValueError("Нужны thumbnail, crop и optimized без повторов")
|
||||||
|
return variants
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
from typing import Literal
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from pydantic import BaseModel, ConfigDict, field_validator
|
||||||
|
|
||||||
|
|
||||||
|
class Base(BaseModel):
|
||||||
|
model_config = ConfigDict(extra="forbid", from_attributes=True)
|
||||||
|
|
||||||
|
id: UUID
|
||||||
|
|
||||||
|
|
||||||
|
class Health(BaseModel):
|
||||||
|
model_config = ConfigDict(extra="forbid")
|
||||||
|
|
||||||
|
status: Literal["ok"]
|
||||||
|
|
||||||
|
|
||||||
|
class BaseCreate(BaseModel):
|
||||||
|
model_config = ConfigDict(extra="forbid")
|
||||||
|
|
||||||
|
@field_validator("*", mode="before")
|
||||||
|
@classmethod
|
||||||
|
def reject_invalid_strings(cls, value: object) -> object:
|
||||||
|
"""Отклоняет строки из одних пробелов и строки с NUL, который не хранит PostgreSQL."""
|
||||||
|
if isinstance(value, str):
|
||||||
|
if not value.strip():
|
||||||
|
raise ValueError("Строка не может состоять только из пробелов")
|
||||||
|
if "\x00" in value:
|
||||||
|
raise ValueError("Строка не может содержать символ NUL")
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
class BasePage(BaseModel):
|
||||||
|
model_config = ConfigDict(extra="forbid")
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
from datetime import datetime
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from pydantic import Field
|
||||||
|
|
||||||
|
from app.schemas.base import Base, BaseCreate, BasePage
|
||||||
|
|
||||||
|
|
||||||
|
class Chat(Base):
|
||||||
|
title: str = Field(min_length=1, max_length=100)
|
||||||
|
member_ids: list[UUID] = Field(min_length=1, max_length=100)
|
||||||
|
created_at: datetime
|
||||||
|
|
||||||
|
|
||||||
|
class CreateChat(BaseCreate):
|
||||||
|
title: str = Field(min_length=1, max_length=100)
|
||||||
|
member_ids: list[UUID] = Field(min_length=1, max_length=100)
|
||||||
|
|
||||||
|
|
||||||
|
class ChatPage(BasePage):
|
||||||
|
items: list[Chat]
|
||||||
|
next_cursor: str | None
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
"""Сообщение брокера: только идентификаторы и служебные поля контракта v1."""
|
||||||
|
|
||||||
|
from typing import Literal
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from pydantic import AwareDatetime, BaseModel, ConfigDict, Field
|
||||||
|
|
||||||
|
|
||||||
|
class JobMessage(BaseModel):
|
||||||
|
model_config = ConfigDict(extra="forbid")
|
||||||
|
|
||||||
|
schema_version: Literal[1] = 1
|
||||||
|
type: Literal["attachment.process.v1"] = "attachment.process.v1"
|
||||||
|
job_id: UUID
|
||||||
|
attachment_id: UUID
|
||||||
|
pipeline_version: int = Field(default=1, ge=1)
|
||||||
|
attempt: int = Field(ge=1)
|
||||||
|
created_at: AwareDatetime
|
||||||
|
traceparent: str | None = Field(
|
||||||
|
default=None, pattern=r"^00-[0-9a-f]{32}-[0-9a-f]{16}-[0-9a-f]{2}$"
|
||||||
|
)
|
||||||
|
tracestate: str | None = Field(default=None, max_length=512)
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
from datetime import datetime
|
||||||
|
from typing import Self
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from pydantic import Field, field_validator, model_validator
|
||||||
|
|
||||||
|
from app.schemas.base import Base, BaseCreate, BasePage
|
||||||
|
|
||||||
|
|
||||||
|
class Message(Base):
|
||||||
|
chat_id: UUID
|
||||||
|
sender_id: UUID
|
||||||
|
text: str = Field(max_length=4000)
|
||||||
|
attachment_ids: list[UUID] = Field(default_factory=list, max_length=10)
|
||||||
|
created_at: datetime
|
||||||
|
|
||||||
|
|
||||||
|
class CreateMessage(BaseCreate):
|
||||||
|
text: str = Field(max_length=4000)
|
||||||
|
attachment_ids: list[UUID] = Field(default_factory=list, max_length=10)
|
||||||
|
|
||||||
|
@field_validator("*", mode="before")
|
||||||
|
@classmethod
|
||||||
|
def reject_invalid_strings(cls, value: object) -> object:
|
||||||
|
if isinstance(value, str):
|
||||||
|
if "\x00" in value or (value != "" and not value.strip()):
|
||||||
|
raise ValueError("Некорректная строка сообщения")
|
||||||
|
return value
|
||||||
|
|
||||||
|
@model_validator(mode="after")
|
||||||
|
def require_content(self) -> Self:
|
||||||
|
if not self.text and not self.attachment_ids:
|
||||||
|
raise ValueError("Нужен текст или вложение")
|
||||||
|
if len(self.attachment_ids) != len(set(self.attachment_ids)):
|
||||||
|
raise ValueError("attachment_ids содержит дубликаты")
|
||||||
|
return self
|
||||||
|
|
||||||
|
|
||||||
|
class MessagePage(BasePage):
|
||||||
|
items: list[Message]
|
||||||
|
next_cursor: str | None
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
from datetime import datetime
|
||||||
|
from typing import Literal
|
||||||
|
|
||||||
|
from pydantic import BaseModel, ConfigDict, Field
|
||||||
|
|
||||||
|
from app.schemas.user import User
|
||||||
|
|
||||||
|
|
||||||
|
class AuthSession(BaseModel):
|
||||||
|
"""Ответ POST /auth/sessions (в OpenAPI — Session)."""
|
||||||
|
|
||||||
|
model_config = ConfigDict(extra="forbid")
|
||||||
|
|
||||||
|
token: str = Field(min_length=32)
|
||||||
|
token_type: Literal["Bearer"] = "Bearer"
|
||||||
|
expires_at: datetime
|
||||||
|
user: User
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
from pydantic import Field
|
||||||
|
|
||||||
|
from app.schemas.base import Base, BaseCreate
|
||||||
|
|
||||||
|
USERNAME_PATTERN = r"^[a-z0-9_]{3,32}$"
|
||||||
|
PASSWORD_MIN_LENGTH = 12
|
||||||
|
PASSWORD_MAX_LENGTH = 128
|
||||||
|
|
||||||
|
|
||||||
|
class User(Base):
|
||||||
|
"""Публичное представление пользователя. Пароль/хеш сюда не попадают никогда."""
|
||||||
|
|
||||||
|
username: str = Field(pattern=USERNAME_PATTERN)
|
||||||
|
display_name: str = Field(min_length=1, max_length=100)
|
||||||
|
created_at: datetime
|
||||||
|
|
||||||
|
|
||||||
|
class CreateUser(BaseCreate):
|
||||||
|
username: str = Field(pattern=USERNAME_PATTERN)
|
||||||
|
display_name: str = Field(min_length=1, max_length=100)
|
||||||
|
password: str = Field(
|
||||||
|
min_length=PASSWORD_MIN_LENGTH, max_length=PASSWORD_MAX_LENGTH, repr=False
|
||||||
|
)
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
"""Доступ к данным PostgreSQL и Redis."""
|
||||||
@@ -0,0 +1,99 @@
|
|||||||
|
"""SQL-запросы метаданных и журнала; commit выполняется в domain."""
|
||||||
|
|
||||||
|
from datetime import timedelta
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from sqlalchemy import delete, func, select
|
||||||
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
|
from app.models.attachment import Attachment, MessageAttachment, UploadIntent
|
||||||
|
|
||||||
|
|
||||||
|
async def create_attachment(session: AsyncSession, **values) -> Attachment:
|
||||||
|
record = Attachment(**values)
|
||||||
|
session.add(record)
|
||||||
|
await session.flush()
|
||||||
|
return record
|
||||||
|
|
||||||
|
|
||||||
|
async def get_attachment(
|
||||||
|
session: AsyncSession, attachment_id: UUID
|
||||||
|
) -> Attachment | None:
|
||||||
|
return await session.get(Attachment, attachment_id)
|
||||||
|
|
||||||
|
|
||||||
|
async def get_by_key(session: AsyncSession, key: str) -> Attachment | None:
|
||||||
|
return await session.scalar(select(Attachment).where(Attachment.object_key == key))
|
||||||
|
|
||||||
|
|
||||||
|
async def get_for_chat(
|
||||||
|
session: AsyncSession, chat_id: UUID, attachment_ids: list[UUID]
|
||||||
|
) -> list[Attachment]:
|
||||||
|
records = []
|
||||||
|
if attachment_ids:
|
||||||
|
records = list(
|
||||||
|
await session.scalars(
|
||||||
|
select(Attachment).where(
|
||||||
|
Attachment.id.in_(attachment_ids),
|
||||||
|
Attachment.chat_id == chat_id,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return records
|
||||||
|
|
||||||
|
|
||||||
|
async def bind_attachments(
|
||||||
|
session: AsyncSession, message_id: UUID, attachment_ids: list[UUID]
|
||||||
|
) -> None:
|
||||||
|
session.add_all(
|
||||||
|
MessageAttachment(message_id=message_id, attachment_id=id, position=position)
|
||||||
|
for position, id in enumerate(attachment_ids)
|
||||||
|
)
|
||||||
|
await session.flush()
|
||||||
|
|
||||||
|
|
||||||
|
async def message_attachment_ids(
|
||||||
|
session: AsyncSession, message_ids: list[UUID]
|
||||||
|
) -> dict[UUID, list[UUID]]:
|
||||||
|
result: dict[UUID, list[UUID]] = {id: [] for id in message_ids}
|
||||||
|
if not message_ids:
|
||||||
|
return result
|
||||||
|
rows = await session.execute(
|
||||||
|
select(MessageAttachment)
|
||||||
|
.where(MessageAttachment.message_id.in_(message_ids))
|
||||||
|
.order_by(MessageAttachment.position)
|
||||||
|
)
|
||||||
|
for link in rows.scalars():
|
||||||
|
result[link.message_id].append(link.attachment_id)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
async def reserve_upload(session: AsyncSession, id: UUID, key: str) -> None:
|
||||||
|
session.add(UploadIntent(id=id, object_key=key))
|
||||||
|
await session.flush()
|
||||||
|
|
||||||
|
|
||||||
|
async def lock_upload(session: AsyncSession, id: UUID) -> UploadIntent | None:
|
||||||
|
return await session.scalar(
|
||||||
|
select(UploadIntent).where(UploadIntent.id == id).with_for_update()
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def finish_upload(session: AsyncSession, id: UUID) -> None:
|
||||||
|
await session.execute(delete(UploadIntent).where(UploadIntent.id == id))
|
||||||
|
|
||||||
|
|
||||||
|
async def stale_uploads(
|
||||||
|
session: AsyncSession, grace_seconds: int, limit: int
|
||||||
|
) -> list[UploadIntent]:
|
||||||
|
result = await session.scalars(
|
||||||
|
select(UploadIntent)
|
||||||
|
.where(
|
||||||
|
UploadIntent.created_at
|
||||||
|
< func.clock_timestamp() - timedelta(seconds=grace_seconds),
|
||||||
|
)
|
||||||
|
.order_by(UploadIntent.created_at)
|
||||||
|
.limit(limit)
|
||||||
|
.with_for_update(skip_locked=True)
|
||||||
|
)
|
||||||
|
return list(result)
|
||||||
@@ -0,0 +1,94 @@
|
|||||||
|
"""RabbitMQ: durable queue, persistent messages, publisher confirms и manual ack."""
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import logging
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
import aio_pika
|
||||||
|
from aio_pika.abc import AbstractIncomingMessage
|
||||||
|
|
||||||
|
from app.core.jobs import JobSettings
|
||||||
|
from app.schemas.job import JobMessage
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class Delivery:
|
||||||
|
id: AbstractIncomingMessage
|
||||||
|
payload: bytes
|
||||||
|
|
||||||
|
|
||||||
|
class JobBroker:
|
||||||
|
"""Один consumer/канал на экземпляр; publisher использует отдельный экземпляр."""
|
||||||
|
|
||||||
|
def __init__(self, settings: JobSettings) -> None:
|
||||||
|
self.settings = settings
|
||||||
|
self.connection = self.channel = self.queue = self.iterator = None
|
||||||
|
# Transport exceptions могут содержать URL с credentials.
|
||||||
|
logging.getLogger("aio_pika").setLevel(logging.CRITICAL)
|
||||||
|
logging.getLogger("aiormq").setLevel(logging.CRITICAL)
|
||||||
|
|
||||||
|
async def ensure_ready(self) -> None:
|
||||||
|
if (
|
||||||
|
self.connection is None
|
||||||
|
or self.connection.is_closed
|
||||||
|
or self.channel is None
|
||||||
|
or self.channel.is_closed
|
||||||
|
):
|
||||||
|
await self.aclose()
|
||||||
|
self.connection = await aio_pika.connect(
|
||||||
|
host=self.settings.broker_host,
|
||||||
|
port=self.settings.broker_port,
|
||||||
|
login=self.settings.broker_user,
|
||||||
|
password=self.settings.broker_password_file.read_text().strip(),
|
||||||
|
virtualhost=self.settings.broker_vhost,
|
||||||
|
timeout=5,
|
||||||
|
heartbeat=30,
|
||||||
|
)
|
||||||
|
self.channel = await self.connection.channel(
|
||||||
|
publisher_confirms=True, on_return_raises=True
|
||||||
|
)
|
||||||
|
await self.channel.set_qos(prefetch_count=1)
|
||||||
|
self.queue = await self.channel.declare_queue(
|
||||||
|
self.settings.broker_queue, durable=True
|
||||||
|
)
|
||||||
|
|
||||||
|
async def publish_job(self, message: JobMessage) -> None:
|
||||||
|
await self.ensure_ready()
|
||||||
|
await self.channel.default_exchange.publish(
|
||||||
|
aio_pika.Message(
|
||||||
|
body=message.model_dump_json(exclude_none=True).encode(),
|
||||||
|
content_type="application/json",
|
||||||
|
delivery_mode=aio_pika.DeliveryMode.PERSISTENT,
|
||||||
|
message_id=f"{message.job_id}:{message.attempt}",
|
||||||
|
),
|
||||||
|
routing_key=self.settings.broker_queue,
|
||||||
|
mandatory=True,
|
||||||
|
timeout=10,
|
||||||
|
)
|
||||||
|
|
||||||
|
async def receive(self, consumer: str) -> Delivery:
|
||||||
|
await self.ensure_ready()
|
||||||
|
if self.iterator is None:
|
||||||
|
self.iterator = self.queue.iterator(no_ack=False, consumer_tag=consumer)
|
||||||
|
await self.iterator.__aenter__()
|
||||||
|
message = await self.iterator.__anext__()
|
||||||
|
return Delivery(message, message.body)
|
||||||
|
|
||||||
|
async def acknowledge_job(self, delivery_id: AbstractIncomingMessage) -> None:
|
||||||
|
await delivery_id.ack()
|
||||||
|
|
||||||
|
async def defer_job(self, delivery_id: AbstractIncomingMessage) -> None:
|
||||||
|
# Живая чужая lease: не удерживать единственный prefetch slot навсегда.
|
||||||
|
await asyncio.sleep(self.settings.publisher_poll_seconds)
|
||||||
|
await delivery_id.nack(requeue=True)
|
||||||
|
|
||||||
|
async def reject_invalid(self, delivery_id: AbstractIncomingMessage) -> None:
|
||||||
|
# Failed jobs находятся в PostgreSQL; неизвестные payload не копируются в DLQ.
|
||||||
|
logging.getLogger(__name__).warning("Rejected invalid job envelope")
|
||||||
|
await delivery_id.reject(requeue=False)
|
||||||
|
|
||||||
|
async def aclose(self) -> None:
|
||||||
|
connection = self.connection
|
||||||
|
self.connection = self.channel = self.queue = self.iterator = None
|
||||||
|
if connection is not None and not connection.is_closed:
|
||||||
|
await connection.close()
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
from collections.abc import Sequence
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from sqlalchemy import exists, select, tuple_
|
||||||
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
|
from app.models.chat import Chat
|
||||||
|
from app.models.chat_member import ChatMember
|
||||||
|
from app.storage.types import Position
|
||||||
|
|
||||||
|
|
||||||
|
async def create_chat(
|
||||||
|
session: AsyncSession,
|
||||||
|
*,
|
||||||
|
title: str,
|
||||||
|
member_ids: Sequence[UUID],
|
||||||
|
) -> Chat:
|
||||||
|
"""Добавляет чат вместе с участниками."""
|
||||||
|
chat = Chat(
|
||||||
|
title=title,
|
||||||
|
members=[ChatMember(user_id=user_id) for user_id in member_ids],
|
||||||
|
)
|
||||||
|
session.add(chat)
|
||||||
|
await session.flush()
|
||||||
|
return chat
|
||||||
|
|
||||||
|
|
||||||
|
async def get_chat_for_member(
|
||||||
|
session: AsyncSession,
|
||||||
|
chat_id: UUID,
|
||||||
|
user_id: UUID,
|
||||||
|
) -> Chat | None:
|
||||||
|
"""Возвращает чат, если пользователь в нём состоит, иначе None."""
|
||||||
|
stmt = (
|
||||||
|
select(Chat)
|
||||||
|
.join(ChatMember, ChatMember.chat_id == Chat.id)
|
||||||
|
.where(Chat.id == chat_id, ChatMember.user_id == user_id)
|
||||||
|
)
|
||||||
|
result = await session.execute(stmt)
|
||||||
|
chat = result.scalar_one_or_none()
|
||||||
|
return chat
|
||||||
|
|
||||||
|
|
||||||
|
async def is_chat_member(session: AsyncSession, chat_id: UUID, user_id: UUID) -> bool:
|
||||||
|
"""Возвращает True, если пользователь состоит в чате; одна проверка по PK."""
|
||||||
|
stmt = select(
|
||||||
|
exists().where(ChatMember.chat_id == chat_id, ChatMember.user_id == user_id)
|
||||||
|
)
|
||||||
|
result = await session.execute(stmt)
|
||||||
|
is_member = bool(result.scalar_one())
|
||||||
|
return is_member
|
||||||
|
|
||||||
|
|
||||||
|
async def list_chats_for_user(
|
||||||
|
session: AsyncSession,
|
||||||
|
user_id: UUID,
|
||||||
|
after: Position | None,
|
||||||
|
limit: int,
|
||||||
|
) -> list[Chat]:
|
||||||
|
"""Возвращает чаты пользователя по (created_at, id) строго после позиции after."""
|
||||||
|
stmt = (
|
||||||
|
select(Chat)
|
||||||
|
.join(ChatMember, ChatMember.chat_id == Chat.id)
|
||||||
|
.where(ChatMember.user_id == user_id)
|
||||||
|
)
|
||||||
|
if after is not None:
|
||||||
|
stmt = stmt.where(tuple_(Chat.created_at, Chat.id) > tuple_(*after))
|
||||||
|
stmt = stmt.order_by(Chat.created_at, Chat.id).limit(limit)
|
||||||
|
result = await session.execute(stmt)
|
||||||
|
chats = list(result.scalars().all())
|
||||||
|
return chats
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
"""Простые проверки соединения с общими хранилищами."""
|
||||||
|
|
||||||
|
from redis.asyncio import Redis
|
||||||
|
from sqlalchemy import text
|
||||||
|
from sqlalchemy.ext.asyncio import AsyncEngine
|
||||||
|
|
||||||
|
|
||||||
|
async def check_database(engine: AsyncEngine) -> bool:
|
||||||
|
async with engine.connect() as connection:
|
||||||
|
await connection.execute(text("SELECT 1"))
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
async def check_redis(client: Redis) -> bool:
|
||||||
|
return bool(await client.ping())
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
"""Хранение и блокировка ключей в PostgreSQL."""
|
||||||
|
|
||||||
|
from datetime import timedelta
|
||||||
|
import hashlib
|
||||||
|
import json
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from sqlalchemy import BigInteger, func, literal, select, tuple_
|
||||||
|
from sqlalchemy.dialects.postgresql import insert
|
||||||
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
|
from app.models.idempotency_key import IdempotencyKey
|
||||||
|
|
||||||
|
Scope = tuple[UUID, str, str, str]
|
||||||
|
|
||||||
|
|
||||||
|
async def lock_key(session: AsyncSession, scope: Scope) -> None:
|
||||||
|
"""Сериализует один ключ до конца транзакции."""
|
||||||
|
raw = json.dumps([str(scope[0]), *scope[1:]]).encode()
|
||||||
|
lock_id = int.from_bytes(hashlib.sha256(raw).digest()[:8], signed=True)
|
||||||
|
await session.execute(
|
||||||
|
select(func.pg_advisory_xact_lock(literal(lock_id, type_=BigInteger)))
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def get_active_key(
|
||||||
|
session: AsyncSession, scope: Scope, ttl_seconds: int
|
||||||
|
) -> IdempotencyKey | None:
|
||||||
|
"""Читает результат, пока срок ключа не истёк."""
|
||||||
|
stmt = select(IdempotencyKey).where(
|
||||||
|
tuple_(
|
||||||
|
IdempotencyKey.user_id,
|
||||||
|
IdempotencyKey.method,
|
||||||
|
IdempotencyKey.path,
|
||||||
|
IdempotencyKey.key,
|
||||||
|
)
|
||||||
|
== tuple_(*scope),
|
||||||
|
IdempotencyKey.created_at
|
||||||
|
> func.clock_timestamp() - timedelta(seconds=ttl_seconds),
|
||||||
|
)
|
||||||
|
return await session.scalar(stmt)
|
||||||
|
|
||||||
|
|
||||||
|
async def save_result(
|
||||||
|
session: AsyncSession, scope: Scope, request_hash: str, message_id: UUID
|
||||||
|
) -> None:
|
||||||
|
"""Сохраняет новый результат или заменяет истёкший."""
|
||||||
|
stmt = insert(IdempotencyKey).values(
|
||||||
|
user_id=scope[0],
|
||||||
|
method=scope[1],
|
||||||
|
path=scope[2],
|
||||||
|
key=scope[3],
|
||||||
|
request_hash=request_hash,
|
||||||
|
message_id=message_id,
|
||||||
|
)
|
||||||
|
await session.execute(
|
||||||
|
stmt.on_conflict_do_update(
|
||||||
|
index_elements=[
|
||||||
|
IdempotencyKey.user_id,
|
||||||
|
IdempotencyKey.method,
|
||||||
|
IdempotencyKey.path,
|
||||||
|
IdempotencyKey.key,
|
||||||
|
],
|
||||||
|
set_={
|
||||||
|
"request_hash": request_hash,
|
||||||
|
"message_id": message_id,
|
||||||
|
"created_at": func.clock_timestamp(),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
)
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
"""Операторские выборки и журнал replay; транзакция принадлежит domain."""
|
||||||
|
|
||||||
|
from sqlalchemy import select
|
||||||
|
from sqlalchemy.dialects.postgresql import insert
|
||||||
|
|
||||||
|
from app.models.attachment import Attachment
|
||||||
|
from app.models.job import AttachmentJob
|
||||||
|
from app.models.job_replay import JobReplay
|
||||||
|
|
||||||
|
|
||||||
|
async def failed_jobs(session, limit: int):
|
||||||
|
result = await session.execute(
|
||||||
|
select(AttachmentJob, Attachment)
|
||||||
|
.join(Attachment, Attachment.id == AttachmentJob.attachment_id)
|
||||||
|
.where(Attachment.status == "failed")
|
||||||
|
.order_by(AttachmentJob.finished_at.desc(), AttachmentJob.id)
|
||||||
|
.limit(limit)
|
||||||
|
)
|
||||||
|
return result.all()
|
||||||
|
|
||||||
|
|
||||||
|
async def replay_record(session, request_id):
|
||||||
|
return await session.get(JobReplay, request_id)
|
||||||
|
|
||||||
|
|
||||||
|
async def insert_replay(session, **values) -> bool:
|
||||||
|
result = await session.scalar(
|
||||||
|
insert(JobReplay)
|
||||||
|
.values(**values)
|
||||||
|
.on_conflict_do_nothing(index_elements=[JobReplay.id])
|
||||||
|
.returning(JobReplay.id)
|
||||||
|
)
|
||||||
|
return result is not None
|
||||||
|
|
||||||
|
|
||||||
|
async def replay_history(session, job_id):
|
||||||
|
return (
|
||||||
|
await session.scalars(
|
||||||
|
select(JobReplay)
|
||||||
|
.where(JobReplay.job_id == job_id)
|
||||||
|
.order_by(JobReplay.created_at)
|
||||||
|
.limit(100)
|
||||||
|
)
|
||||||
|
).all()
|
||||||
|
|
||||||
|
|
||||||
|
def reset_failed(job, attachment, now, attempt_limit):
|
||||||
|
job.cycle_start_attempt = job.attempt
|
||||||
|
job.attempt_limit = attempt_limit
|
||||||
|
job.published_attempt = job.attempt
|
||||||
|
job.next_attempt_at = now
|
||||||
|
job.lease_token = None
|
||||||
|
job.lease_until = None
|
||||||
|
job.finished_at = None
|
||||||
|
job.last_error_code = None
|
||||||
|
job.last_error_permanent = None
|
||||||
|
attachment.status = "queued"
|
||||||
|
attachment.error_code = None
|
||||||
|
attachment.updated_at = now
|
||||||
@@ -0,0 +1,160 @@
|
|||||||
|
"""SQL и изменения jobs/attachments; commit остаётся в domain."""
|
||||||
|
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from sqlalchemy import and_, or_, func, select
|
||||||
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
|
from app.models.attachment import Attachment
|
||||||
|
from app.models.job import AttachmentJob
|
||||||
|
from app.schemas.attachment import VARIANT_FILENAMES
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class JobWork:
|
||||||
|
job_id: UUID
|
||||||
|
attachment_id: UUID
|
||||||
|
lease_token: UUID
|
||||||
|
pipeline_version: int
|
||||||
|
staging_key: str
|
||||||
|
content_type: str
|
||||||
|
size_bytes: int
|
||||||
|
sha256: str
|
||||||
|
|
||||||
|
@property
|
||||||
|
def original_key(self) -> str:
|
||||||
|
return f"originals/{self.attachment_id.hex}/v{self.pipeline_version}"
|
||||||
|
|
||||||
|
def variant_key(self, name: str) -> str:
|
||||||
|
filename = VARIANT_FILENAMES[name]
|
||||||
|
return f"variants/{self.attachment_id.hex}/v{self.pipeline_version}/{filename}"
|
||||||
|
|
||||||
|
|
||||||
|
async def create_job(
|
||||||
|
session: AsyncSession, attachment_id: UUID, staging_key: str
|
||||||
|
) -> UUID:
|
||||||
|
job = AttachmentJob(attachment_id=attachment_id, staging_key=staging_key)
|
||||||
|
session.add(job)
|
||||||
|
await session.flush()
|
||||||
|
return job.id
|
||||||
|
|
||||||
|
|
||||||
|
async def lock_job(session: AsyncSession, job_id: UUID):
|
||||||
|
result = await session.execute(
|
||||||
|
select(AttachmentJob, Attachment)
|
||||||
|
.join(Attachment, Attachment.id == AttachmentJob.attachment_id)
|
||||||
|
.where(AttachmentJob.id == job_id)
|
||||||
|
.with_for_update(of=AttachmentJob)
|
||||||
|
.execution_options(populate_existing=True)
|
||||||
|
)
|
||||||
|
return result.one_or_none()
|
||||||
|
|
||||||
|
|
||||||
|
async def database_time(session: AsyncSession) -> datetime:
|
||||||
|
return await session.scalar(select(func.clock_timestamp()))
|
||||||
|
|
||||||
|
|
||||||
|
async def expired_jobs(session: AsyncSession, limit: int = 20):
|
||||||
|
result = await session.execute(
|
||||||
|
select(AttachmentJob, Attachment)
|
||||||
|
.join(Attachment, Attachment.id == AttachmentJob.attachment_id)
|
||||||
|
.where(
|
||||||
|
Attachment.status == "processing",
|
||||||
|
AttachmentJob.lease_until < func.clock_timestamp(),
|
||||||
|
)
|
||||||
|
.order_by(AttachmentJob.lease_until)
|
||||||
|
.limit(limit)
|
||||||
|
.with_for_update(of=AttachmentJob, skip_locked=True)
|
||||||
|
)
|
||||||
|
return result.all()
|
||||||
|
|
||||||
|
|
||||||
|
def start_job(
|
||||||
|
job, attachment, token: UUID, now: datetime, lease_seconds: int
|
||||||
|
) -> JobWork:
|
||||||
|
job.attempt += 1
|
||||||
|
job.lease_token = token
|
||||||
|
job.lease_until = now + timedelta(seconds=lease_seconds)
|
||||||
|
attachment.status = "processing"
|
||||||
|
attachment.updated_at = now
|
||||||
|
return JobWork(
|
||||||
|
job.id,
|
||||||
|
attachment.id,
|
||||||
|
token,
|
||||||
|
job.pipeline_version,
|
||||||
|
job.staging_key,
|
||||||
|
attachment.content_type,
|
||||||
|
attachment.size_bytes,
|
||||||
|
attachment.sha256,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def record_failure(
|
||||||
|
job,
|
||||||
|
attachment,
|
||||||
|
now: datetime,
|
||||||
|
error_code: str,
|
||||||
|
*,
|
||||||
|
terminal: bool,
|
||||||
|
retry_seconds: float,
|
||||||
|
) -> None:
|
||||||
|
job.lease_token = None
|
||||||
|
job.lease_until = None
|
||||||
|
job.last_error_code = error_code
|
||||||
|
job.next_attempt_at = now + timedelta(seconds=retry_seconds)
|
||||||
|
job.finished_at = now if terminal else None
|
||||||
|
attachment.status = "failed" if terminal else "queued"
|
||||||
|
attachment.error_code = error_code if terminal else None
|
||||||
|
attachment.updated_at = now
|
||||||
|
|
||||||
|
|
||||||
|
def complete_job(
|
||||||
|
job, attachment, work: JobWork, variants: list[dict], now: datetime
|
||||||
|
) -> None:
|
||||||
|
attachment.object_key = work.original_key
|
||||||
|
attachment.thumbnail_key = work.variant_key("thumbnail")
|
||||||
|
attachment.crop_key = work.variant_key("crop")
|
||||||
|
attachment.optimized_key = work.variant_key("optimized")
|
||||||
|
attachment.variants = variants
|
||||||
|
attachment.status = "ready"
|
||||||
|
attachment.error_code = None
|
||||||
|
attachment.updated_at = now
|
||||||
|
job.lease_token = None
|
||||||
|
job.lease_until = None
|
||||||
|
job.last_error_code = None
|
||||||
|
job.last_error_permanent = None
|
||||||
|
job.finished_at = now
|
||||||
|
|
||||||
|
|
||||||
|
async def terminal_staging(
|
||||||
|
session: AsyncSession, retention: int, failed_retention: int, limit: int = 20
|
||||||
|
):
|
||||||
|
result = await session.execute(
|
||||||
|
select(AttachmentJob, Attachment)
|
||||||
|
.join(Attachment, Attachment.id == AttachmentJob.attachment_id)
|
||||||
|
.where(
|
||||||
|
AttachmentJob.staging_key.is_not(None),
|
||||||
|
or_(
|
||||||
|
and_(
|
||||||
|
Attachment.status == "ready",
|
||||||
|
AttachmentJob.finished_at
|
||||||
|
< func.clock_timestamp() - timedelta(seconds=retention),
|
||||||
|
),
|
||||||
|
and_(
|
||||||
|
Attachment.status == "failed",
|
||||||
|
AttachmentJob.finished_at
|
||||||
|
< func.clock_timestamp() - timedelta(seconds=failed_retention),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.order_by(AttachmentJob.finished_at)
|
||||||
|
.limit(limit)
|
||||||
|
.with_for_update(of=AttachmentJob, skip_locked=True)
|
||||||
|
)
|
||||||
|
return result.all()
|
||||||
|
|
||||||
|
|
||||||
|
def clear_staging(job) -> None:
|
||||||
|
job.staging_key = None
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from sqlalchemy import select, tuple_
|
||||||
|
from sqlalchemy.dialects.postgresql import insert
|
||||||
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
|
from app.models.counter import Counter
|
||||||
|
from app.models.message import Message
|
||||||
|
from app.storage.types import Position
|
||||||
|
|
||||||
|
MESSAGES_COUNTER = "messages"
|
||||||
|
|
||||||
|
|
||||||
|
async def get_message(session: AsyncSession, message_id: UUID) -> Message | None:
|
||||||
|
return await session.get(Message, message_id)
|
||||||
|
|
||||||
|
|
||||||
|
async def create_message(
|
||||||
|
session: AsyncSession,
|
||||||
|
*,
|
||||||
|
chat_id: UUID,
|
||||||
|
sender_id: UUID,
|
||||||
|
text: str,
|
||||||
|
) -> Message:
|
||||||
|
"""Добавляет сообщение; id и created_at назначает сервер."""
|
||||||
|
message = Message(chat_id=chat_id, sender_id=sender_id, text=text)
|
||||||
|
session.add(message)
|
||||||
|
await session.flush()
|
||||||
|
return message
|
||||||
|
|
||||||
|
|
||||||
|
async def list_messages(
|
||||||
|
session: AsyncSession,
|
||||||
|
chat_id: UUID,
|
||||||
|
after: Position | None,
|
||||||
|
limit: int,
|
||||||
|
) -> list[Message]:
|
||||||
|
"""Возвращает сообщения чата по (created_at, id) строго после позиции after."""
|
||||||
|
stmt = select(Message).where(Message.chat_id == chat_id)
|
||||||
|
if after is not None:
|
||||||
|
stmt = stmt.where(tuple_(Message.created_at, Message.id) > tuple_(*after))
|
||||||
|
stmt = stmt.order_by(Message.created_at, Message.id).limit(limit)
|
||||||
|
result = await session.execute(stmt)
|
||||||
|
messages = list(result.scalars().all())
|
||||||
|
return messages
|
||||||
|
|
||||||
|
|
||||||
|
async def reserve_message_slot(session: AsyncSession, max_messages: int) -> bool:
|
||||||
|
"""Атомарно увеличивает счётчик сообщений, если лимит не исчерпан."""
|
||||||
|
stmt = (
|
||||||
|
insert(Counter)
|
||||||
|
.values(name=MESSAGES_COUNTER, value=1)
|
||||||
|
.on_conflict_do_update(
|
||||||
|
index_elements=[Counter.name],
|
||||||
|
set_={"value": Counter.value + 1},
|
||||||
|
where=Counter.value < max_messages,
|
||||||
|
)
|
||||||
|
.returning(Counter.value)
|
||||||
|
)
|
||||||
|
result = await session.execute(stmt)
|
||||||
|
is_reserved = result.scalar_one_or_none() is not None
|
||||||
|
return is_reserved
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
"""Jobs одновременно хранят намерение публикации очередной попытки."""
|
||||||
|
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from sqlalchemy import func, select
|
||||||
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
|
from app.models.attachment import Attachment
|
||||||
|
from app.models.job import AttachmentJob
|
||||||
|
|
||||||
|
|
||||||
|
async def next_unpublished_job(
|
||||||
|
session: AsyncSession, *, attachment_id: UUID | None = None
|
||||||
|
) -> AttachmentJob | None:
|
||||||
|
statement = (
|
||||||
|
select(AttachmentJob)
|
||||||
|
.join(Attachment, Attachment.id == AttachmentJob.attachment_id)
|
||||||
|
.where(
|
||||||
|
Attachment.status == "queued",
|
||||||
|
AttachmentJob.next_attempt_at <= func.clock_timestamp(),
|
||||||
|
AttachmentJob.published_attempt <= AttachmentJob.attempt,
|
||||||
|
)
|
||||||
|
.order_by(AttachmentJob.next_attempt_at, AttachmentJob.id)
|
||||||
|
.limit(1)
|
||||||
|
.with_for_update(of=AttachmentJob, skip_locked=True)
|
||||||
|
)
|
||||||
|
if attachment_id is not None:
|
||||||
|
statement = statement.where(AttachmentJob.attachment_id == attachment_id)
|
||||||
|
return await session.scalar(statement)
|
||||||
|
|
||||||
|
|
||||||
|
def mark_published(job: AttachmentJob) -> None:
|
||||||
|
job.published_attempt = job.attempt + 1
|
||||||
@@ -0,0 +1,180 @@
|
|||||||
|
"""MinIO SDK выполняется в потоках; ресурсы живут до конца операции."""
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
from collections.abc import Callable
|
||||||
|
from contextlib import suppress
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from datetime import datetime
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import TypeVar
|
||||||
|
import time
|
||||||
|
from urllib.parse import urlsplit
|
||||||
|
|
||||||
|
from anyio import CancelScope
|
||||||
|
from minio import Minio
|
||||||
|
import urllib3
|
||||||
|
|
||||||
|
from app.core.s3 import S3Settings
|
||||||
|
|
||||||
|
T = TypeVar("T")
|
||||||
|
|
||||||
|
|
||||||
|
async def run_sync(
|
||||||
|
operation: Callable[..., T], *args, discard_result=None, **kwargs
|
||||||
|
) -> T:
|
||||||
|
"""При отмене дождаться потока: иначе PUT может догнать компенсацию удаления."""
|
||||||
|
task = asyncio.create_task(asyncio.to_thread(operation, *args, **kwargs))
|
||||||
|
try:
|
||||||
|
result = await asyncio.shield(task)
|
||||||
|
except asyncio.CancelledError:
|
||||||
|
with CancelScope(shield=True):
|
||||||
|
await settle_cancelled_call(task, discard_result)
|
||||||
|
raise
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
async def settle_cancelled_call(
|
||||||
|
task: asyncio.Task, discard_result: Callable | None
|
||||||
|
) -> None:
|
||||||
|
"""wait не переносит исключение SDK; повторная отмена не прерывает компенсацию."""
|
||||||
|
while not task.done():
|
||||||
|
with suppress(asyncio.CancelledError):
|
||||||
|
await asyncio.wait([task])
|
||||||
|
completed = not task.cancelled() and task.exception() is None
|
||||||
|
if completed and discard_result is not None:
|
||||||
|
discard_result(task.result())
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class ObjectInfo:
|
||||||
|
"""Поля S3 сохраняют необязательность SDK; согласованность проверяет domain."""
|
||||||
|
|
||||||
|
key: str
|
||||||
|
size_bytes: int | None
|
||||||
|
content_type: str | None
|
||||||
|
last_modified: datetime | None
|
||||||
|
|
||||||
|
|
||||||
|
def close_response(response) -> None:
|
||||||
|
response.close()
|
||||||
|
response.release_conn()
|
||||||
|
|
||||||
|
|
||||||
|
class ObjectStream:
|
||||||
|
"""Закрывается даже если StreamingResponse не успел начать итерацию."""
|
||||||
|
|
||||||
|
def __init__(self, storage, response):
|
||||||
|
self.storage = storage
|
||||||
|
self.response = response
|
||||||
|
self.deadline = time.monotonic() + storage.settings.download_timeout_seconds
|
||||||
|
self.closed = False
|
||||||
|
|
||||||
|
def __aiter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
async def __anext__(self):
|
||||||
|
if self.closed:
|
||||||
|
raise StopAsyncIteration
|
||||||
|
try:
|
||||||
|
remaining = self.deadline - time.monotonic()
|
||||||
|
if remaining <= 0:
|
||||||
|
raise TimeoutError
|
||||||
|
async with asyncio.timeout(remaining):
|
||||||
|
chunk = await self.storage._call(
|
||||||
|
lambda: self.response.read(self.storage.settings.chunk_bytes)
|
||||||
|
)
|
||||||
|
if not chunk:
|
||||||
|
await self.aclose()
|
||||||
|
raise StopAsyncIteration
|
||||||
|
except BaseException:
|
||||||
|
await self.aclose()
|
||||||
|
raise
|
||||||
|
return chunk
|
||||||
|
|
||||||
|
async def aclose(self):
|
||||||
|
if not self.closed:
|
||||||
|
self.closed = True
|
||||||
|
close_response(self.response)
|
||||||
|
|
||||||
|
|
||||||
|
class MinioStorage:
|
||||||
|
def __init__(self, settings: S3Settings) -> None:
|
||||||
|
self.settings = settings
|
||||||
|
# Клиент создаётся в event loop один раз, не конкурентно в worker threads.
|
||||||
|
url = urlsplit(settings.endpoint)
|
||||||
|
self._http = urllib3.PoolManager(
|
||||||
|
timeout=urllib3.Timeout(
|
||||||
|
connect=settings.connect_timeout_seconds,
|
||||||
|
read=settings.read_timeout_seconds,
|
||||||
|
),
|
||||||
|
retries=urllib3.Retry(total=0),
|
||||||
|
maxsize=32,
|
||||||
|
)
|
||||||
|
self._health_http = urllib3.PoolManager(
|
||||||
|
timeout=urllib3.Timeout(connect=0.25, read=0.5),
|
||||||
|
retries=urllib3.Retry(total=0),
|
||||||
|
)
|
||||||
|
self._client = Minio(
|
||||||
|
url.netloc,
|
||||||
|
secure=url.scheme == "https",
|
||||||
|
region=settings.region,
|
||||||
|
access_key=settings.access_key_id_file.read_text().strip(),
|
||||||
|
secret_key=settings.secret_access_key_file.read_text().strip(),
|
||||||
|
http_client=self._http,
|
||||||
|
)
|
||||||
|
|
||||||
|
async def _call(self, operation: Callable, *, discard_result=None):
|
||||||
|
return await run_sync(operation, discard_result=discard_result)
|
||||||
|
|
||||||
|
async def put_file(
|
||||||
|
self, key: str, path: Path, *, size_bytes: int, content_type: str, sha256: str
|
||||||
|
) -> None:
|
||||||
|
def put():
|
||||||
|
with path.open("rb") as source:
|
||||||
|
# Один PUT: размер части равен максимальному разрешённому файлу.
|
||||||
|
self._client.put_object(
|
||||||
|
self.settings.bucket,
|
||||||
|
key,
|
||||||
|
source,
|
||||||
|
size_bytes,
|
||||||
|
content_type=content_type,
|
||||||
|
metadata={"sha256": sha256},
|
||||||
|
part_size=10_485_760,
|
||||||
|
num_parallel_uploads=1,
|
||||||
|
)
|
||||||
|
|
||||||
|
await self._call(put)
|
||||||
|
|
||||||
|
async def open_object(self, key: str) -> ObjectStream:
|
||||||
|
response = await self._call(
|
||||||
|
lambda: self._client.get_object(self.settings.bucket, key),
|
||||||
|
discard_result=close_response,
|
||||||
|
)
|
||||||
|
return ObjectStream(self, response)
|
||||||
|
|
||||||
|
async def stat_object(self, key: str) -> ObjectInfo:
|
||||||
|
def stat():
|
||||||
|
info = self._client.stat_object(self.settings.bucket, key)
|
||||||
|
return ObjectInfo(key, info.size, info.content_type, info.last_modified)
|
||||||
|
|
||||||
|
return await self._call(stat)
|
||||||
|
|
||||||
|
async def delete_object(self, key: str) -> None:
|
||||||
|
await self._call(lambda: self._client.remove_object(self.settings.bucket, key))
|
||||||
|
|
||||||
|
async def check_available(self) -> bool:
|
||||||
|
status = await self._call(
|
||||||
|
lambda: self._health_http.request(
|
||||||
|
"GET",
|
||||||
|
self.settings.endpoint.rstrip("/") + "/minio/health/ready",
|
||||||
|
).status
|
||||||
|
)
|
||||||
|
return status == 200
|
||||||
|
|
||||||
|
async def aclose(self) -> None:
|
||||||
|
self._http.clear()
|
||||||
|
self._health_http.clear()
|
||||||
|
|
||||||
|
|
||||||
|
def create_s3_storage(settings: S3Settings) -> MinioStorage:
|
||||||
|
return MinioStorage(settings)
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
from datetime import datetime
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from redis.asyncio import Redis
|
||||||
|
|
||||||
|
SESSION_KEY_PREFIX = "session:"
|
||||||
|
|
||||||
|
|
||||||
|
def session_key(token_hash: str) -> str:
|
||||||
|
"""Возвращает ключ Redis для хеша токена."""
|
||||||
|
return f"{SESSION_KEY_PREFIX}{token_hash}"
|
||||||
|
|
||||||
|
|
||||||
|
async def create_session(
|
||||||
|
redis: Redis,
|
||||||
|
*,
|
||||||
|
token_hash: str,
|
||||||
|
user_id: UUID,
|
||||||
|
expires_at: datetime,
|
||||||
|
) -> None:
|
||||||
|
"""Добавляет сессию с абсолютным временем истечения."""
|
||||||
|
await redis.set(session_key(token_hash), str(user_id), pxat=expires_at)
|
||||||
|
|
||||||
|
|
||||||
|
async def get_session_user_id(redis: Redis, token_hash: str) -> UUID | None:
|
||||||
|
"""Возвращает id владельца действующей сессии или None."""
|
||||||
|
raw_user_id = await redis.get(session_key(token_hash))
|
||||||
|
if isinstance(raw_user_id, bytes):
|
||||||
|
raw_user_id = raw_user_id.decode()
|
||||||
|
user_id = UUID(raw_user_id) if raw_user_id else None
|
||||||
|
return user_id
|
||||||
|
|
||||||
|
|
||||||
|
async def delete_session(redis: Redis, token_hash: str) -> bool:
|
||||||
|
"""Удаляет сессию; возвращает True, если она существовала."""
|
||||||
|
deleted_count = await redis.delete(session_key(token_hash))
|
||||||
|
is_deleted = deleted_count > 0
|
||||||
|
return is_deleted
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
"""Общие типы слоя хранения."""
|
||||||
|
|
||||||
|
from datetime import datetime
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
Position = tuple[datetime, UUID]
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
from collections.abc import Collection
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from sqlalchemy import select
|
||||||
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
|
from app.models.user import User
|
||||||
|
|
||||||
|
|
||||||
|
async def create_user(
|
||||||
|
session: AsyncSession,
|
||||||
|
*,
|
||||||
|
username: str,
|
||||||
|
display_name: str,
|
||||||
|
password_hash: str,
|
||||||
|
) -> User:
|
||||||
|
"""Добавляет пользователя; занятый username даёт IntegrityError при flush."""
|
||||||
|
user = User(
|
||||||
|
username=username, display_name=display_name, password_hash=password_hash
|
||||||
|
)
|
||||||
|
session.add(user)
|
||||||
|
await session.flush()
|
||||||
|
return user
|
||||||
|
|
||||||
|
|
||||||
|
async def get_user_by_id(session: AsyncSession, user_id: UUID) -> User | None:
|
||||||
|
"""Возвращает пользователя по id или None."""
|
||||||
|
user = await session.get(User, user_id)
|
||||||
|
return user
|
||||||
|
|
||||||
|
|
||||||
|
async def get_user_by_username(session: AsyncSession, username: str) -> User | None:
|
||||||
|
"""Возвращает пользователя по username или None."""
|
||||||
|
stmt = select(User).where(User.username == username)
|
||||||
|
result = await session.execute(stmt)
|
||||||
|
user = result.scalar_one_or_none()
|
||||||
|
return user
|
||||||
|
|
||||||
|
|
||||||
|
async def get_existing_user_ids(
|
||||||
|
session: AsyncSession, user_ids: Collection[UUID]
|
||||||
|
) -> set[UUID]:
|
||||||
|
"""Возвращает те из user_ids, что существуют, одним запросом."""
|
||||||
|
stmt = select(User.id).where(User.id.in_(user_ids))
|
||||||
|
result = await session.execute(stmt)
|
||||||
|
existing_ids = set(result.scalars().all())
|
||||||
|
return existing_ids
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
# Override для отдельного тестового стенда (make test-stand-up):
|
||||||
|
# публикует PostgreSQL и Redis на 127.0.0.1, чтобы pytest из student-tests/
|
||||||
|
# мог подключаться к ним напрямую. В основном стенде эти порты закрыты.
|
||||||
|
services:
|
||||||
|
postgres:
|
||||||
|
ports:
|
||||||
|
- "127.0.0.1:${DB_HOST_PORT:?Set DB_HOST_PORT in .env}:5432"
|
||||||
|
redis:
|
||||||
|
ports:
|
||||||
|
- "127.0.0.1:${REDIS_HOST_PORT:?Set REDIS_HOST_PORT in .env}:6379"
|
||||||
+416
@@ -0,0 +1,416 @@
|
|||||||
|
name: messenger
|
||||||
|
|
||||||
|
x-api-image: &api-image
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
args:
|
||||||
|
API_BUILD_REVISION: ${API_BUILD_REVISION:?Set API_BUILD_REVISION in .env}
|
||||||
|
image: ${API_IMAGE:?Set API_IMAGE in .env}
|
||||||
|
|
||||||
|
x-base-env: &base-env
|
||||||
|
APP_HOST: ${APP_HOST:?Set APP_HOST in .env}
|
||||||
|
APP_PORT: ${APP_PORT:?Set APP_PORT in .env}
|
||||||
|
APP_ENV: ${APP_ENV:?Set APP_ENV in .env}
|
||||||
|
LOG_LEVEL: ${LOG_LEVEL:?Set LOG_LEVEL in .env}
|
||||||
|
MAX_MESSAGES: ${MAX_MESSAGES:?Set MAX_MESSAGES in .env}
|
||||||
|
MAX_IN_FLIGHT: ${MAX_IN_FLIGHT:?Set MAX_IN_FLIGHT in .env}
|
||||||
|
DB_HOST: ${DB_HOST:?Set DB_HOST in .env}
|
||||||
|
DB_PORT: ${DB_PORT:?Set DB_PORT in .env}
|
||||||
|
DB_NAME: ${DB_NAME:?Set DB_NAME in .env}
|
||||||
|
DB_USER: ${DB_USER:?Set DB_USER in .env}
|
||||||
|
DB_POOL_SIZE: ${DB_POOL_SIZE:?Set DB_POOL_SIZE in .env}
|
||||||
|
DB_MAX_OVERFLOW: ${DB_MAX_OVERFLOW:?Set DB_MAX_OVERFLOW in .env}
|
||||||
|
DB_POOL_TIMEOUT_SECONDS: ${DB_POOL_TIMEOUT_SECONDS:?Set DB_POOL_TIMEOUT_SECONDS in .env}
|
||||||
|
DB_CONNECT_TIMEOUT_SECONDS: ${DB_CONNECT_TIMEOUT_SECONDS:?Set DB_CONNECT_TIMEOUT_SECONDS in .env}
|
||||||
|
DB_STATEMENT_TIMEOUT_MS: ${DB_STATEMENT_TIMEOUT_MS:?Set DB_STATEMENT_TIMEOUT_MS in .env}
|
||||||
|
SHUTDOWN_DRAIN_SECONDS: ${SHUTDOWN_DRAIN_SECONDS:?Set SHUTDOWN_DRAIN_SECONDS in .env}
|
||||||
|
GRACEFUL_SHUTDOWN_TIMEOUT_SECONDS: ${GRACEFUL_SHUTDOWN_TIMEOUT_SECONDS:?Set GRACEFUL_SHUTDOWN_TIMEOUT_SECONDS in .env}
|
||||||
|
READINESS_TIMEOUT_SECONDS: ${READINESS_TIMEOUT_SECONDS:?Set READINESS_TIMEOUT_SECONDS in .env}
|
||||||
|
|
||||||
|
x-s3-env: &s3-env
|
||||||
|
S3_ENDPOINT: ${S3_ENDPOINT:?Set S3_ENDPOINT in .env}
|
||||||
|
S3_REGION: ${S3_REGION:?Set S3_REGION in .env}
|
||||||
|
S3_BUCKET: ${S3_BUCKET:?Set S3_BUCKET in .env}
|
||||||
|
S3_ACCESS_KEY_ID_FILE: ${S3_ACCESS_KEY_ID_FILE:?Set S3_ACCESS_KEY_ID_FILE in .env}
|
||||||
|
S3_SECRET_ACCESS_KEY_FILE: ${S3_SECRET_ACCESS_KEY_FILE:?Set S3_SECRET_ACCESS_KEY_FILE in .env}
|
||||||
|
S3_CONNECT_TIMEOUT_SECONDS: ${S3_CONNECT_TIMEOUT_SECONDS:?Set S3_CONNECT_TIMEOUT_SECONDS in .env}
|
||||||
|
S3_READ_TIMEOUT_SECONDS: ${S3_READ_TIMEOUT_SECONDS:?Set S3_READ_TIMEOUT_SECONDS in .env}
|
||||||
|
S3_UPLOAD_TIMEOUT_SECONDS: ${S3_UPLOAD_TIMEOUT_SECONDS:?Set S3_UPLOAD_TIMEOUT_SECONDS in .env}
|
||||||
|
S3_DOWNLOAD_TIMEOUT_SECONDS: ${S3_DOWNLOAD_TIMEOUT_SECONDS:?Set S3_DOWNLOAD_TIMEOUT_SECONDS in .env}
|
||||||
|
S3_IMAGE_TIMEOUT_SECONDS: ${S3_IMAGE_TIMEOUT_SECONDS:?Set S3_IMAGE_TIMEOUT_SECONDS in .env}
|
||||||
|
S3_MAX_UPLOAD_BYTES: ${S3_MAX_UPLOAD_BYTES:?Set S3_MAX_UPLOAD_BYTES in .env}
|
||||||
|
S3_MAX_IMAGE_PIXELS: ${S3_MAX_IMAGE_PIXELS:?Set S3_MAX_IMAGE_PIXELS in .env}
|
||||||
|
S3_CHUNK_BYTES: ${S3_CHUNK_BYTES:?Set S3_CHUNK_BYTES in .env}
|
||||||
|
S3_MAX_CONCURRENT_UPLOADS: ${S3_MAX_CONCURRENT_UPLOADS:?Set S3_MAX_CONCURRENT_UPLOADS in .env}
|
||||||
|
S3_CLEANUP_GRACE_SECONDS: ${S3_CLEANUP_GRACE_SECONDS:?Set S3_CLEANUP_GRACE_SECONDS in .env}
|
||||||
|
S3_CLEANUP_INTERVAL_SECONDS: ${S3_CLEANUP_INTERVAL_SECONDS:?Set S3_CLEANUP_INTERVAL_SECONDS in .env}
|
||||||
|
|
||||||
|
x-jobs-env: &jobs-env
|
||||||
|
BROKER_HOST: ${BROKER_HOST:?Set BROKER_HOST in .env}
|
||||||
|
BROKER_PORT: ${BROKER_PORT:?Set BROKER_PORT in .env}
|
||||||
|
BROKER_PASSWORD_FILE: ${BROKER_PASSWORD_FILE:?Set BROKER_PASSWORD_FILE in .env}
|
||||||
|
BROKER_QUEUE: ${BROKER_QUEUE:?Set BROKER_QUEUE in .env}
|
||||||
|
BROKER_USER: ${BROKER_USER:?Set BROKER_USER in .env}
|
||||||
|
BROKER_VHOST: ${BROKER_VHOST:?Set BROKER_VHOST in .env}
|
||||||
|
WORKER_CONCURRENCY: ${WORKER_CONCURRENCY:?Set WORKER_CONCURRENCY in .env}
|
||||||
|
JOB_MAX_ATTEMPTS: ${JOB_MAX_ATTEMPTS:?Set JOB_MAX_ATTEMPTS in .env}
|
||||||
|
JOB_TIMEOUT_SECONDS: ${JOB_TIMEOUT_SECONDS:?Set JOB_TIMEOUT_SECONDS in .env}
|
||||||
|
JOB_LEASE_SECONDS: ${JOB_LEASE_SECONDS:?Set JOB_LEASE_SECONDS in .env}
|
||||||
|
JOB_RETRY_MAX_SECONDS: ${JOB_RETRY_MAX_SECONDS:?Set JOB_RETRY_MAX_SECONDS in .env}
|
||||||
|
JOB_RETRY_SECONDS: ${JOB_RETRY_SECONDS:?Set JOB_RETRY_SECONDS in .env}
|
||||||
|
PUBLISHER_POLL_SECONDS: ${PUBLISHER_POLL_SECONDS:?Set PUBLISHER_POLL_SECONDS in .env}
|
||||||
|
FAILED_STAGING_RETENTION_SECONDS: ${FAILED_STAGING_RETENTION_SECONDS:?Set FAILED_STAGING_RETENTION_SECONDS in .env}
|
||||||
|
STAGING_RETENTION_SECONDS: ${STAGING_RETENTION_SECONDS:?Set STAGING_RETENTION_SECONDS in .env}
|
||||||
|
IMAGE_JPEG_QUALITY: ${IMAGE_JPEG_QUALITY:?Set IMAGE_JPEG_QUALITY in .env}
|
||||||
|
IMAGE_WEBP_QUALITY: ${IMAGE_WEBP_QUALITY:?Set IMAGE_WEBP_QUALITY in .env}
|
||||||
|
IMAGE_WEBP_METHOD: ${IMAGE_WEBP_METHOD:?Set IMAGE_WEBP_METHOD in .env}
|
||||||
|
IMAGE_MEMORY_MIB: ${IMAGE_MEMORY_MIB:?Set IMAGE_MEMORY_MIB in .env}
|
||||||
|
IMAGE_CPU_SECONDS: ${IMAGE_CPU_SECONDS:?Set IMAGE_CPU_SECONDS in .env}
|
||||||
|
|
||||||
|
services:
|
||||||
|
minio:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: infra/minio/Dockerfile
|
||||||
|
target: server
|
||||||
|
image: messenger-minio:local
|
||||||
|
command: ["server", "/data", "--console-address", "127.0.0.1:9001"]
|
||||||
|
restart: unless-stopped
|
||||||
|
environment:
|
||||||
|
MINIO_ROOT_USER_FILE: ${MINIO_ROOT_USER_FILE:?Set MINIO_ROOT_USER_FILE in .env}
|
||||||
|
MINIO_ROOT_PASSWORD_FILE: ${MINIO_ROOT_PASSWORD_FILE:?Set MINIO_ROOT_PASSWORD_FILE in .env}
|
||||||
|
MINIO_BROWSER: ${MINIO_BROWSER:?Set MINIO_BROWSER in .env}
|
||||||
|
secrets:
|
||||||
|
- source: minio_root_user
|
||||||
|
target: ${MINIO_ROOT_USER_FILE:?Set MINIO_ROOT_USER_FILE in .env}
|
||||||
|
- source: minio_root_password
|
||||||
|
target: ${MINIO_ROOT_PASSWORD_FILE:?Set MINIO_ROOT_PASSWORD_FILE in .env}
|
||||||
|
volumes:
|
||||||
|
- miniodata:/data
|
||||||
|
networks: [objects]
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://127.0.0.1:9000/minio/health/ready', timeout=2)"]
|
||||||
|
interval: 3s
|
||||||
|
timeout: 3s
|
||||||
|
retries: 10
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
cpus: "1.0"
|
||||||
|
memory: 512M
|
||||||
|
|
||||||
|
minio-bootstrap:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: infra/minio/Dockerfile
|
||||||
|
target: bootstrap
|
||||||
|
image: messenger-minio-bootstrap:local
|
||||||
|
environment:
|
||||||
|
S3_ENDPOINT: ${S3_ENDPOINT:?Set S3_ENDPOINT in .env}
|
||||||
|
S3_BUCKET: ${S3_BUCKET:?Set S3_BUCKET in .env}
|
||||||
|
S3_ACCESS_KEY_ID_FILE: ${S3_ACCESS_KEY_ID_FILE:?Set S3_ACCESS_KEY_ID_FILE in .env}
|
||||||
|
S3_SECRET_ACCESS_KEY_FILE: ${S3_SECRET_ACCESS_KEY_FILE:?Set S3_SECRET_ACCESS_KEY_FILE in .env}
|
||||||
|
MINIO_ROOT_USER_FILE: ${MINIO_ROOT_USER_FILE:?Set MINIO_ROOT_USER_FILE in .env}
|
||||||
|
MINIO_ROOT_PASSWORD_FILE: ${MINIO_ROOT_PASSWORD_FILE:?Set MINIO_ROOT_PASSWORD_FILE in .env}
|
||||||
|
secrets:
|
||||||
|
- source: minio_root_user
|
||||||
|
target: ${MINIO_ROOT_USER_FILE:?Set MINIO_ROOT_USER_FILE in .env}
|
||||||
|
- source: minio_root_password
|
||||||
|
target: ${MINIO_ROOT_PASSWORD_FILE:?Set MINIO_ROOT_PASSWORD_FILE in .env}
|
||||||
|
- source: s3_access_key_id
|
||||||
|
target: ${S3_ACCESS_KEY_ID_FILE:?Set S3_ACCESS_KEY_ID_FILE in .env}
|
||||||
|
- source: s3_secret_access_key
|
||||||
|
target: ${S3_SECRET_ACCESS_KEY_FILE:?Set S3_SECRET_ACCESS_KEY_FILE in .env}
|
||||||
|
networks: [objects]
|
||||||
|
restart: "no"
|
||||||
|
depends_on:
|
||||||
|
minio:
|
||||||
|
condition: service_healthy
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
cpus: "0.25"
|
||||||
|
memory: 128M
|
||||||
|
|
||||||
|
postgres:
|
||||||
|
image: postgres:16.10-bookworm
|
||||||
|
restart: unless-stopped
|
||||||
|
environment:
|
||||||
|
POSTGRES_USER: ${DB_USER:?Set DB_USER in .env}
|
||||||
|
POSTGRES_DB: ${DB_NAME:?Set DB_NAME in .env}
|
||||||
|
POSTGRES_PASSWORD_FILE: ${POSTGRES_PASSWORD_FILE:?Set POSTGRES_PASSWORD_FILE in .env}
|
||||||
|
secrets:
|
||||||
|
- source: db_password
|
||||||
|
target: ${POSTGRES_PASSWORD_FILE:?Set POSTGRES_PASSWORD_FILE in .env}
|
||||||
|
volumes:
|
||||||
|
- pgdata:/var/lib/postgresql/data
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"]
|
||||||
|
interval: 3s
|
||||||
|
timeout: 3s
|
||||||
|
retries: 10
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
cpus: "1.0"
|
||||||
|
memory: 512M
|
||||||
|
|
||||||
|
redis:
|
||||||
|
image: redis:7.4.2-bookworm
|
||||||
|
restart: unless-stopped
|
||||||
|
entrypoint: ["/bin/sh", "/usr/local/bin/start-redis.sh"]
|
||||||
|
secrets:
|
||||||
|
- redis_password
|
||||||
|
volumes:
|
||||||
|
- ./infra/redis/redis.conf:/usr/local/etc/redis/redis.conf:ro
|
||||||
|
- ./infra/redis/start-redis.sh:/usr/local/bin/start-redis.sh:ro
|
||||||
|
- redisdata:/data
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD-SHELL", "REDISCLI_AUTH=\"$$(cat /run/secrets/redis_password)\" redis-cli ping"]
|
||||||
|
interval: 3s
|
||||||
|
timeout: 3s
|
||||||
|
retries: 10
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
cpus: "0.5"
|
||||||
|
memory: 512M
|
||||||
|
|
||||||
|
broker:
|
||||||
|
image: rabbitmq:4.4.0
|
||||||
|
hostname: broker
|
||||||
|
restart: unless-stopped
|
||||||
|
entrypoint: ["/bin/sh", "/usr/local/bin/start-broker.sh"]
|
||||||
|
environment:
|
||||||
|
BROKER_PORT: ${BROKER_PORT:?Set BROKER_PORT in .env}
|
||||||
|
BROKER_USER: ${BROKER_USER:?Set BROKER_USER in .env}
|
||||||
|
BROKER_VHOST: ${BROKER_VHOST:?Set BROKER_VHOST in .env}
|
||||||
|
BROKER_PASSWORD_FILE: ${BROKER_PASSWORD_FILE:?Set BROKER_PASSWORD_FILE in .env}
|
||||||
|
secrets:
|
||||||
|
- source: broker_password
|
||||||
|
target: ${BROKER_PASSWORD_FILE:?Set BROKER_PASSWORD_FILE in .env}
|
||||||
|
volumes:
|
||||||
|
- ./infra/broker/start-broker.sh:/usr/local/bin/start-broker.sh:ro
|
||||||
|
- rabbitmqdata:/var/lib/rabbitmq
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "rabbitmq-diagnostics", "-q", "ping"]
|
||||||
|
interval: 5s
|
||||||
|
timeout: 5s
|
||||||
|
retries: 20
|
||||||
|
start_period: 20s
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
cpus: "1.0"
|
||||||
|
memory: 768M
|
||||||
|
|
||||||
|
publisher:
|
||||||
|
<<: *api-image
|
||||||
|
init: true
|
||||||
|
command: ["python", "-m", "app.core.outbox_publisher"]
|
||||||
|
restart: unless-stopped
|
||||||
|
environment:
|
||||||
|
<<: [*base-env, *jobs-env]
|
||||||
|
secrets:
|
||||||
|
- db_password
|
||||||
|
- source: broker_password
|
||||||
|
target: ${BROKER_PASSWORD_FILE:?Set BROKER_PASSWORD_FILE in .env}
|
||||||
|
depends_on:
|
||||||
|
postgres:
|
||||||
|
condition: service_healthy
|
||||||
|
broker:
|
||||||
|
condition: service_healthy
|
||||||
|
migrate:
|
||||||
|
condition: service_completed_successfully
|
||||||
|
healthcheck:
|
||||||
|
disable: true
|
||||||
|
stop_grace_period: 15s
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
cpus: "0.5"
|
||||||
|
memory: 256M
|
||||||
|
|
||||||
|
worker:
|
||||||
|
<<: *api-image
|
||||||
|
init: true
|
||||||
|
command: ["python", "-m", "app.core.job_worker"]
|
||||||
|
restart: unless-stopped
|
||||||
|
environment:
|
||||||
|
<<: [*base-env, *s3-env, *jobs-env]
|
||||||
|
secrets:
|
||||||
|
- db_password
|
||||||
|
- source: broker_password
|
||||||
|
target: ${BROKER_PASSWORD_FILE:?Set BROKER_PASSWORD_FILE in .env}
|
||||||
|
- source: s3_access_key_id
|
||||||
|
target: ${S3_ACCESS_KEY_ID_FILE:?Set S3_ACCESS_KEY_ID_FILE in .env}
|
||||||
|
- source: s3_secret_access_key
|
||||||
|
target: ${S3_SECRET_ACCESS_KEY_FILE:?Set S3_SECRET_ACCESS_KEY_FILE in .env}
|
||||||
|
networks: [default, objects]
|
||||||
|
tmpfs:
|
||||||
|
- /tmp:rw,noexec,nosuid,size=128m,mode=1777
|
||||||
|
depends_on:
|
||||||
|
postgres:
|
||||||
|
condition: service_healthy
|
||||||
|
broker:
|
||||||
|
condition: service_healthy
|
||||||
|
minio:
|
||||||
|
condition: service_healthy
|
||||||
|
minio-bootstrap:
|
||||||
|
condition: service_completed_successfully
|
||||||
|
migrate:
|
||||||
|
condition: service_completed_successfully
|
||||||
|
healthcheck:
|
||||||
|
disable: true
|
||||||
|
stop_signal: SIGTERM
|
||||||
|
stop_grace_period: 20s
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
cpus: "2.0"
|
||||||
|
memory: 2048M
|
||||||
|
|
||||||
|
migrate:
|
||||||
|
<<: *api-image
|
||||||
|
command: ["alembic", "upgrade", "head"]
|
||||||
|
environment: *base-env
|
||||||
|
secrets:
|
||||||
|
- db_password
|
||||||
|
depends_on:
|
||||||
|
postgres:
|
||||||
|
condition: service_healthy
|
||||||
|
restart: "no"
|
||||||
|
healthcheck:
|
||||||
|
disable: true
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
cpus: "0.5"
|
||||||
|
memory: 256M
|
||||||
|
|
||||||
|
api:
|
||||||
|
<<: *api-image
|
||||||
|
restart: unless-stopped
|
||||||
|
environment:
|
||||||
|
<<: [*base-env, *s3-env]
|
||||||
|
REDIS_HOST: ${REDIS_HOST:?Set REDIS_HOST in .env}
|
||||||
|
REDIS_PORT: ${REDIS_PORT:?Set REDIS_PORT in .env}
|
||||||
|
REDIS_DB: ${REDIS_DB:?Set REDIS_DB in .env}
|
||||||
|
REDIS_MAX_CONNECTIONS: ${REDIS_MAX_CONNECTIONS:?Set REDIS_MAX_CONNECTIONS in .env}
|
||||||
|
REDIS_POOL_TIMEOUT_SECONDS: ${REDIS_POOL_TIMEOUT_SECONDS:?Set REDIS_POOL_TIMEOUT_SECONDS in .env}
|
||||||
|
REDIS_CONNECT_TIMEOUT_SECONDS: ${REDIS_CONNECT_TIMEOUT_SECONDS:?Set REDIS_CONNECT_TIMEOUT_SECONDS in .env}
|
||||||
|
REDIS_SOCKET_TIMEOUT_SECONDS: ${REDIS_SOCKET_TIMEOUT_SECONDS:?Set REDIS_SOCKET_TIMEOUT_SECONDS in .env}
|
||||||
|
SESSION_STORE: ${SESSION_STORE:?Set SESSION_STORE in .env}
|
||||||
|
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:?Set SESSION_TTL_SECONDS in .env}
|
||||||
|
IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:?Set IDEMPOTENCY_TTL_SECONDS in .env}
|
||||||
|
secrets:
|
||||||
|
- db_password
|
||||||
|
- redis_password
|
||||||
|
- source: s3_access_key_id
|
||||||
|
target: ${S3_ACCESS_KEY_ID_FILE:?Set S3_ACCESS_KEY_ID_FILE in .env}
|
||||||
|
- source: s3_secret_access_key
|
||||||
|
target: ${S3_SECRET_ACCESS_KEY_FILE:?Set S3_SECRET_ACCESS_KEY_FILE in .env}
|
||||||
|
networks: [default, objects]
|
||||||
|
tmpfs:
|
||||||
|
- /tmp:rw,noexec,nosuid,size=64m,mode=1777
|
||||||
|
deploy:
|
||||||
|
replicas: ${API_REPLICAS:?Set API_REPLICAS in .env}
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
cpus: "1.0"
|
||||||
|
memory: 1536M
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "python", "-c", "import os, urllib.request; urllib.request.urlopen('http://127.0.0.1:' + os.environ['APP_PORT'] + '/health/ready', timeout=2.5)"]
|
||||||
|
interval: 3s
|
||||||
|
timeout: 3s
|
||||||
|
start_period: 10s
|
||||||
|
retries: 2
|
||||||
|
stop_signal: SIGTERM
|
||||||
|
stop_grace_period: 8s
|
||||||
|
depends_on:
|
||||||
|
minio:
|
||||||
|
condition: service_healthy
|
||||||
|
minio-bootstrap:
|
||||||
|
condition: service_completed_successfully
|
||||||
|
postgres:
|
||||||
|
condition: service_healthy
|
||||||
|
redis:
|
||||||
|
condition: service_healthy
|
||||||
|
migrate:
|
||||||
|
condition: service_completed_successfully
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
<<: *api-image
|
||||||
|
command: ["python", "-m", "app.core.cleanup", "--apply"]
|
||||||
|
restart: unless-stopped
|
||||||
|
environment:
|
||||||
|
<<: [*base-env, *s3-env]
|
||||||
|
FAILED_STAGING_RETENTION_SECONDS: ${FAILED_STAGING_RETENTION_SECONDS:?Set FAILED_STAGING_RETENTION_SECONDS in .env}
|
||||||
|
STAGING_RETENTION_SECONDS: ${STAGING_RETENTION_SECONDS:?Set STAGING_RETENTION_SECONDS in .env}
|
||||||
|
secrets:
|
||||||
|
- db_password
|
||||||
|
- source: s3_access_key_id
|
||||||
|
target: ${S3_ACCESS_KEY_ID_FILE:?Set S3_ACCESS_KEY_ID_FILE in .env}
|
||||||
|
- source: s3_secret_access_key
|
||||||
|
target: ${S3_SECRET_ACCESS_KEY_FILE:?Set S3_SECRET_ACCESS_KEY_FILE in .env}
|
||||||
|
networks: [default, objects]
|
||||||
|
healthcheck:
|
||||||
|
disable: true
|
||||||
|
depends_on:
|
||||||
|
migrate:
|
||||||
|
condition: service_completed_successfully
|
||||||
|
minio-bootstrap:
|
||||||
|
condition: service_completed_successfully
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
cpus: "0.25"
|
||||||
|
memory: 256M
|
||||||
|
|
||||||
|
nginx:
|
||||||
|
image: nginx:1.28.0-alpine
|
||||||
|
restart: unless-stopped
|
||||||
|
volumes:
|
||||||
|
- ./infra/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
|
||||||
|
ports:
|
||||||
|
- "127.0.0.1:${LB_HOST_PORT:?Set LB_HOST_PORT in .env}:8080"
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:8080/lb-health"]
|
||||||
|
interval: 5s
|
||||||
|
timeout: 3s
|
||||||
|
retries: 5
|
||||||
|
depends_on:
|
||||||
|
api:
|
||||||
|
condition: service_healthy
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
cpus: "0.5"
|
||||||
|
memory: 128M
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
pgdata:
|
||||||
|
redisdata:
|
||||||
|
miniodata:
|
||||||
|
rabbitmqdata:
|
||||||
|
|
||||||
|
networks:
|
||||||
|
default:
|
||||||
|
objects:
|
||||||
|
internal: true
|
||||||
|
|
||||||
|
secrets:
|
||||||
|
broker_password:
|
||||||
|
file: ./secrets/broker_password
|
||||||
|
db_password:
|
||||||
|
file: ./secrets/db_password
|
||||||
|
redis_password:
|
||||||
|
file: ./secrets/redis_password
|
||||||
|
minio_root_user:
|
||||||
|
file: ./secrets/minio_root_user
|
||||||
|
minio_root_password:
|
||||||
|
file: ./secrets/minio_root_password
|
||||||
|
s3_access_key_id:
|
||||||
|
file: ./secrets/s3_access_key_id
|
||||||
|
s3_secret_access_key:
|
||||||
|
file: ./secrets/s3_secret_access_key
|
||||||
@@ -0,0 +1,378 @@
|
|||||||
|
{
|
||||||
|
"status": "passed",
|
||||||
|
"results": [
|
||||||
|
{
|
||||||
|
"scenario": "worker_stopped_restart_all",
|
||||||
|
"attachment_id": "f5cc75ba-d2c7-43ff-8cf4-123b723118b8",
|
||||||
|
"queue_before": {
|
||||||
|
"name": "attachment-jobs",
|
||||||
|
"messages_unacknowledged": 0,
|
||||||
|
"consumers": 0,
|
||||||
|
"durable": true,
|
||||||
|
"messages_ready": 1
|
||||||
|
},
|
||||||
|
"queue_after": {
|
||||||
|
"name": "attachment-jobs",
|
||||||
|
"messages_unacknowledged": 0,
|
||||||
|
"consumers": 0,
|
||||||
|
"durable": true,
|
||||||
|
"messages_ready": 1
|
||||||
|
},
|
||||||
|
"after": {
|
||||||
|
"job_id": "9adcea8e-9046-4736-9763-c24bb6daf417",
|
||||||
|
"status": "ready",
|
||||||
|
"attempt": 1,
|
||||||
|
"published_attempt": 1,
|
||||||
|
"limit": 5,
|
||||||
|
"updated_at": "2026-11-10 04:36:45.543612+00:00",
|
||||||
|
"next_attempt_at": "2026-11-10 04:35:56.727228+00:00",
|
||||||
|
"last_error_code": null,
|
||||||
|
"permanent": null,
|
||||||
|
"objects": [
|
||||||
|
{
|
||||||
|
"name": "crop.jpg",
|
||||||
|
"size": 2462,
|
||||||
|
"sha256": "fae35500205a78065cf7d589f121cc909b017e2a4cde882177e24f5608fb4c1f",
|
||||||
|
"last_modified": "2026-11-10 04:36:45.525000+00:00"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "optimized.webp",
|
||||||
|
"size": 2462,
|
||||||
|
"sha256": "1090d84ed8411226010ae498b08e1b9e6d81c8eee02ee77143a0dc327b894beb",
|
||||||
|
"last_modified": "2026-11-10 04:36:45.533000+00:00"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "thumbnail.jpg",
|
||||||
|
"size": 5813,
|
||||||
|
"sha256": "b8e59b0cad429b1c49c27c4a1c79e52bfc23e2fab7c7238d18d5addd4df4b546",
|
||||||
|
"last_modified": "2026-11-10 04:36:45.521000+00:00"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "v1",
|
||||||
|
"size": 111817,
|
||||||
|
"sha256": "99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75",
|
||||||
|
"last_modified": "2026-11-10 04:36:45.508000+00:00"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"scenario": "crash-after-object",
|
||||||
|
"attachment_id": "1927c264-c721-4de1-8914-32d9beab48e5",
|
||||||
|
"exit_code": 137,
|
||||||
|
"before": {
|
||||||
|
"job_id": "bda1d00d-41c7-4d28-b2e5-07759be55985",
|
||||||
|
"status": "processing",
|
||||||
|
"attempt": 1,
|
||||||
|
"published_attempt": 1,
|
||||||
|
"limit": 5,
|
||||||
|
"updated_at": "2026-11-10 04:36:50.666631+00:00",
|
||||||
|
"next_attempt_at": "2026-11-10 04:36:48.673346+00:00",
|
||||||
|
"last_error_code": null,
|
||||||
|
"permanent": null,
|
||||||
|
"objects": [
|
||||||
|
{
|
||||||
|
"name": "crop.jpg",
|
||||||
|
"size": 2462,
|
||||||
|
"sha256": "fae35500205a78065cf7d589f121cc909b017e2a4cde882177e24f5608fb4c1f",
|
||||||
|
"last_modified": "2026-11-10 04:36:51.443000+00:00"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "optimized.webp",
|
||||||
|
"size": 2462,
|
||||||
|
"sha256": "1090d84ed8411226010ae498b08e1b9e6d81c8eee02ee77143a0dc327b894beb",
|
||||||
|
"last_modified": "2026-11-10 04:36:51.448000+00:00"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "thumbnail.jpg",
|
||||||
|
"size": 5813,
|
||||||
|
"sha256": "b8e59b0cad429b1c49c27c4a1c79e52bfc23e2fab7c7238d18d5addd4df4b546",
|
||||||
|
"last_modified": "2026-11-10 04:36:51.434000+00:00"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "v1",
|
||||||
|
"size": 111817,
|
||||||
|
"sha256": "99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75",
|
||||||
|
"last_modified": "2026-11-10 04:36:51.420000+00:00"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"after": {
|
||||||
|
"job_id": "bda1d00d-41c7-4d28-b2e5-07759be55985",
|
||||||
|
"status": "ready",
|
||||||
|
"attempt": 2,
|
||||||
|
"published_attempt": 2,
|
||||||
|
"limit": 5,
|
||||||
|
"updated_at": "2026-11-10 04:38:26.694238+00:00",
|
||||||
|
"next_attempt_at": "2026-11-10 04:38:25.019759+00:00",
|
||||||
|
"last_error_code": null,
|
||||||
|
"permanent": null,
|
||||||
|
"objects": [
|
||||||
|
{
|
||||||
|
"name": "crop.jpg",
|
||||||
|
"size": 2462,
|
||||||
|
"sha256": "fae35500205a78065cf7d589f121cc909b017e2a4cde882177e24f5608fb4c1f",
|
||||||
|
"last_modified": "2026-11-10 04:38:26.675000+00:00"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "optimized.webp",
|
||||||
|
"size": 2462,
|
||||||
|
"sha256": "1090d84ed8411226010ae498b08e1b9e6d81c8eee02ee77143a0dc327b894beb",
|
||||||
|
"last_modified": "2026-11-10 04:38:26.686000+00:00"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "thumbnail.jpg",
|
||||||
|
"size": 5813,
|
||||||
|
"sha256": "b8e59b0cad429b1c49c27c4a1c79e52bfc23e2fab7c7238d18d5addd4df4b546",
|
||||||
|
"last_modified": "2026-11-10 04:38:26.666000+00:00"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "v1",
|
||||||
|
"size": 111817,
|
||||||
|
"sha256": "99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75",
|
||||||
|
"last_modified": "2026-11-10 04:38:26.656000+00:00"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"scenario": "crash-before-ack",
|
||||||
|
"attachment_id": "e02d817d-9615-4058-ae5c-63ab895aa6f5",
|
||||||
|
"exit_code": 137,
|
||||||
|
"before": {
|
||||||
|
"job_id": "9bfea78c-ce05-4a00-bf76-a2213ed3a8df",
|
||||||
|
"status": "ready",
|
||||||
|
"attempt": 1,
|
||||||
|
"published_attempt": 1,
|
||||||
|
"limit": 5,
|
||||||
|
"updated_at": "2026-11-10 04:38:32.063984+00:00",
|
||||||
|
"next_attempt_at": "2026-11-10 04:38:29.436074+00:00",
|
||||||
|
"last_error_code": null,
|
||||||
|
"permanent": null,
|
||||||
|
"objects": [
|
||||||
|
{
|
||||||
|
"name": "crop.jpg",
|
||||||
|
"size": 2462,
|
||||||
|
"sha256": "fae35500205a78065cf7d589f121cc909b017e2a4cde882177e24f5608fb4c1f",
|
||||||
|
"last_modified": "2026-11-10 04:38:32.054000+00:00"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "optimized.webp",
|
||||||
|
"size": 2462,
|
||||||
|
"sha256": "1090d84ed8411226010ae498b08e1b9e6d81c8eee02ee77143a0dc327b894beb",
|
||||||
|
"last_modified": "2026-11-10 04:38:32.059000+00:00"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "thumbnail.jpg",
|
||||||
|
"size": 5813,
|
||||||
|
"sha256": "b8e59b0cad429b1c49c27c4a1c79e52bfc23e2fab7c7238d18d5addd4df4b546",
|
||||||
|
"last_modified": "2026-11-10 04:38:32.048000+00:00"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "v1",
|
||||||
|
"size": 111817,
|
||||||
|
"sha256": "99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75",
|
||||||
|
"last_modified": "2026-11-10 04:38:32.032000+00:00"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"after": {
|
||||||
|
"job_id": "9bfea78c-ce05-4a00-bf76-a2213ed3a8df",
|
||||||
|
"status": "ready",
|
||||||
|
"attempt": 1,
|
||||||
|
"published_attempt": 1,
|
||||||
|
"limit": 5,
|
||||||
|
"updated_at": "2026-11-10 04:38:32.063984+00:00",
|
||||||
|
"next_attempt_at": "2026-11-10 04:38:29.436074+00:00",
|
||||||
|
"last_error_code": null,
|
||||||
|
"permanent": null,
|
||||||
|
"objects": [
|
||||||
|
{
|
||||||
|
"name": "crop.jpg",
|
||||||
|
"size": 2462,
|
||||||
|
"sha256": "fae35500205a78065cf7d589f121cc909b017e2a4cde882177e24f5608fb4c1f",
|
||||||
|
"last_modified": "2026-11-10 04:38:32.054000+00:00"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "optimized.webp",
|
||||||
|
"size": 2462,
|
||||||
|
"sha256": "1090d84ed8411226010ae498b08e1b9e6d81c8eee02ee77143a0dc327b894beb",
|
||||||
|
"last_modified": "2026-11-10 04:38:32.059000+00:00"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "thumbnail.jpg",
|
||||||
|
"size": 5813,
|
||||||
|
"sha256": "b8e59b0cad429b1c49c27c4a1c79e52bfc23e2fab7c7238d18d5addd4df4b546",
|
||||||
|
"last_modified": "2026-11-10 04:38:32.048000+00:00"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "v1",
|
||||||
|
"size": 111817,
|
||||||
|
"sha256": "99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75",
|
||||||
|
"last_modified": "2026-11-10 04:38:32.032000+00:00"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"scenario": "fail-transient",
|
||||||
|
"attachment_id": "a9c15ce8-cacf-43c0-8373-8e3ff5fa23aa",
|
||||||
|
"samples": [
|
||||||
|
{
|
||||||
|
"job_id": "ffa9abca-645a-4ee2-ab2a-7bd06a7e0a59",
|
||||||
|
"status": "queued",
|
||||||
|
"attempt": 1,
|
||||||
|
"published_attempt": 1,
|
||||||
|
"limit": 5,
|
||||||
|
"updated_at": "2026-11-10 04:38:46.283315+00:00",
|
||||||
|
"next_attempt_at": "2026-11-10 04:38:50.994899+00:00",
|
||||||
|
"last_error_code": "processing_timeout",
|
||||||
|
"permanent": false,
|
||||||
|
"objects": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"job_id": "ffa9abca-645a-4ee2-ab2a-7bd06a7e0a59",
|
||||||
|
"status": "queued",
|
||||||
|
"attempt": 2,
|
||||||
|
"published_attempt": 2,
|
||||||
|
"limit": 5,
|
||||||
|
"updated_at": "2026-11-10 04:38:51.039278+00:00",
|
||||||
|
"next_attempt_at": "2026-11-10 04:38:59.940067+00:00",
|
||||||
|
"last_error_code": "processing_timeout",
|
||||||
|
"permanent": false,
|
||||||
|
"objects": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"job_id": "ffa9abca-645a-4ee2-ab2a-7bd06a7e0a59",
|
||||||
|
"status": "queued",
|
||||||
|
"attempt": 3,
|
||||||
|
"published_attempt": 3,
|
||||||
|
"limit": 5,
|
||||||
|
"updated_at": "2026-11-10 04:39:00.122276+00:00",
|
||||||
|
"next_attempt_at": "2026-11-10 04:39:19.754304+00:00",
|
||||||
|
"last_error_code": "processing_timeout",
|
||||||
|
"permanent": false,
|
||||||
|
"objects": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"job_id": "ffa9abca-645a-4ee2-ab2a-7bd06a7e0a59",
|
||||||
|
"status": "queued",
|
||||||
|
"attempt": 4,
|
||||||
|
"published_attempt": 4,
|
||||||
|
"limit": 5,
|
||||||
|
"updated_at": "2026-11-10 04:39:20.250087+00:00",
|
||||||
|
"next_attempt_at": "2026-11-10 04:39:56.121121+00:00",
|
||||||
|
"last_error_code": "processing_timeout",
|
||||||
|
"permanent": false,
|
||||||
|
"objects": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"job_id": "ffa9abca-645a-4ee2-ab2a-7bd06a7e0a59",
|
||||||
|
"status": "failed",
|
||||||
|
"attempt": 5,
|
||||||
|
"published_attempt": 5,
|
||||||
|
"limit": 5,
|
||||||
|
"updated_at": "2026-11-10 04:39:56.526646+00:00",
|
||||||
|
"next_attempt_at": "2026-11-10 04:41:15.809038+00:00",
|
||||||
|
"last_error_code": "processing_timeout",
|
||||||
|
"permanent": false,
|
||||||
|
"objects": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"after": {
|
||||||
|
"job_id": "ffa9abca-645a-4ee2-ab2a-7bd06a7e0a59",
|
||||||
|
"status": "ready",
|
||||||
|
"attempt": 6,
|
||||||
|
"published_attempt": 6,
|
||||||
|
"limit": 10,
|
||||||
|
"updated_at": "2026-11-10 04:40:09.651133+00:00",
|
||||||
|
"next_attempt_at": "2026-11-10 04:40:03.031585+00:00",
|
||||||
|
"last_error_code": null,
|
||||||
|
"permanent": null,
|
||||||
|
"objects": [
|
||||||
|
{
|
||||||
|
"name": "crop.jpg",
|
||||||
|
"size": 2462,
|
||||||
|
"sha256": "fae35500205a78065cf7d589f121cc909b017e2a4cde882177e24f5608fb4c1f",
|
||||||
|
"last_modified": "2026-11-10 04:40:09.642000+00:00"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "optimized.webp",
|
||||||
|
"size": 2462,
|
||||||
|
"sha256": "1090d84ed8411226010ae498b08e1b9e6d81c8eee02ee77143a0dc327b894beb",
|
||||||
|
"last_modified": "2026-11-10 04:40:09.647000+00:00"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "thumbnail.jpg",
|
||||||
|
"size": 5813,
|
||||||
|
"sha256": "b8e59b0cad429b1c49c27c4a1c79e52bfc23e2fab7c7238d18d5addd4df4b546",
|
||||||
|
"last_modified": "2026-11-10 04:40:09.637000+00:00"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "v1",
|
||||||
|
"size": 111817,
|
||||||
|
"sha256": "99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75",
|
||||||
|
"last_modified": "2026-11-10 04:40:09.624000+00:00"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"replay_idempotent": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"scenario": "fail-permanent",
|
||||||
|
"attachment_id": "d8a6e312-e468-4e4b-a7ee-1d84751d5843",
|
||||||
|
"samples": [
|
||||||
|
{
|
||||||
|
"job_id": "0da2ef52-65a8-4a74-8d43-015666e2975c",
|
||||||
|
"status": "failed",
|
||||||
|
"attempt": 1,
|
||||||
|
"published_attempt": 1,
|
||||||
|
"limit": 5,
|
||||||
|
"updated_at": "2026-11-10 04:40:24.826416+00:00",
|
||||||
|
"next_attempt_at": "2026-11-10 04:40:29.630307+00:00",
|
||||||
|
"last_error_code": "injected_permanent",
|
||||||
|
"permanent": true,
|
||||||
|
"objects": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"after": {
|
||||||
|
"job_id": "0da2ef52-65a8-4a74-8d43-015666e2975c",
|
||||||
|
"status": "ready",
|
||||||
|
"attempt": 2,
|
||||||
|
"published_attempt": 2,
|
||||||
|
"limit": 6,
|
||||||
|
"updated_at": "2026-11-10 04:40:40.004814+00:00",
|
||||||
|
"next_attempt_at": "2026-11-10 04:40:30.084006+00:00",
|
||||||
|
"last_error_code": null,
|
||||||
|
"permanent": null,
|
||||||
|
"objects": [
|
||||||
|
{
|
||||||
|
"name": "crop.jpg",
|
||||||
|
"size": 2462,
|
||||||
|
"sha256": "fae35500205a78065cf7d589f121cc909b017e2a4cde882177e24f5608fb4c1f",
|
||||||
|
"last_modified": "2026-11-10 04:40:39.988000+00:00"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "optimized.webp",
|
||||||
|
"size": 2462,
|
||||||
|
"sha256": "1090d84ed8411226010ae498b08e1b9e6d81c8eee02ee77143a0dc327b894beb",
|
||||||
|
"last_modified": "2026-11-10 04:40:39.996000+00:00"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "thumbnail.jpg",
|
||||||
|
"size": 5813,
|
||||||
|
"sha256": "b8e59b0cad429b1c49c27c4a1c79e52bfc23e2fab7c7238d18d5addd4df4b546",
|
||||||
|
"last_modified": "2026-11-10 04:40:39.982000+00:00"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "v1",
|
||||||
|
"size": 111817,
|
||||||
|
"sha256": "99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75",
|
||||||
|
"last_modified": "2026-11-10 04:40:39.968000+00:00"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"replay_idempotent": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
python3 scripts/check_contract.py
|
||||||
|
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
python3 tests/smoke.py --base-url "http://localhost:28080"
|
||||||
|
PASS: lab5 public API scenario; infrastructure and grading evidence remain separate.
|
||||||
@@ -0,0 +1,308 @@
|
|||||||
|
{
|
||||||
|
"date": "2026-11-10",
|
||||||
|
"status": "passed",
|
||||||
|
"project": "mtusi-lab5-verification",
|
||||||
|
"base_url": "http://localhost:28080",
|
||||||
|
"checks": [
|
||||||
|
{
|
||||||
|
"name": "pytest_regression",
|
||||||
|
"exit_code": 0,
|
||||||
|
"stdout": "199 passed in 82.29s (0:01:22)",
|
||||||
|
"scope": "Dedicated *_test database and Redis DB15; Docker restart tests enabled"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "pytest_timeout_retention",
|
||||||
|
"exit_code": 0,
|
||||||
|
"stdout": "2 passed, 5 deselected in 1.91s",
|
||||||
|
"scope": "Dedicated *_test database and Redis DB15; Docker restart tests enabled"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "compose_configuration",
|
||||||
|
"command": [
|
||||||
|
"docker",
|
||||||
|
"compose",
|
||||||
|
"--env-file",
|
||||||
|
".env",
|
||||||
|
"config",
|
||||||
|
"--quiet"
|
||||||
|
],
|
||||||
|
"exit_code": 0,
|
||||||
|
"stdout": ""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "contract",
|
||||||
|
"command": [
|
||||||
|
"/home/gytall/.pyenv/versions/3.12.14/bin/python3",
|
||||||
|
"scripts/check_contract.py"
|
||||||
|
],
|
||||||
|
"exit_code": 0,
|
||||||
|
"stdout": "PASS: offline skeleton structure, local refs and Python syntax; application NOT tested."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "openapi",
|
||||||
|
"command": [
|
||||||
|
"/home/gytall/.pyenv/versions/3.12.14/bin/python3",
|
||||||
|
"scripts/validate_openapi.py"
|
||||||
|
],
|
||||||
|
"exit_code": 0,
|
||||||
|
"stdout": "PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "public_smoke",
|
||||||
|
"command": [
|
||||||
|
"/home/gytall/.pyenv/versions/3.12.14/bin/python3",
|
||||||
|
"tests/smoke.py",
|
||||||
|
"--base-url",
|
||||||
|
"http://localhost:28080"
|
||||||
|
],
|
||||||
|
"exit_code": 0,
|
||||||
|
"stdout": "PASS: lab5 public API scenario; infrastructure and grading evidence remain separate."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "message_idempotency",
|
||||||
|
"command": [
|
||||||
|
"/home/gytall/.pyenv/versions/3.12.14/bin/python3",
|
||||||
|
"-m",
|
||||||
|
"scripts.check_idempotency",
|
||||||
|
"--base-url",
|
||||||
|
"http://localhost:28080"
|
||||||
|
],
|
||||||
|
"exit_code": 0,
|
||||||
|
"stdout": "PASS: 24 concurrent replays, 2 replicas, one message"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "outbox_crash",
|
||||||
|
"command": [
|
||||||
|
"/home/gytall/.pyenv/versions/3.12.14/bin/python3",
|
||||||
|
"-m",
|
||||||
|
"scripts.check_outbox_recovery",
|
||||||
|
"--base-url",
|
||||||
|
"http://localhost:28080"
|
||||||
|
],
|
||||||
|
"exit_code": 0,
|
||||||
|
"stdout": "{\"scenario\": \"commit_before_publish\", \"attachment_id\": \"0fff1dce-c479-40d8-8627-f28cc85cb6cf\", \"message_id\": \"e79bfb7e-c2af-4011-a87a-52528d7a7840\", \"publisher_exit_code\": 137, \"status_after_crash\": \"queued\", \"status_after_restart\": \"ready\", \"variants\": [\"thumbnail\", \"crop\", \"optimized\"], \"reuploaded\": false}"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "public_persistence",
|
||||||
|
"command": [
|
||||||
|
"/home/gytall/.pyenv/versions/3.12.14/bin/python3",
|
||||||
|
"tests/resilience.py",
|
||||||
|
"--mode",
|
||||||
|
"persistence",
|
||||||
|
"--action-script",
|
||||||
|
"scripts/restart.sh",
|
||||||
|
"--base-url",
|
||||||
|
"http://localhost:28080"
|
||||||
|
],
|
||||||
|
"exit_code": 0,
|
||||||
|
"stdout": "Executing explicit persistence hook; hook output suppressed to avoid leaking secrets.\nPASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "public_failover",
|
||||||
|
"command": [
|
||||||
|
"/home/gytall/.pyenv/versions/3.12.14/bin/python3",
|
||||||
|
"tests/resilience.py",
|
||||||
|
"--mode",
|
||||||
|
"failover",
|
||||||
|
"--action-script",
|
||||||
|
"scripts/stop-one.sh",
|
||||||
|
"--base-url",
|
||||||
|
"http://localhost:28080"
|
||||||
|
],
|
||||||
|
"exit_code": 0,
|
||||||
|
"stdout": "Executing explicit failover hook; hook output suppressed to avoid leaking secrets.\n{\"observed_instances_before\": 2, \"successful_writes\": 104, \"failed_attempts\": 5, \"first_recovery_seconds\": 1.907}\nPASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "rabbitmq_prefetch",
|
||||||
|
"command": [
|
||||||
|
"docker",
|
||||||
|
"compose",
|
||||||
|
"--env-file",
|
||||||
|
".env",
|
||||||
|
"exec",
|
||||||
|
"-T",
|
||||||
|
"broker",
|
||||||
|
"rabbitmqctl",
|
||||||
|
"list_channels",
|
||||||
|
"--formatter=json",
|
||||||
|
"consumer_count",
|
||||||
|
"prefetch_count",
|
||||||
|
"messages_unacknowledged"
|
||||||
|
],
|
||||||
|
"exit_code": 0,
|
||||||
|
"stdout": "[\n{\"consumer_count\":1,\"messages_unacknowledged\":0,\"prefetch_count\":1}\n,{\"consumer_count\":0,\"messages_unacknowledged\":0,\"prefetch_count\":1}\n,{\"consumer_count\":1,\"messages_unacknowledged\":0,\"prefetch_count\":1}\n]"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "rabbitmq_durability_ack",
|
||||||
|
"command": [
|
||||||
|
"docker",
|
||||||
|
"compose",
|
||||||
|
"--env-file",
|
||||||
|
".env",
|
||||||
|
"exec",
|
||||||
|
"-T",
|
||||||
|
"broker",
|
||||||
|
"rabbitmqctl",
|
||||||
|
"list_queues",
|
||||||
|
"--formatter=json",
|
||||||
|
"name",
|
||||||
|
"durable",
|
||||||
|
"messages_ready",
|
||||||
|
"messages_unacknowledged",
|
||||||
|
"consumers"
|
||||||
|
],
|
||||||
|
"exit_code": 0,
|
||||||
|
"stdout": "[\n{\"name\":\"attachment-jobs\",\"messages_unacknowledged\":0,\"consumers\":2,\"durable\":true,\"messages_ready\":0}\n]"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "worker_fault_retry_replay",
|
||||||
|
"exit_code": 0,
|
||||||
|
"evidence": "jobs.json",
|
||||||
|
"scenarios": [
|
||||||
|
"worker_stopped_restart_all",
|
||||||
|
"crash-after-object",
|
||||||
|
"crash-before-ack",
|
||||||
|
"fail-transient",
|
||||||
|
"fail-permanent"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "whitespace",
|
||||||
|
"command": [
|
||||||
|
"git",
|
||||||
|
"diff",
|
||||||
|
"--check"
|
||||||
|
],
|
||||||
|
"exit_code": 0,
|
||||||
|
"stdout": ""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "public_contract_tests_unchanged",
|
||||||
|
"exit_code": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "final_services",
|
||||||
|
"command": [
|
||||||
|
"docker",
|
||||||
|
"compose",
|
||||||
|
"--env-file",
|
||||||
|
".env",
|
||||||
|
"ps",
|
||||||
|
"--format",
|
||||||
|
"{{.Service}} {{.State}} {{.Health}}"
|
||||||
|
],
|
||||||
|
"exit_code": 0,
|
||||||
|
"stdout": "api running healthy\napi running healthy\nbroker running healthy\ncleanup running \nminio running healthy\nnginx running healthy\npostgres running healthy\npublisher running \nredis running healthy\nworker running"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"git_base_commit": "0a703588290e26d0f148b45d0e6bee2e8eefce7d",
|
||||||
|
"uncommitted_changes": true,
|
||||||
|
"environment": {
|
||||||
|
"test_runner_python": "3.12.14",
|
||||||
|
"runtime_python": "3.12.15",
|
||||||
|
"broker": "RabbitMQ 4.4.0",
|
||||||
|
"sessions": "Redis 7.4.2",
|
||||||
|
"worker_memory_bytes": 2147483648,
|
||||||
|
"worker_cpu_limit": 2.0,
|
||||||
|
"worker_init": true
|
||||||
|
},
|
||||||
|
"profile": {
|
||||||
|
"data": "Synthetic users/chats only; tests/fixtures/sample.png for fault scenarios; no credentials in evidence",
|
||||||
|
"worker_concurrency": 2,
|
||||||
|
"prefetch_per_consumer": 1,
|
||||||
|
"max_attempts_per_cycle": 5,
|
||||||
|
"timeout_seconds": 60,
|
||||||
|
"lease_seconds": 90,
|
||||||
|
"retry_base_seconds": 5,
|
||||||
|
"retry_cap_seconds": 120,
|
||||||
|
"image_memory_mib": 768,
|
||||||
|
"image_cpu_seconds": 20,
|
||||||
|
"isolated_limit_tests": "256 MiB / 1 CPU second in child process; timeout cancellation checked separately"
|
||||||
|
},
|
||||||
|
"criteria": {
|
||||||
|
"3.1_lab4_and_durable_202": [
|
||||||
|
"pytest_regression",
|
||||||
|
"public_smoke",
|
||||||
|
"public_persistence",
|
||||||
|
"worker_fault_retry_replay"
|
||||||
|
],
|
||||||
|
"3.2_durable_broker_worker_late_ack": [
|
||||||
|
"rabbitmq_durability_ack",
|
||||||
|
"rabbitmq_prefetch",
|
||||||
|
"worker_fault_retry_replay"
|
||||||
|
],
|
||||||
|
"3.3_states_original_thumbnail": [
|
||||||
|
"public_smoke",
|
||||||
|
"pytest_regression",
|
||||||
|
"worker_fault_retry_replay"
|
||||||
|
],
|
||||||
|
"3.4_bounded_retry_idempotency": [
|
||||||
|
"pytest_regression",
|
||||||
|
"worker_fault_retry_replay"
|
||||||
|
],
|
||||||
|
"3.5_stopped_worker_api_broker_restart": [
|
||||||
|
"worker_fault_retry_replay",
|
||||||
|
"public_persistence"
|
||||||
|
],
|
||||||
|
"4.1_crop_webp_and_worker_limits": [
|
||||||
|
"pytest_regression",
|
||||||
|
"worker_fault_retry_replay"
|
||||||
|
],
|
||||||
|
"4.2_commit_publish_recovery": [
|
||||||
|
"outbox_crash"
|
||||||
|
],
|
||||||
|
"4.3_backoff_jitter_classification_registry_replay": [
|
||||||
|
"pytest_regression",
|
||||||
|
"pytest_timeout_retention",
|
||||||
|
"worker_fault_retry_replay"
|
||||||
|
],
|
||||||
|
"4.4_crash_after_put_lease_prefetch_cleanup": [
|
||||||
|
"worker_fault_retry_replay",
|
||||||
|
"rabbitmq_prefetch",
|
||||||
|
"pytest_regression",
|
||||||
|
"pytest_timeout_retention"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"limitations": [
|
||||||
|
"Single Docker host, no disk-loss or cluster failover claim",
|
||||||
|
"Grade 5 experiments not performed",
|
||||||
|
"No production-scale performance/security claim"
|
||||||
|
],
|
||||||
|
"post_test_cleanup": {
|
||||||
|
"date": "2026-11-10",
|
||||||
|
"project_removed": "mtusi-lab5-verification",
|
||||||
|
"test_volumes_removed": 4,
|
||||||
|
"conserver_removed": true,
|
||||||
|
"old_lab3_image_removed": true,
|
||||||
|
"old_empty_networks_removed": true,
|
||||||
|
"current_lab5_images_retained": true
|
||||||
|
},
|
||||||
|
"maintenance_checks": {
|
||||||
|
"status": "passed",
|
||||||
|
"command": "make test-suite",
|
||||||
|
"project": "messenger-test",
|
||||||
|
"pipeline_tests": 42,
|
||||||
|
"checks": [
|
||||||
|
"contract",
|
||||||
|
"OpenAPI",
|
||||||
|
"Compose startup",
|
||||||
|
"public smoke",
|
||||||
|
"MinIO private access/bootstrap",
|
||||||
|
"outbox recovery",
|
||||||
|
"worker_stopped_restart_all",
|
||||||
|
"crash-after-object",
|
||||||
|
"crash-before-ack",
|
||||||
|
"fail-transient",
|
||||||
|
"fail-permanent"
|
||||||
|
],
|
||||||
|
"standalone_nginx": "passed",
|
||||||
|
"test_resources_removed": true,
|
||||||
|
"obsolete_image_tags_removed": [
|
||||||
|
"mtusi-messenger-minio-bootstrap:lab5",
|
||||||
|
"mtusi-messenger-minio:lab5",
|
||||||
|
"mtusi-messenger:lab5"
|
||||||
|
],
|
||||||
|
"regression_tests_preserved": true,
|
||||||
|
"note": "Earlier recorded commands retain their original names as historical evidence; operational scripts and resource names are now neutral."
|
||||||
|
}
|
||||||
|
}
|
||||||
+13
-4
@@ -1,7 +1,16 @@
|
|||||||
# Инфраструктура студента
|
# Инфраструктура
|
||||||
|
|
||||||
Здесь разместите конфигурации выбранных сервисов; основную топологию — в корневом compose.yaml/stack.yaml. `compose.example.yaml` — только worksheet ролей и намеренно не запускается. `contracts/infrastructure.json` задаёт минимальные роли, не готовые образы/пароли.
|
compose.yaml запускает две API-реплики за nginx, PostgreSQL, Redis сессий,
|
||||||
|
RabbitMQ, приватный MinIO, publisher, worker и cleanup. Все environment-настройки
|
||||||
|
берутся из .env; credentials передаются файлами secrets. Миграции и bootstrap
|
||||||
|
завершаются до старта зависимых сервисов.
|
||||||
|
|
||||||
В REPORT.md сопоставьте роль → service name → версия/digest → health/readiness → volume → сеть/порт → способ передачи credentials. Приведите bootstrap (schema, bucket, broker topology, а с lab6 provisioning) и безопасное повторение этих команд.
|
Publisher читает outbox PostgreSQL и ждёт подтверждения RabbitMQ. Worker фиксирует
|
||||||
|
результат в MinIO/PostgreSQL до ack; prefetch=1 на consumer. init:true обеспечивает
|
||||||
|
корректные сигналы. Broker и хранилища не публикуют внешние порты.
|
||||||
|
|
||||||
Приложение строится из исходников. Не используйте заранее существующий образ преподавательского сервера вместо своей реализации. Restart hook перезапускает все соответствующие stores с volumes; failover hook останавливает только заданную API-реплику.
|
make up/down управляют основным проектом messenger и сохраняют volumes.
|
||||||
|
make test-suite использует отдельный messenger-test и удаляет его вместе с
|
||||||
|
volumes и сетями при завершении. compose.test.yaml открывает тестовые PostgreSQL
|
||||||
|
и Redis только на localhost. Полная регрессия доступна через make test-unit,
|
||||||
|
отдельные проверки — make test-nginx, test-storage, test-outbox и test-jobs.
|
||||||
|
|||||||
Executable
+8
@@ -0,0 +1,8 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
set -eu
|
||||||
|
# Secret читается внутри контейнера, не попадает в compose config/аргументы.
|
||||||
|
export RABBITMQ_DEFAULT_PASS="$(cat "${BROKER_PASSWORD_FILE:?}")"
|
||||||
|
export RABBITMQ_DEFAULT_USER="${BROKER_USER:?}"
|
||||||
|
export RABBITMQ_DEFAULT_VHOST="${BROKER_VHOST:?}"
|
||||||
|
export RABBITMQ_NODE_PORT="${BROKER_PORT:?}"
|
||||||
|
exec /usr/local/bin/docker-entrypoint.sh rabbitmq-server
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
FROM golang:1.24.8-bookworm AS compiler
|
||||||
|
ENV CGO_ENABLED=0 GOTOOLCHAIN=local GOBIN=/out GOMAXPROCS=4
|
||||||
|
RUN --mount=type=cache,target=/go/pkg/mod --mount=type=cache,target=/root/.cache/go-build \
|
||||||
|
go install -p 4 -ldflags='-s -w -X github.com/minio/minio/cmd.Version=RELEASE.2025-10-15T17-29-55Z -X github.com/minio/minio/cmd.ReleaseTag=RELEASE.2025-10-15T17-29-55Z' github.com/minio/minio@RELEASE.2025-10-15T17-29-55Z
|
||||||
|
RUN --mount=type=cache,target=/go/pkg/mod --mount=type=cache,target=/root/.cache/go-build \
|
||||||
|
go install -p 4 -ldflags='-s -w' github.com/minio/mc@RELEASE.2025-08-13T08-35-41Z
|
||||||
|
|
||||||
|
FROM python:3.12.15-slim-bookworm AS runtime
|
||||||
|
RUN useradd --system --uid 10001 --no-create-home minio && mkdir /data && chown minio /data
|
||||||
|
USER minio
|
||||||
|
|
||||||
|
FROM runtime AS server
|
||||||
|
COPY --from=compiler /out/minio /usr/local/bin/minio
|
||||||
|
LABEL org.opencontainers.image.source="https://github.com/minio/minio" \
|
||||||
|
org.opencontainers.image.version="RELEASE.2025-10-15T17-29-55Z"
|
||||||
|
VOLUME ["/data"]
|
||||||
|
ENTRYPOINT ["/usr/local/bin/minio"]
|
||||||
|
|
||||||
|
FROM runtime AS bootstrap
|
||||||
|
COPY --from=compiler /out/mc /usr/local/bin/mc
|
||||||
|
COPY infra/minio/bootstrap.py /bootstrap.py
|
||||||
|
ENTRYPOINT ["python", "/bootstrap.py"]
|
||||||
@@ -0,0 +1,120 @@
|
|||||||
|
"""Идемпотентный private bucket и отдельная ограниченная учётная запись API."""
|
||||||
|
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
import subprocess
|
||||||
|
from tempfile import TemporaryDirectory
|
||||||
|
import time
|
||||||
|
from urllib.request import urlopen
|
||||||
|
|
||||||
|
|
||||||
|
def secret(name):
|
||||||
|
value = Path(os.environ[name.upper() + "_FILE"]).read_text().strip()
|
||||||
|
if not value:
|
||||||
|
raise ValueError("Пустой файл секрета")
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
endpoint = os.environ["S3_ENDPOINT"]
|
||||||
|
bucket = os.environ["S3_BUCKET"]
|
||||||
|
root_user, root_password = secret("minio_root_user"), secret("minio_root_password")
|
||||||
|
access, password = secret("s3_access_key_id"), secret("s3_secret_access_key")
|
||||||
|
if access == root_user:
|
||||||
|
raise ValueError("API не должен использовать root credentials")
|
||||||
|
deadline = time.monotonic() + 60
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
with urlopen(endpoint + "/minio/health/ready", timeout=2) as response:
|
||||||
|
if response.status == 200:
|
||||||
|
break
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
if time.monotonic() >= deadline:
|
||||||
|
raise RuntimeError("MinIO не готов за 60 секунд")
|
||||||
|
time.sleep(1)
|
||||||
|
with TemporaryDirectory(prefix="minio-bootstrap-") as directory:
|
||||||
|
config = Path(directory)
|
||||||
|
# Credentials не передаются mc alias set через аргументы или окружение.
|
||||||
|
config_file = config / "config.json"
|
||||||
|
config_file.write_text(
|
||||||
|
json.dumps(
|
||||||
|
{
|
||||||
|
"version": "10",
|
||||||
|
"aliases": {
|
||||||
|
"storage": {
|
||||||
|
"url": endpoint,
|
||||||
|
"accessKey": root_user,
|
||||||
|
"secretKey": root_password,
|
||||||
|
"api": "S3v4",
|
||||||
|
"path": "auto",
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
config_file.chmod(0o600)
|
||||||
|
|
||||||
|
def mc(*args):
|
||||||
|
result = subprocess.run(
|
||||||
|
["mc", "--config-dir", directory, *args],
|
||||||
|
stdout=subprocess.DEVNULL,
|
||||||
|
stderr=subprocess.DEVNULL,
|
||||||
|
timeout=30,
|
||||||
|
)
|
||||||
|
if result.returncode:
|
||||||
|
raise RuntimeError(
|
||||||
|
"Ошибка bootstrap MinIO; значения credentials скрыты"
|
||||||
|
)
|
||||||
|
|
||||||
|
mc("mb", "--ignore-existing", "storage/" + bucket)
|
||||||
|
mc("anonymous", "set", "none", "storage/" + bucket)
|
||||||
|
policy = {
|
||||||
|
"Version": "2012-10-17",
|
||||||
|
"Statement": [
|
||||||
|
{
|
||||||
|
"Effect": "Allow",
|
||||||
|
"Action": ["s3:GetBucketLocation", "s3:ListBucket"],
|
||||||
|
"Resource": [f"arn:aws:s3:::{bucket}"],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Effect": "Allow",
|
||||||
|
"Action": ["s3:GetObject", "s3:PutObject", "s3:DeleteObject"],
|
||||||
|
"Resource": [
|
||||||
|
f"arn:aws:s3:::{bucket}/{prefix}/*"
|
||||||
|
for prefix in ("originals", "staging", "variants")
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
policy_file = config / "policy.json"
|
||||||
|
policy_file.write_text(json.dumps(policy))
|
||||||
|
mc(
|
||||||
|
"admin",
|
||||||
|
"policy",
|
||||||
|
"create",
|
||||||
|
"storage",
|
||||||
|
"messenger-attachments",
|
||||||
|
str(policy_file),
|
||||||
|
)
|
||||||
|
# mc не поддерживает stdin для user add: вывод и ошибки команды скрыты.
|
||||||
|
mc("admin", "user", "add", "storage", access, password)
|
||||||
|
mc(
|
||||||
|
"admin",
|
||||||
|
"policy",
|
||||||
|
"attach",
|
||||||
|
"storage",
|
||||||
|
"messenger-attachments",
|
||||||
|
"--user",
|
||||||
|
access,
|
||||||
|
)
|
||||||
|
print("MinIO private bucket and API policy configured", flush=True)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
try:
|
||||||
|
main()
|
||||||
|
except Exception as error:
|
||||||
|
print("MinIO bootstrap failed: " + type(error).__name__, flush=True)
|
||||||
|
raise SystemExit(1) from None
|
||||||
@@ -0,0 +1,140 @@
|
|||||||
|
# Единая точка входа: round-robin между API-репликами, без sticky sessions.
|
||||||
|
# Директивы рассчитаны на закреплённый nginx:1.28.0-alpine.
|
||||||
|
|
||||||
|
worker_processes 2;
|
||||||
|
|
||||||
|
events {
|
||||||
|
worker_connections 1024;
|
||||||
|
}
|
||||||
|
|
||||||
|
http {
|
||||||
|
# В логе используем request_id ответа API, для собственных ошибок — id nginx.
|
||||||
|
map $upstream_http_x_request_id $response_request_id {
|
||||||
|
"" $request_id;
|
||||||
|
default $upstream_http_x_request_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
log_format requests escape=json
|
||||||
|
'{"time":"$time_iso8601","request_id":"$response_request_id",'
|
||||||
|
'"method":"$request_method","uri":"$uri","status":$status,'
|
||||||
|
'"bytes":$body_bytes_sent,"request_time":$request_time,'
|
||||||
|
'"upstream":"$upstream_addr","upstream_status":"$upstream_status",'
|
||||||
|
'"upstream_time":"$upstream_response_time",'
|
||||||
|
'"instance_id":"$upstream_http_x_instance_id",'
|
||||||
|
'"limit_status":"$limit_req_status"}';
|
||||||
|
|
||||||
|
access_log /dev/stdout requests;
|
||||||
|
error_log /dev/stderr warn;
|
||||||
|
server_tokens off;
|
||||||
|
|
||||||
|
# DNS Docker возвращает адреса всех реплик сервиса api.
|
||||||
|
resolver 127.0.0.11 valid=5s ipv6=off;
|
||||||
|
resolver_timeout 1s;
|
||||||
|
|
||||||
|
# Общий для worker-процессов лимит на IP, который видит nginx.
|
||||||
|
# Клиентский X-Forwarded-For не используется в качестве ключа лимита.
|
||||||
|
limit_req_zone $binary_remote_addr zone=per_client:10m rate=50r/s;
|
||||||
|
|
||||||
|
gzip on;
|
||||||
|
gzip_vary on;
|
||||||
|
gzip_min_length 1024;
|
||||||
|
gzip_comp_level 3;
|
||||||
|
gzip_types application/json;
|
||||||
|
|
||||||
|
upstream api_backend {
|
||||||
|
zone api_backend 64k;
|
||||||
|
server api:8080 resolve max_fails=1 fail_timeout=5s;
|
||||||
|
|
||||||
|
# Кэш свободных соединений на worker, а не лимит запросов к API.
|
||||||
|
keepalive 16;
|
||||||
|
keepalive_timeout 4s;
|
||||||
|
}
|
||||||
|
|
||||||
|
server {
|
||||||
|
listen 8080;
|
||||||
|
|
||||||
|
client_header_timeout 10s;
|
||||||
|
client_body_timeout 10s;
|
||||||
|
# Размер ограничивает API при чтении, включая chunked-передачу.
|
||||||
|
client_max_body_size 0;
|
||||||
|
client_body_buffer_size 64k;
|
||||||
|
send_timeout 10s;
|
||||||
|
keepalive_timeout 30s;
|
||||||
|
|
||||||
|
proxy_http_version 1.1;
|
||||||
|
proxy_set_header Connection "";
|
||||||
|
proxy_set_header Host $host;
|
||||||
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
|
proxy_set_header X-Forwarded-Proto $scheme;
|
||||||
|
|
||||||
|
proxy_connect_timeout 500ms;
|
||||||
|
proxy_send_timeout 10s;
|
||||||
|
proxy_read_timeout 10s;
|
||||||
|
|
||||||
|
# Пассивное обнаружение отказов; Docker healthcheck сам не управляет LB.
|
||||||
|
# non_idempotent не включён: уже отправленные POST не повторяем.
|
||||||
|
proxy_next_upstream error timeout http_502 http_503 http_504;
|
||||||
|
proxy_next_upstream_tries 2;
|
||||||
|
# Окно перехода на другой backend, не общий deadline ответа.
|
||||||
|
proxy_next_upstream_timeout 2s;
|
||||||
|
|
||||||
|
# JSON и заголовки ошибок API передаются без подмены.
|
||||||
|
proxy_intercept_errors off;
|
||||||
|
error_page 429 = @rate_limited;
|
||||||
|
error_page 502 503 504 = @unavailable;
|
||||||
|
|
||||||
|
# Проверка самого LB для healthcheck в compose; не проксируется в API.
|
||||||
|
location = /lb-health {
|
||||||
|
access_log off;
|
||||||
|
default_type text/plain;
|
||||||
|
return 200 "ok\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
# Проверки готовности/жизни не расходуют бюджет бизнес-запросов.
|
||||||
|
location /health/ {
|
||||||
|
proxy_pass http://api_backend;
|
||||||
|
}
|
||||||
|
|
||||||
|
location / {
|
||||||
|
limit_req zone=per_client burst=100 nodelay;
|
||||||
|
limit_req_status 429;
|
||||||
|
limit_req_log_level warn;
|
||||||
|
proxy_pass http://api_backend;
|
||||||
|
}
|
||||||
|
|
||||||
|
location ~ ^/api/v1/chats/[^/]+/attachments$ {
|
||||||
|
limit_req zone=per_client burst=100 nodelay;
|
||||||
|
limit_req_status 429;
|
||||||
|
proxy_request_buffering off;
|
||||||
|
proxy_buffering off;
|
||||||
|
proxy_read_timeout 65s;
|
||||||
|
proxy_send_timeout 15s;
|
||||||
|
# Ошибка одного upload не должна скрыть работающую реплику для чатов.
|
||||||
|
proxy_next_upstream off;
|
||||||
|
proxy_pass http://api_backend;
|
||||||
|
}
|
||||||
|
|
||||||
|
location ~ ^/api/v1/attachments/[^/]+/content$ {
|
||||||
|
limit_req zone=per_client burst=100 nodelay;
|
||||||
|
limit_req_status 429;
|
||||||
|
proxy_buffering off;
|
||||||
|
proxy_read_timeout 65s;
|
||||||
|
proxy_pass http://api_backend;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Это ответы балансировщика: X-Instance-Id реплики у них отсутствует.
|
||||||
|
location @rate_limited {
|
||||||
|
default_type application/json;
|
||||||
|
add_header X-Request-Id $request_id always;
|
||||||
|
add_header Retry-After 1 always;
|
||||||
|
return 429 '{"error":{"code":"rate_limited","message":"Request rate exceeded","request_id":"$request_id"}}';
|
||||||
|
}
|
||||||
|
|
||||||
|
location @unavailable {
|
||||||
|
default_type application/json;
|
||||||
|
add_header X-Request-Id $request_id always;
|
||||||
|
add_header Retry-After 1 always;
|
||||||
|
return 503 '{"error":{"code":"unavailable","message":"API temporarily unavailable","request_id":"$request_id"}}';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user