Rebase sync
This commit is contained in:
parent
1b8cfe0a6b
commit
2b991108f7
2 changed files with 15 additions and 5 deletions
|
|
@ -100,16 +100,26 @@ class AnalysisService:
|
||||||
await _mark_failed(job_id, str(e))
|
await _mark_failed(job_id, str(e))
|
||||||
|
|
||||||
|
|
||||||
|
_background_tasks: set[asyncio.Task] = set()
|
||||||
|
|
||||||
|
|
||||||
def _on_task_done(task: asyncio.Task, *, job_id: str) -> None:
|
def _on_task_done(task: asyncio.Task, *, job_id: str) -> None:
|
||||||
"""Log unhandled exceptions from background analysis tasks and mark job as FAILED."""
|
"""Log unhandled exceptions from background analysis tasks and mark job as FAILED."""
|
||||||
if task.cancelled():
|
if task.cancelled():
|
||||||
logger.warning("Analysis task was cancelled: %s", job_id)
|
logger.warning("Analysis task was cancelled: %s", job_id)
|
||||||
asyncio.ensure_future(_mark_failed(job_id, "Task was cancelled"))
|
_schedule_mark_failed(job_id, "Task was cancelled")
|
||||||
return
|
return
|
||||||
exc = task.exception()
|
exc = task.exception()
|
||||||
if exc:
|
if exc:
|
||||||
logger.error("Unhandled exception in analysis task %s: %s", job_id, exc, exc_info=True)
|
logger.error("Unhandled exception in analysis task %s: %s", job_id, exc, exc_info=True)
|
||||||
asyncio.ensure_future(_mark_failed(job_id, str(exc)))
|
_schedule_mark_failed(job_id, str(exc))
|
||||||
|
|
||||||
|
|
||||||
|
def _schedule_mark_failed(job_id: str, error: str) -> None:
|
||||||
|
"""Schedule _mark_failed as a tracked background task."""
|
||||||
|
t = asyncio.ensure_future(_mark_failed(job_id, error))
|
||||||
|
_background_tasks.add(t)
|
||||||
|
t.add_done_callback(_background_tasks.discard)
|
||||||
|
|
||||||
|
|
||||||
async def _mark_failed(job_id: str, error: str) -> None:
|
async def _mark_failed(job_id: str, error: str) -> None:
|
||||||
|
|
|
||||||
|
|
@ -40,12 +40,12 @@ class TestOnTaskDone:
|
||||||
async def slow_task():
|
async def slow_task():
|
||||||
await asyncio.sleep(999)
|
await asyncio.sleep(999)
|
||||||
|
|
||||||
|
import contextlib
|
||||||
|
|
||||||
task = asyncio.create_task(slow_task())
|
task = asyncio.create_task(slow_task())
|
||||||
task.cancel()
|
task.cancel()
|
||||||
try:
|
with contextlib.suppress(asyncio.CancelledError):
|
||||||
await task
|
await task
|
||||||
except asyncio.CancelledError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
with patch("services.analysis_service._mark_failed", new_callable=AsyncMock) as mock_mark:
|
with patch("services.analysis_service._mark_failed", new_callable=AsyncMock) as mock_mark:
|
||||||
_on_task_done(task, job_id=job_id)
|
_on_task_done(task, job_id=job_id)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue