use the uvicorn logger; also update dockerfile to run migrations as parto f startup instead of when starting uvicorn

This commit is contained in:
Viktor Barzin 2025-06-22 14:00:47 +00:00
parent 684af9a040
commit 744fa7b8a7
No known key found for this signature in database
GPG key ID: 4056458DBDBF8863
4 changed files with 17 additions and 63 deletions

View file

@ -2,9 +2,9 @@ import asyncio
import importlib
import itertools
import json
import logging
import pathlib
from typing import Any
from logger import get_logger
from rec.query import detail_query, listing_query, QueryParameters
from rec.districts import get_districts
from repositories import ListingRepository
@ -16,7 +16,7 @@ from models import Listing as modelListing
dump_images_module = importlib.import_module("3_dump_images")
detect_floorplan_module = importlib.import_module("4_detect_floorplan")
logger = get_logger(__file__)
logger = logging.getLogger("uvicorn")
async def dump_listings_full(

View file

@ -40,4 +40,5 @@ EXPOSE 8000
# Set the entry point (adjust to your CLI's entry point)
# ENTRYPOINT ["python", "/app/main.py"]
# ENTRYPOINT ["/app/runall.sh"]
ENTRYPOINT ["uvicorn", "api.app:app", "--host", "0.0.0.0", "--port", "8000"]
CMD ["/bin/bash" ,"-c" ,"alembic upgrade head && uvicorn api.app:app --host 0.0.0.0 --port 8000"]
# ENTRYPOINT ["uvicorn", "api.app:app", "--host", "0.0.0.0", "--port", "8000"]

View file

@ -1,4 +1,6 @@
import asyncio
import logging
import logging.config
from pathlib import Path
import queue
from threading import Thread
@ -15,7 +17,6 @@ from api.worker import (
)
from fastapi import Depends, FastAPI, HTTPException, Query
from api.auth import User
from logger import get_logger
from models.listing import QueryParameters
from repositories.listing_repository import ListingRepository
from repositories.listing_repository import ListingRepository
@ -26,20 +27,21 @@ from alembic import command
from alembic.config import Config
from contextlib import asynccontextmanager
logger = get_logger(__file__)
logger = logging.getLogger("uvicorn")
@asynccontextmanager
async def lifespan(app: FastAPI):
alembic_cfg = Config("./alembic.ini")
logger.info("Running alembic migrations")
command.upgrade(alembic_cfg, "head")
logger.info("Finished running alembic migrations")
yield
logger.warning("Shutting down")
# @asynccontextmanager
# async def lifespan(app: FastAPI):
# alembic_cfg = Config("./alembic.ini")
# logger.info("Running alembic migrations")
# command.upgrade(alembic_cfg, "head")
# logger.info("Finished running alembic migrations")
# yield
# logger.warning("Shutting down")
app = FastAPI(lifespan=lifespan)
# app = FastAPI(lifespan=lifespan)
app = FastAPI()
# Start worker thread
WorkerManager(DumpListingsWorker()).start()

View file

@ -1,49 +0,0 @@
from functools import lru_cache
import logging
import sys
from pathlib import Path
@lru_cache(maxsize=None)
def get_logger(
name: str,
log_file: str = "app.log",
console_level: str = "DEBUG",
file_level: str = "DEBUG",
):
"""
Configure a logger with console and file handlers.
Args:
name: Logger name (usually __name__).
log_file: Path to the log file.
console_level: Console logging level (INFO, DEBUG, etc.).
file_level: File logging level.
"""
logger = logging.getLogger(name)
logger.setLevel(logging.DEBUG) # Set to lowest level (handlers filter further)
# Clear existing handlers (avoid duplicates in Jupyter/reloads)
logger.handlers.clear()
# ---- Console Handler ----
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setLevel(console_level.upper())
console_format = logging.Formatter(
"[%(asctime)s] %(levelname)s in %(module)s:%(lineno)d - %(message)s"
# "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
console_handler.setFormatter(console_format)
logger.addHandler(console_handler)
# ---- File Handler ----
# Path(log_file).parent.mkdir(parents=True, exist_ok=True) # Ensure dir exists
# file_handler = logging.FileHandler(log_file)
# file_handler.setLevel(file_level.upper())
# file_format = logging.Formatter(
# "%(asctime)s - %(name)s - %(levelname)s - %(message)s (%(filename)s:%(lineno)d)"
# )
# file_handler.setFormatter(file_format)
# logger.addHandler(file_handler) # skip files for now
return logger