agronholm / agronholm/apscheduler
Method configure_task(Task(some_func)) doesn't register the callable in the scheduler (left at "None", causing the scheduler to crash)
- Dominant language
- Python
- Stars
- 7.6k
- Forks
- 783
- Avg merge
- 4d 8h
- Merged PRs (30d)
- 5
Description
### Things to check first
- [X] I have checked that my issue does not already have a solution in the [FAQ](https://apscheduler.readthedocs.io/en/master/faq.html)
- [X] I have searched the existing issues and didn't find my bug already reported there
- [X] I have checked that my bug is still present in the latest release
### Version
4.0.0a5post80
### What happened?
We're using version 4.0.0a5.post80 of apscheduler and discovered an issue when configuring tasks on a scheduler passing a "Task" object. Basically, the Callable I'm passing is not stored as part of the Scheduler Task.
```py
import asyncio
from apscheduler import AsyncScheduler, Task
async def my_func():
print("Hello World")
return 42
async def main():
async with scheduler as s:
# Generate the Task object
the_task = Task(id="test", func="__main__:my_func", job_executor="threadpool")
# Register the Task with the Scheduler
await s.configure_task(the_task)
# print the tasks
# Note that this line returns something like this:
# [Task(id='test', func=None, job_executor='threadpool', max_running_jobs=None, misfire_grace_time=None, metadata={}, running_jobs=0)]
print(await s.get_tasks())
await s.add_job("test")
await scheduler.run_until_stopped()
asyncio.run(main())
```
```
Scheduler crashed
+ Exception Group Traceback (most recent call last):
| File "/workspaces/myproject/.venv/lib/python3.12/site-packages/apscheduler/_schedulers/async_.py", line 784, in run_until_stopped
| async with create_task_group() as task_group:
| ^^^^^^^^^^^^^^^^^^^
| File "/workspaces/myproject/.venv/lib/python3.12/site-packages/anyio/_backends/_asyncio.py", line 763, in __aexit__
| raise BaseExceptionGroup(
| ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
+-+---------------- 1 ----------------
| Traceback (most recent call last):
| File "/workspaces/myproject/.venv/lib/python3.12/site-packages/apscheduler/_schedulers/async_.py", line 1012, in _process_jobs
| func = self._get_task_callable(task)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| File "/workspaces/myproject/.venv/lib/python3.12/site-packages/apscheduler/_schedulers/async_.py", line 974, in _get_task_callable
| raise CallableLookupError(
| apscheduler._exceptions.CallableLookupError: Task test requires a locally defined callable to be run, but no such callable has been defined. Call scheduler.configure_task('test', func=...) to define the local callable.
|
...
```
When debugging, it turns out that the parameter passed [here](https://github.com/agronholm/apscheduler/blob/master/src/apscheduler/_schedulers/async_.py#L410) is actually None. This only happens as you pass a Task object to the scheduler. If you pass in the callable function directly, things work like a charm.
Your guidance / support is highly appreciated. Thanks a lot.
### How can we reproduce the bug?
see above :)
Contributor guide
Assessment
This issue has not been assessed yet.