11from datetime import datetime
22
3+ from attrs import define
4+
35from sqlalchemy import text
46from starlette .types import ASGIApp , Receive , Scope , Send
57from apscheduler import AsyncScheduler
@@ -20,16 +22,20 @@ async def tick():
2022 return True
2123
2224
25+ @define
2326class SchedulerMiddleware :
24- def __init__ (
25- self ,
26- app : ASGIApp ,
27- scheduler : AsyncScheduler ,
28- ) -> None :
29- self .app = app
30- self .scheduler = scheduler
27+ app : ASGIApp
28+ scheduler : AsyncScheduler
3129
3230 async def __call__ (self , scope : Scope , receive : Receive , send : Send ) -> None :
31+ """
32+ Handles the incoming request and schedules tasks if the scope type is 'lifespan'.
33+
34+ Args:
35+ scope (Scope): The ASGI scope dictionary containing request information.
36+ receive (Receive): The ASGI receive callable.
37+ send (Send): The ASGI send callable.
38+ """
3339 if scope ["type" ] == "lifespan" :
3440 async with self .scheduler :
3541 await self .scheduler .add_schedule (
@@ -39,3 +45,4 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
3945 await self .app (scope , receive , send )
4046 else :
4147 await self .app (scope , receive , send )
48+
0 commit comments