Files
Javier Martinez cd8ca2214a feat: resumable chat worker + tool worker + async tokenizer (#2298)
* fix: avoid to block the loop

* fix: blocks in expansion

* fix: remove maximum concurrent users

...

* fix: multiplexer

* fix: readers

* fix: more fixes

...

* fix: impl

* feat: tool scheduler

* feat: add adaptative

* feat: add chat worker

* fix: max

* feat: add chat/tools workers

* fix: mypy

* feat: add generic scheduler

* fix: get result

* feat: do serializable the tool executor

* fix: tools

* fix: config

* fix: config

* fix: args

* fix: config

* fix: serializer

* Revert "fix: blocks in expansion"

This reverts commit a2110f94a8.

* fix: unify all logic

* feat: add ingestion scheduler

* fix: settings

* fix: config

* feat: add arq worker to chat

* fix: arq worker

* fix: add nest

* fix: mypy

* fix: await

* fix: script stress

* fix: tokenizer

* fix: chat scheduler

* fix: mypy

* fix: add async tokenizer

* fix: improve condense

* fix: tool scheduler

* feat: add initial real async chat worker

* fix: mypy

* fix: do resumable local executor

...

...

...

fix: revert usleess changes

fix: remove parent chat job

fix: refactor

fix: loop

ref: rename models

fix: chat engine

fix: mypy

...

...

...

fix: fix deps

* fix: tests

* fix: tests

* ...

* fix: stream

* fix: config

* fix: scheduler

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Handle PGPT_WORKER_MODE=celery in health check worker status

* fix: cancel

* fix: arch

* fix: test ingestion

* fix: deserialization of chat messages

* fix: broken results

* fix: mypy

* fix: test

* fix: config

* fix: remove arq tool worker

* fix: output cls

* fix: preserve early resumable tool callbacks

* fix: preserve async tool result order

* refactor: address worker PR review comments

* fix: mypy

* test: colocate ARQ chat enqueue coverage

* fix: remove redis from tests

* test: isolate chat mocks and cancellation timing

* fix: tests

(cherry picked from commit 218b599c66)

# Conflicts:
#	tests/server/chat/anthropic/test_anthropic_client.py
#	tests/server/chat/anthropic/test_langchain_anthropic.py
#	tests/server/chat/test_chat_knowledge_revamp.py
#	tests/server/chat/test_chat_routes.py
#	tests/server/chat/test_chat_routes_skills_integration.py

* fix: tests

(cherry picked from commit fc5ec0f72a)

* fix: ruff

* fix: test

* fix: worker config

(cherry picked from commit 1371c275a1)

* fix: principal

* test: remove flaky chat cancellation assertion

---------

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
2026-07-15 12:12:45 +02:00

101 lines
3.4 KiB
Python

import asyncio
import contextlib
import logging
from collections.abc import Coroutine
from contextvars import copy_context
from typing import Any
from injector import singleton
from private_gpt.di import get_global_injector
logger = logging.getLogger(__name__)
@singleton
class TaskManager:
"""Manages asyncio tasks with cancellation support."""
def __init__(self) -> None:
self._active_tasks: dict[str, asyncio.Task[Any | None]] = {}
self._cancellation_tokens: dict[str, asyncio.Event] = {}
async def create_task(
self,
correlation_id: str,
coro: Coroutine[Any, Any, None],
name: str | None = None,
) -> asyncio.Task[Any]:
"""Create and register a new task."""
if correlation_id in self._active_tasks:
logger.warning(f"Task {correlation_id} already exists")
return self._active_tasks[correlation_id]
cancellation_token = asyncio.Event()
self._cancellation_tokens[correlation_id] = cancellation_token
ctx = copy_context()
task = asyncio.create_task(coro, name=name, context=ctx)
self._active_tasks[correlation_id] = task
task.add_done_callback(lambda _: self._cleanup_task(correlation_id))
return task
def get_task(self, correlation_id: str) -> asyncio.Task[Any | None] | None:
"""Get task by correlation ID."""
return self._active_tasks.get(correlation_id)
def get_cancellation_token(self, correlation_id: str) -> asyncio.Event | None:
"""Get cancellation token by correlation ID."""
return self._cancellation_tokens.get(correlation_id)
def is_cancelled(self, correlation_id: str) -> bool:
"""Check if task is cancelled."""
token = self._cancellation_tokens.get(correlation_id)
return token is not None and token.is_set()
async def cancel_task(self, correlation_id: str) -> bool:
"""Cancel a task and wait for completion."""
if correlation_id in self._cancellation_tokens:
self._cancellation_tokens[correlation_id].set()
task = self._active_tasks.get(correlation_id)
if task is None:
return False
task.cancel()
with contextlib.suppress(asyncio.CancelledError):
await task
return True
async def cancel(self, correlation_id: str) -> bool:
from private_gpt.server.chat.chat_service import ChatService
chat_service = get_global_injector().get(ChatService)
success = await self.cancel_task(correlation_id)
scheduled_cancel = await chat_service.cancel(correlation_id)
return success or scheduled_cancel
def _cleanup_task(self, correlation_id: str) -> None:
"""Clean up task references."""
self._active_tasks.pop(correlation_id, None)
self._cancellation_tokens.pop(correlation_id, None)
async def cancel_all_tasks(self) -> None:
"""Cancel all active tasks."""
tasks = list(self._active_tasks.keys())
await asyncio.gather(
*[self.cancel_task(task_id) for task_id in tasks],
return_exceptions=True,
)
def get_active_tasks(self) -> dict[str, asyncio.Task[Any]]:
"""Get all active tasks."""
return self._active_tasks.copy()
def get_active_count(self) -> int:
"""Get number of active tasks."""
return len(self._active_tasks)