Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change handling #44

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 19 additions & 21 deletions github_tracker_bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down