Spaces:
Runtime error
Runtime error
File size: 2,581 Bytes
94312d9 cf73a43 94312d9 cc7f27e cf73a43 cc7f27e cf73a43 94312d9 6491a46 94312d9 6491a46 94312d9 cc7f27e 94312d9 cc7f27e 94312d9 6372741 94312d9 a5aac16 cf73a43 a5aac16 cc7f27e 040da4c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | import asyncio
import os
from contextlib import asynccontextmanager
from threading import Thread
import pytz
from alembic import command
from alembic.config import Config
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from src.controllers import bot_router, file_router, user_router
from src.repositories import DatabaseConfig
from src.services import ContentDeliveryService
from src.utils import OpenAIClient, logger
scheduler = AsyncIOScheduler()
scheduler.configure({"apscheduler.timezone": pytz.timezone("America/New_York")})
@scheduler.scheduled_job("cron", hour="*", minute="*", second="*", day_of_week="mon")
async def scheduled_job():
try:
async with ContentDeliveryService() as content_delivery_service:
await content_delivery_service.send_messages()
except Exception as e:
logger.error(e)
def run_upgrade(connection, alembic_config: Config):
alembic_config.attributes["connection"] = connection
command.upgrade(alembic_config, "head")
async def run_migrations():
logger.info("Running migrations if any...")
alembic_config = Config("alembic.ini")
alembic_config.set_main_option(
"sqlalchemy.url", os.getenv("SQLALCHEMY_DATABASE_URI")
)
async with DatabaseConfig.async_engine().begin() as session:
await session.run_sync(run_upgrade, alembic_config)
def run_scheduler():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
scheduler.start()
loop.run_forever()
@asynccontextmanager
async def lifespan(app: FastAPI):
try:
logger.info("Starting up the application...")
await run_migrations()
scheduler_thread = Thread(target=run_scheduler, daemon=True)
scheduler_thread.start()
logger.info("Application started successfully...")
yield
except Exception as e:
logger.error(f"Error during startup: {str(e)}")
raise
finally:
logger.info("Shutting down the application...")
scheduler.shutdown(wait=True)
logger.info("Application shutdown complete.")
app = FastAPI(lifespan=lifespan)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
async def check_health():
return {"response": "Service is healthy!"}
app.include_router(bot_router, prefix="/api/v1")
app.include_router(user_router, prefix="/api/v1")
app.include_router(file_router, prefix="/api/v1")
|