-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathapi.py
More file actions
324 lines (262 loc) · 9.97 KB
/
api.py
File metadata and controls
324 lines (262 loc) · 9.97 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import json
import logging
import time
from collections.abc import Callable, Generator
from contextlib import contextmanager
from datetime import timedelta
from enum import Enum
from functools import wraps
from typing import (
Annotated,
Literal,
TypeVar,
)
import httpx
from pydantic import BaseModel, Field, TypeAdapter, ValidationError
from typing_extensions import ParamSpec
from fastapi_cloud_cli import __version__
from fastapi_cloud_cli.config import Settings
from fastapi_cloud_cli.context import ctx
logger = logging.getLogger(__name__)
STREAM_LOGS_MAX_RETRIES = 3
STREAM_LOGS_TIMEOUT = timedelta(minutes=5)
class StreamLogError(Exception):
"""Raised when there's an error streaming logs (build or app logs)."""
def __init__(self, message: str, *, status_code: int | None = None) -> None:
super().__init__(message)
self.status_code = status_code
class TooManyRetriesError(Exception):
pass
class AppLogEntry(BaseModel):
timestamp: str
message: str
level: str
class BuildLogLineGeneric(BaseModel):
type: Literal["complete", "failed", "timeout", "heartbeat"]
id: str | None = None
class BuildLogLineMessage(BaseModel):
type: Literal["message"] = "message"
message: str
id: str | None = None
BuildLogLine = BuildLogLineMessage | BuildLogLineGeneric
BuildLogAdapter: TypeAdapter[BuildLogLine] = TypeAdapter(
Annotated[BuildLogLine, Field(discriminator="type")]
)
@contextmanager
def attempt(attempt_number: int) -> Generator[None, None, None]:
def _backoff() -> None:
backoff_seconds = min(2**attempt_number, 30)
logger.debug(
"Retrying in %ds (attempt %d)",
backoff_seconds,
attempt_number,
)
time.sleep(backoff_seconds)
try:
yield
except (
httpx.TimeoutException,
httpx.NetworkError,
httpx.RemoteProtocolError,
) as error:
logger.debug("Network error (will retry): %s", error)
_backoff()
except httpx.HTTPStatusError as error:
if error.response.status_code >= 500:
logger.debug(
"Server error %d (will retry): %s",
error.response.status_code,
error,
)
_backoff()
else:
# Try to get response text, but handle streaming responses gracefully
try:
error_detail = error.response.text
except Exception:
error_detail = "(response body unavailable)"
raise StreamLogError(
f"HTTP {error.response.status_code}: {error_detail}",
status_code=error.response.status_code,
) from error
P = ParamSpec("P")
T = TypeVar("T")
def attempts(
total_attempts: int = 3, timeout: timedelta = timedelta(minutes=5)
) -> Callable[
[Callable[P, Generator[T, None, None]]], Callable[P, Generator[T, None, None]]
]:
def decorator(
func: Callable[P, Generator[T, None, None]],
) -> Callable[P, Generator[T, None, None]]:
@wraps(func)
def wrapper(*args: P.args, **kwargs: P.kwargs) -> Generator[T, None, None]:
start = time.monotonic()
for attempt_number in range(total_attempts):
if time.monotonic() - start > timeout.total_seconds():
raise TimeoutError(
f"Log streaming timed out after {timeout.total_seconds():.0f}s"
)
with attempt(attempt_number):
yield from func(*args, **kwargs)
# If we get here without exception, the generator completed successfully
return
raise TooManyRetriesError(f"Failed after {total_attempts} attempts")
return wrapper
return decorator
class DeploymentStatus(str, Enum):
waiting_upload = "waiting_upload"
ready_for_build = "ready_for_build"
building = "building"
extracting = "extracting"
extracting_failed = "extracting_failed"
building_image = "building_image"
building_image_failed = "building_image_failed"
deploying = "deploying"
deploying_failed = "deploying_failed"
verifying = "verifying"
verifying_failed = "verifying_failed"
verifying_skipped = "verifying_skipped"
success = "success"
failed = "failed"
@classmethod
def to_human_readable(cls, status: "DeploymentStatus") -> str:
return {
cls.waiting_upload: "Waiting for upload",
cls.ready_for_build: "Ready for build",
cls.building: "Building",
cls.extracting: "Extracting",
cls.extracting_failed: "Extracting failed",
cls.building_image: "Building image",
cls.building_image_failed: "Build failed",
cls.deploying: "Deploying",
cls.deploying_failed: "Deploying failed",
cls.verifying: "Verifying",
cls.verifying_failed: "Verifying failed",
cls.verifying_skipped: "Verification skipped",
cls.success: "Success",
cls.failed: "Failed",
}[status]
SUCCESSFUL_STATUSES = {DeploymentStatus.success, DeploymentStatus.verifying_skipped}
FAILED_STATUSES = {
DeploymentStatus.failed,
DeploymentStatus.verifying_failed,
DeploymentStatus.deploying_failed,
DeploymentStatus.building_image_failed,
DeploymentStatus.extracting_failed,
}
TERMINAL_STATUSES = SUCCESSFUL_STATUSES | FAILED_STATUSES
POLL_INTERVAL = 2.0
POLL_TIMEOUT = timedelta(seconds=120)
POLL_MAX_RETRIES = 5
class APIClient(httpx.Client):
def __init__(self) -> None:
settings = Settings.get()
identity = ctx.get_identity()
super().__init__(
base_url=settings.base_api_url,
timeout=httpx.Timeout(20),
headers={
"Authorization": f"Bearer {identity.token}",
"User-Agent": f"fastapi-cloud-cli/{__version__}",
},
)
@attempts(STREAM_LOGS_MAX_RETRIES, STREAM_LOGS_TIMEOUT)
def stream_build_logs(
self, deployment_id: str
) -> Generator[BuildLogLine, None, None]:
last_id = None
while True:
params = {"last_id": last_id} if last_id else None
with self.stream(
"GET",
f"/deployments/{deployment_id}/build-logs",
timeout=60,
params=params,
) as response:
response.raise_for_status()
for line in response.iter_lines():
if not line or not line.strip():
continue
if log_line := self._parse_log_line(line):
if log_line.id:
last_id = log_line.id
if log_line.type == "message":
yield log_line
if log_line.type in ("complete", "failed"):
yield log_line
return
if log_line.type == "timeout":
logger.debug("Received timeout; reconnecting")
break # Breaks for loop to reconnect
else:
logger.debug("Connection closed by server unexpectedly; will retry")
raise httpx.NetworkError("Connection closed without terminal state")
time.sleep(0.5)
def _parse_log_line(self, line: str) -> BuildLogLine | None:
try:
return BuildLogAdapter.validate_json(line)
except (ValidationError, json.JSONDecodeError) as e:
logger.debug("Skipping malformed log: %s (error: %s)", line[:100], e)
return None
@attempts(STREAM_LOGS_MAX_RETRIES, STREAM_LOGS_TIMEOUT)
def stream_app_logs(
self,
app_id: str,
tail: int,
since: str,
follow: bool,
) -> Generator[AppLogEntry, None, None]:
timeout = 120 if follow else 30
with self.stream(
"GET",
f"/apps/{app_id}/logs/stream",
params={
"tail": tail,
"since": since,
"follow": follow,
},
timeout=timeout,
) as response:
response.raise_for_status()
for line in response.iter_lines():
if not line or not line.strip(): # pragma: no cover
continue
try:
data = json.loads(line)
except json.JSONDecodeError:
logger.debug("Failed to parse log line: %s", line)
continue
if data.get("type") == "heartbeat":
continue
if data.get("type") == "error":
raise StreamLogError(data.get("message", "Unknown error"))
try:
yield AppLogEntry.model_validate(data)
except ValidationError as e: # pragma: no cover
logger.debug("Failed to parse log entry: %s - %s", data, e)
continue
def poll_deployment_status(
self,
app_id: str,
deployment_id: str,
) -> DeploymentStatus:
start = time.monotonic()
error_count = 0
while True:
if time.monotonic() - start > POLL_TIMEOUT.total_seconds():
raise TimeoutError("Deployment verification timed out")
with attempt(error_count):
response = self.get(f"/apps/{app_id}/deployments/{deployment_id}")
response.raise_for_status()
status = DeploymentStatus(response.json()["status"])
error_count = 0
if status in TERMINAL_STATUSES:
return status
time.sleep(POLL_INTERVAL)
continue
error_count += 1
if error_count >= POLL_MAX_RETRIES:
raise TooManyRetriesError(
f"Failed after {POLL_MAX_RETRIES} attempts polling deployment status"
)