-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathasync_execution.py
More file actions
144 lines (115 loc) · 4.99 KB
/
Copy pathasync_execution.py
File metadata and controls
144 lines (115 loc) · 4.99 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"""Async execution management for async commands."""
from __future__ import annotations
import asyncio
import logging
from typing import Optional, Awaitable, cast
from typing_extensions import Unpack, override
from ._types import BaseRequestOptions, LongRequestOptions
from .._client import AsyncRunloop
from .async_execution_result import AsyncExecutionResult
from ..types.devbox_async_execution_detail_view import DevboxAsyncExecutionDetailView
class _AsyncStreamingGroup:
"""Internal helper to manage background streaming tasks."""
def __init__(self, tasks: list[asyncio.Task[None]]) -> None:
self._tasks = tasks
self._logger = logging.getLogger(__name__)
async def wait(self) -> None:
results = await asyncio.gather(*self._tasks, return_exceptions=True)
self._log_results(tuple(results))
async def cancel(self) -> None:
for task in self._tasks:
task.cancel()
results = await asyncio.gather(*self._tasks, return_exceptions=True)
self._log_results(tuple(results))
def _log_results(self, results: tuple[object | BaseException | None, ...]) -> None:
for result in results:
if isinstance(result, Exception) and not isinstance(result, asyncio.CancelledError):
self._logger.debug("stream task error: %s", result)
class AsyncExecution:
"""Manages an asynchronous command execution on a devbox.
Provides coroutines to poll execution state, wait for completion, and
terminate the running process. Created by ``await devbox.cmd.exec_async()``.
Example:
>>> execution = await devbox.cmd.exec_async("python train.py")
>>> state = await execution.get_state()
>>> if state.status == "running":
... await execution.kill()
>>> result = await execution.result() # Wait for completion
>>> print(await result.stdout())
"""
def __init__(
self,
client: AsyncRunloop,
devbox_id: str,
execution: DevboxAsyncExecutionDetailView,
streaming_group: Optional[_AsyncStreamingGroup] = None,
) -> None:
self._client = client
self._devbox_id = devbox_id
self._execution_id = execution.execution_id
self._initial_result = execution
self._streaming_group = streaming_group
@override
def __repr__(self) -> str:
return f"<AsyncExecution id={self._execution_id!r}>"
@property
def execution_id(self) -> str:
"""Return the execution identifier.
:return: Unique execution ID
:rtype: str
"""
return self._execution_id
@property
def devbox_id(self) -> str:
"""Return the devbox identifier.
:return: Devbox ID where the command is running
:rtype: str
"""
return self._devbox_id
async def result(self, **options: Unpack[LongRequestOptions]) -> AsyncExecutionResult:
"""Wait for completion and return an :class:`AsyncExecutionResult`.
:param options: Optional long-running request configuration
:return: Wrapper with exit status and output helpers
:rtype: AsyncExecutionResult
"""
# Wait for both command completion and streaming to finish
awaitables: list[Awaitable[DevboxAsyncExecutionDetailView | None]] = [
self._client.devboxes.wait_for_command(
self._execution_id,
devbox_id=self._devbox_id,
statuses=["completed"],
**options,
)
]
if self._streaming_group is not None:
awaitables.append(self._streaming_group.wait())
results = await asyncio.gather(*awaitables, return_exceptions=True)
command_result = results[0]
# Extract command result (throw if it failed, ignore streaming errors)
if isinstance(command_result, Exception):
raise command_result
if self._streaming_group is not None:
self._streaming_group = None
# Streaming errors are already logged in _AsyncStreamingGroup._log_results()
final = cast(DevboxAsyncExecutionDetailView, command_result)
return AsyncExecutionResult(self._client, self._devbox_id, final)
async def get_state(self, **options: Unpack[BaseRequestOptions]) -> DevboxAsyncExecutionDetailView:
"""Fetch the latest execution state.
:param options: Optional request configuration
:return: Current execution metadata
:rtype: DevboxAsyncExecutionDetailView
"""
return await self._client.devboxes.executions.retrieve(
self._execution_id,
devbox_id=self._devbox_id,
**options,
)
async def kill(self, **options: Unpack[LongRequestOptions]) -> None:
"""Request termination of the running execution.
:param options: Optional long-running request configuration
"""
await self._client.devboxes.executions.kill(
self._execution_id,
devbox_id=self._devbox_id,
**options,
)