Skip to content

Commit

Permalink
Remove migration script, Use gunicorn for deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaczero committed Apr 3, 2024
1 parent 5afe91f commit 9778da6
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 35 deletions.
2 changes: 1 addition & 1 deletion default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ let
set -o allexport
source "envs/app/${envTag}.env" set
set +o allexport
exec python -m uvicorn main:app "$@"
exec python -m gunicorn main:app "$@"
'';
in
with pkgs; dockerTools.buildLayeredImage {
Expand Down
18 changes: 11 additions & 7 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@ services:
restart: unless-stopped
command:
[
"--host",
"0.0.0.0",
"--bind",
"0.0.0.0:8000",
"--workers",
"${WORKERS:-1}",
"--timeout-keep-alive",
"300",
"--timeout-graceful-shutdown",
"--worker-class",
"uvicorn.workers.UvicornWorker",
"--max-requests",
"1000",
"--max-requests-jitter",
"100",
"--graceful-timeout",
"5",
"--proxy-headers",
"--keep-alive",
"300",
"--forwarded-allow-ips",
"*",
]
Expand All @@ -23,6 +28,5 @@ services:
- ${LISTEN:-80}:8000

volumes:
- ./.migrate.json:/app/.migrate.json
- ./data/postgres:/app/data/postgres
- /mnt/data/${TAG}/photos:/app/data/photos
4 changes: 0 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from middlewares.version_middleware import VersionMiddleware
from services.aed_service import AEDService
from services.country_service import CountryService
from services.photo_service import PhotoService
from services.worker_service import WorkerService


Expand All @@ -29,9 +28,6 @@ async def lifespan(_):
await tg.start(CountryService.update_db_task)
await tg.start(AEDService.update_db_task)

# TODO: remove after migration
await PhotoService.migrate()

await worker_state.set_state('running')
yield

Expand Down
33 changes: 32 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ beautifulsoup4 = {extras = ["charset-normalizer", "lxml"], version = "^4.12.3"}
cachetools = "^5.3.2"
fastapi = "<1"
feedgen = "^1.0.0"
gunicorn = "^21.2.0"
httpx = {extras = ["brotli", "http2"], version = "<1"}
jinja2 = "^3.1.2"
mapbox-vector-tile = "^2.0.1"
Expand Down
22 changes: 0 additions & 22 deletions services/photo_service.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,16 @@
import logging
import pathlib
from io import BytesIO

from fastapi import UploadFile
from PIL import Image, ImageOps
from sentry_sdk import trace
from sqlalchemy import select

from config import IMAGE_LIMIT_PIXELS, IMAGE_MAX_FILE_SIZE
from db import db_read, db_write
from models.db.photo import Photo
from utils import JSON_DECODE


class PhotoService:
@staticmethod
async def migrate() -> None:
async with db_write() as session:
stmt = select(Photo.id).limit(1)
scalar = await session.scalar(stmt)
if scalar is not None:
return

logging.info('Migrating photos')
file = pathlib.Path('.migrate.json').read_bytes()
data: list[dict] = JSON_DECODE(file)
for item in data:
photo = Photo(
node_id=int(item['node_id']),
user_id=int(item['user_id']),
)
photo.id = item['id']
session.add(photo)

@staticmethod
@trace
async def get_by_id(id: str, *, check_file: bool = True) -> Photo | None:
Expand Down

0 comments on commit 9778da6

Please sign in to comment.