1
1
from datetime import datetime
2
2
3
+ from attrs import define
4
+
3
5
from sqlalchemy import text
4
6
from starlette .types import ASGIApp , Receive , Scope , Send
5
7
from apscheduler import AsyncScheduler
@@ -20,16 +22,20 @@ async def tick():
20
22
return True
21
23
22
24
25
+ @define
23
26
class 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
31
29
32
30
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
+ """
33
39
if scope ["type" ] == "lifespan" :
34
40
async with self .scheduler :
35
41
await self .scheduler .add_schedule (
@@ -39,3 +45,4 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
39
45
await self .app (scope , receive , send )
40
46
else :
41
47
await self .app (scope , receive , send )
48
+
0 commit comments