File size: 954 Bytes
8777bc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
"""In-process registry for cancellable live agent runs.

Durable status/event records live in SQLite; this registry only owns the
current task and therefore is intentionally cleared on process restart.
"""
from __future__ import annotations

import asyncio


class ActiveRunRegistry:
    def __init__(self) -> None:
        self._tasks: dict[str, asyncio.Task[object]] = {}
        self._lock = asyncio.Lock()

    async def register(self, run_id: str, task: asyncio.Task[object]) -> None:
        async with self._lock:
            self._tasks[run_id] = task

    async def unregister(self, run_id: str) -> None:
        async with self._lock:
            self._tasks.pop(run_id, None)

    async def cancel(self, run_id: str) -> bool:
        async with self._lock:
            task = self._tasks.get(run_id)
        if task is None or task.done():
            return False
        task.cancel()
        return True


ACTIVE_RUNS = ActiveRunRegistry()