-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Abort async tasks using asyncio #1190
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,6 +119,7 @@ async def _persist_job_status( | |
job: jobs.Job, | ||
status: jobs.Status, | ||
retry_decision: retry.RetryDecision | None, | ||
context: job_context.JobContext, | ||
): | ||
if retry_decision: | ||
await self.app.job_manager.retry_job( | ||
|
@@ -138,6 +139,13 @@ async def _persist_job_status( | |
job=job, status=status, delete_job=delete_job | ||
) | ||
|
||
self._job_ids_to_abort.discard(job.id) | ||
|
||
self.logger.debug( | ||
f"Acknowledged job completion {job.call_string}", | ||
extra=self._log_extra(action="finish_task", context=context, status=status), | ||
) | ||
|
||
def _log_job_outcome( | ||
self, | ||
status: jobs.Status, | ||
|
@@ -257,18 +265,20 @@ async def ensure_async() -> Callable[..., Awaitable]: | |
job_retry=job_retry, | ||
exc_info=exc_info, | ||
) | ||
await self._persist_job_status( | ||
job=job, status=status, retry_decision=retry_decision | ||
) | ||
|
||
self._job_ids_to_abort.discard(job.id) | ||
|
||
self.logger.debug( | ||
f"Acknowledged job completion {job.call_string}", | ||
extra=self._log_extra( | ||
action="finish_task", context=context, status=status | ||
), | ||
persist_job_status_task = asyncio.create_task( | ||
self._persist_job_status( | ||
job=job, | ||
status=status, | ||
retry_decision=retry_decision, | ||
context=context, | ||
) | ||
) | ||
try: | ||
await asyncio.shield(persist_job_status_task) | ||
except asyncio.CancelledError: | ||
await persist_job_status_task | ||
raise | ||
|
||
async def _fetch_and_process_jobs(self): | ||
"""Fetch and process jobs until there is no job left or asked to stop""" | ||
|
@@ -372,8 +382,19 @@ async def _poll_jobs_to_abort(self): | |
|
||
def _handle_abort_jobs_requested(self, job_ids: Iterable[int]): | ||
running_job_ids = {c.job.id for c in self._running_jobs.values() if c.job.id} | ||
self._job_ids_to_abort |= set(job_ids) | ||
self._job_ids_to_abort &= running_job_ids | ||
new_job_ids_to_abort = (running_job_ids & set(job_ids)) - self._job_ids_to_abort | ||
self._job_ids_to_abort |= new_job_ids_to_abort | ||
|
||
tasks_to_cancel = ( | ||
task | ||
for (task, context) in self._running_jobs.items() | ||
if context.job.id in new_job_ids_to_abort | ||
and context.task | ||
and asyncio.iscoroutinefunction(context.task.func) | ||
) | ||
|
||
for task in tasks_to_cancel: | ||
task.cancel() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's worth logging something (in debug) for each cancelled task. I think I would remove the
above, and instead make a dedicated function that receives a task & context and logs and cancels it if it's a coroutine. |
||
|
||
async def _shutdown(self, side_tasks: list[asyncio.Task]): | ||
""" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically, what prevents the job from being aborted is catching the error, not shielding.
In practice, it is likely the combination of both that the user wants.
How would you prefer to word it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.