@@ -21,34 +21,32 @@ def add_task(self, func: Callable[..., Any], *args: Any, **kwargs: Any) -> None:
2121 if settings .use_docket :
2222 logger .info ("Scheduling task through Docket" )
2323
24- # Create a wrapper that will handle Docket scheduling in a thread
25- def docket_wrapper ():
26- """Wrapper function that schedules the task through Docket"""
27-
28- def run_in_thread ():
29- """Run the async Docket operations in a separate thread"""
30- import asyncio
31-
32- from docket import Docket
33-
34- async def schedule_task ():
35- async with Docket (
36- name = settings .docket_name ,
37- url = settings .redis_url ,
38- ) as docket :
39- # Schedule task in Docket's queue
40- await docket .add (func )(* args , ** kwargs )
41-
42- # Run in a new event loop in this thread
43- asyncio .run (schedule_task ())
44-
45- # Execute in a thread pool to avoid event loop conflicts
46- with concurrent .futures .ThreadPoolExecutor () as executor :
47- future = executor .submit (run_in_thread )
48- future .result () # Wait for completion
49-
50- # Add the wrapper to FastAPI background tasks
51- super ().add_task (docket_wrapper )
24+ # Import Docket here to avoid import issues in tests
25+ from docket import Docket
26+
27+ # Schedule task directly in Docket without using FastAPI background tasks
28+ # This runs in a thread to avoid event loop conflicts
29+ def run_in_thread ():
30+ """Run the async Docket operations in a separate thread"""
31+ import asyncio
32+
33+ async def schedule_task ():
34+ async with Docket (
35+ name = settings .docket_name ,
36+ url = settings .redis_url ,
37+ ) as docket :
38+ # Schedule task in Docket's queue
39+ await docket .add (func )(* args , ** kwargs )
40+
41+ # Run in a new event loop in this thread
42+ asyncio .run (schedule_task ())
43+
44+ # Execute in a thread pool to avoid event loop conflicts
45+ with concurrent .futures .ThreadPoolExecutor () as executor :
46+ future = executor .submit (run_in_thread )
47+ future .result () # Wait for completion
48+
49+ # When using Docket, we don't add anything to FastAPI background tasks
5250 else :
5351 logger .info ("Using FastAPI background tasks" )
5452 # Use FastAPI's background tasks directly
0 commit comments