diff --git a/github_tracker_bot/bot.py b/github_tracker_bot/bot.py index 2016850..7aebd7b 100644 --- a/github_tracker_bot/bot.py +++ b/github_tracker_bot/bot.py @@ -29,29 +29,9 @@ from slowapi.middleware import SlowAPIMiddleware from slowapi.errors import RateLimitExceeded - -@asynccontextmanager -async def lifespan(app: FastAPI): - app.state.scheduler_task = asyncio.create_task(scheduler()) - logger.info("Scheduler started on application startup") - - try: - yield - finally: - if app.state.scheduler_task: - app.state.scheduler_task.cancel() - try: - await app.state.scheduler_task - except asyncio.CancelledError: - pass - app.state.scheduler_task = None - logger.info("Scheduler stopped on application shutdown") - - -app = FastAPI(lifespan=lifespan) +app = FastAPI() limiter = Limiter(key_func=get_remote_address, default_limits=["10/minute"]) - app.state.limiter = limiter app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler) app.add_middleware(SlowAPIMiddleware) @@ -114,6 +94,24 @@ async def job(): await asyncio.sleep(1) +@app.on_event("startup") +async def startup_event(): + app.state.scheduler_task = asyncio.create_task(scheduler()) + logger.info("Scheduler started on application startup") + + +@app.on_event("shutdown") +async def shutdown_event(): + if app.state.scheduler_task: + app.state.scheduler_task.cancel() + try: + await app.state.scheduler_task + except asyncio.CancelledError: + pass + app.state.scheduler_task = None + logger.info("Scheduler stopped on application shutdown") + + @app.middleware("http") async def check_auth_token(request: Request, call_next): auth_token = config.SHARED_SECRET