ЛР4 БВТ2304 Запорожец на 3
Вложения: приватный S3-bucket (RustFS 1.0.4) + метаданные в PostgreSQL, upload PNG/JPEG с лимитами до декодирования, авторизованный download, attachment_ids в сообщениях, идемпотентный bootstrap bucket, failover/persistence/S3-down тесты и REPORT.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
"""Запуск: python -m app (HTTP-процесс — PID 1 в контейнере).
|
||||
|
||||
SIGTERM (docker stop) обрабатывается в два этапа:
|
||||
1. drain: SHUTDOWN_DRAIN_MS процесс продолжает обслуживать запросы, но /health/ready отвечает 503 —
|
||||
балансировщик за это время исключает реплику по активной проверке и перестаёт слать новые запросы;
|
||||
2. обычный graceful shutdown uvicorn: новые соединения не принимаются, начатые запросы
|
||||
дообрабатываются до timeout_graceful_shutdown.
|
||||
"""
|
||||
import logging
|
||||
import os
|
||||
import threading
|
||||
|
||||
import uvicorn
|
||||
|
||||
from . import lifecycle
|
||||
|
||||
log = logging.getLogger('messenger')
|
||||
|
||||
|
||||
class DrainingServer(uvicorn.Server):
|
||||
drain_seconds = 0.0
|
||||
|
||||
def handle_exit(self, sig, frame):
|
||||
if lifecycle.draining or self.drain_seconds <= 0:
|
||||
# Повторный сигнал (или drain выключен) — сразу штатное завершение uvicorn.
|
||||
return super().handle_exit(sig, frame)
|
||||
lifecycle.start_draining()
|
||||
log.info('signal %s: draining for %.1fs before shutdown, instance=%s',
|
||||
sig, self.drain_seconds, lifecycle.INSTANCE_ID)
|
||||
timer = threading.Timer(self.drain_seconds, super().handle_exit, (sig, frame))
|
||||
timer.daemon = True
|
||||
timer.start()
|
||||
|
||||
|
||||
def main() -> None:
|
||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(name)s %(message)s')
|
||||
config = uvicorn.Config('app.main:app', host=os.environ.get('APP_HOST', '0.0.0.0'),
|
||||
port=int(os.environ.get('APP_PORT', '8080')), proxy_headers=False,
|
||||
server_header=False, timeout_graceful_shutdown=10,
|
||||
timeout_keep_alive=30) # дольше, чем держит idle-соединение HAProxy
|
||||
server = DrainingServer(config)
|
||||
server.drain_seconds = int(os.environ.get('SHUTDOWN_DRAIN_MS', '3000')) / 1000
|
||||
log.info('starting instance %s', lifecycle.INSTANCE_ID)
|
||||
server.run()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user