Skip to content

Commit

Permalink
fix: dockerfile and main
Browse files Browse the repository at this point in the history
  • Loading branch information
erfjab committed Oct 25, 2024
1 parent 7ec075c commit 8d366b9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ WORKDIR /code

COPY ./requirements.txt .

RUN pip install --no-cache-dir --upgrade -r requirements.txt
RUN apk add --no-cache tzdata && \
cp /usr/share/zoneinfo/UTC /etc/localtime && \
echo "UTC" > /etc/timezone && \
pip install --no-cache-dir --upgrade -r requirements.txt

COPY . .

Expand Down
33 changes: 14 additions & 19 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
http_exception_handler,
request_validation_exception_handler,
)
from contextlib import asynccontextmanager

from jobs import stop_scheduler, start_scheduler
from routers import subscription
Expand All @@ -26,15 +27,24 @@

__version__ = "0.1.0"


async def create_app() -> FastAPI:

@asynccontextmanager
async def lifespan(app: FastAPI):
"""Manage application startup and shutdown events."""
await start_scheduler()
logger.info("Application started successfully.")
yield # App will be running during this period
await stop_scheduler()
logger.info("Application shut down successfully.")

def create_app() -> FastAPI:
"""Create and configure FastAPI application instance."""
app = FastAPI(
title="Migration",
description="API to proxy subscription requests between users and backend system",
version=__version__,
docs_url="/docs" if DOCS else None,
redoc_url="/redoc" if DOCS else None,
lifespan=lifespan, # Set the lifespan context manager
)

app.openapi = custom_openapi(app)
Expand Down Expand Up @@ -67,24 +77,9 @@ async def validation_exception_handler(request, exc):
return app


async def setup_event_handlers(app: FastAPI):
"""Set up event handlers for the application."""

@app.on_event("startup")
async def on_startup():
await start_scheduler()
logger.info("Application started successfully.")

@app.on_event("shutdown")
async def on_shutdown():
await stop_scheduler()
logger.info("Application shut down successfully.")


async def main():
"""Main function to run the application."""
app = await create_app()
await setup_event_handlers(app)
app = create_app()

config = uvicorn.Config(
app,
Expand Down

0 comments on commit 8d366b9

Please sign in to comment.