agronholm / agronholm/apscheduler

Allow jobs to time out

Offen
#1,005 7 Kommentare 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
enhancement
Vorherrschende Sprache
Python
Sterne
7.6k
Forks
783
Ø Merge
4 T. 8 Std.
Gemergte PRs (30 T.)
5

Beschreibung

### Things to check first

- [X] I have searched the existing issues and didn't find my feature already requested there

### Feature description

This is mentioned in the Roadmap (#465). I am just opening this issue as a place to discuss this feature and how to realize it as I am currently trying to program something like this.

In the Roadmap, the point I am referring to is 'Timeouts for jobs'. I will precise now how I would try to realize this. If you have any other wishes/ideas/suggestions on how this feature should be implemented: That's exactly the reason I created this issue (-:

I would add a timeout parameter to the Job structure and would handle the timeout in the different job executors. For example, here is an example of how this could work with the `AsyncJobExecutor`:

```python
class AsyncJobExecutor(JobExecutor):
"""
Executes functions directly on the event loop thread.

If the function returns a coroutine object (or another kind of awaitable), that is
awaited on and its return value is used as the job's return value.
"""

async def run_job(self, func: Callable[..., Any], job: Job) -> Any:
# Convert timeout to seconds if it's a timedelta
timeout_seconds = job.timeout.total_seconds() if isinstance(job.timeout, timedelta) else job.timeout

async def wrapper():
retval = func(*job.args, **job.kwargs)
if isawaitable(retval):
retval = await retval
return retval

try:
async with anyio.fail_after(timeout_seconds):
return await wrapper()
except TimeoutError:
raise JobTimedOutError from None
```

The `JobOutcome` should receive a new state called `timeout` which should be set if the `JobTimedOutError` (or we could also use the `TimeoutError` directly which anyio uses) is raised. The scheduler needs to catch that and handle it appropriately.

### Use case

As this is in the roadmap, I think the use case is clear.

Beitragsleitfaden

Beitragsleitfaden öffnen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.