File size: 2,560 Bytes
851e5ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""Carry download recovery notices from blocking providers to task progress."""

import asyncio
from contextvars import ContextVar
from enum import Enum
from typing import Any, Awaitable, Callable, Optional, TypeVar

from .utils.async_helpers import run_in_thread


class DownloadRecovery(str, Enum):
    CONNECTION = "Retrying video download with another connection..."
    AUDIO = "Recovering video audio..."
    FULL_VIDEO = "Recovering the full video download..."
    BACKUP = "Using a backup video download option..."
    ANOTHER_BACKUP = "Trying another video download recovery option..."


_listener: ContextVar[Optional[Callable[[DownloadRecovery], None]]] = ContextVar(
    "download_recovery_listener", default=None,
)
T = TypeVar("T")
ProgressCallback = Callable[[int, str, str], Awaitable[None]]


def report_download_recovery(recovery: DownloadRecovery) -> None:
    listener = _listener.get()
    if listener is not None:
        listener(recovery)


async def run_download_in_thread(
    func: Callable[..., T], *args: Any,
    progress_callback: Optional[ProgressCallback] = None,
    progress: int = 10, **kwargs: Any,
) -> T:
    """Persist notices on the event loop while the provider runs in a thread.

    The listener belongs only to this operation. Callbacks finish before the
    next pipeline stage, and never use an async database session in a thread.
    """
    if progress_callback is None:
        return await run_in_thread(func, *args, **kwargs)

    loop = asyncio.get_running_loop()
    updates: asyncio.Queue[Optional[DownloadRecovery]] = asyncio.Queue()
    closed = False

    def send(recovery: DownloadRecovery) -> None:
        if not closed:
            loop.call_soon_threadsafe(updates.put_nowait, recovery)

    def execute() -> T:
        token = _listener.set(send)
        try:
            return func(*args, **kwargs)
        finally:
            _listener.reset(token)

    operation = asyncio.create_task(run_in_thread(execute))
    # Thread notices are queued before the operation's completion callback.
    operation.add_done_callback(lambda _: updates.put_nowait(None))
    previous = None
    try:
        while (recovery := await updates.get()) is not None:
            if recovery != previous:
                await progress_callback(progress, recovery.value, "processing")
                previous = recovery
        return await operation
    finally:
        closed = True
        if not operation.done():
            operation.cancel()
        await asyncio.gather(operation, return_exceptions=True)