|
22 | 22 | item and parse it as an item
|
23 | 23 | """
|
24 | 24 |
|
| 25 | +import asyncio |
25 | 26 | import logging
|
26 | 27 | import tempfile
|
27 | 28 | import traceback
|
|
30 | 31 | from functools import lru_cache
|
31 | 32 | from json import JSONDecodeError
|
32 | 33 | from pathlib import Path
|
33 |
| -from typing import AsyncIterator, List, Optional |
| 34 | +from typing import Any, AsyncIterator, List, Optional |
34 | 35 | from urllib.parse import ParseResult, urlparse
|
35 | 36 |
|
36 | 37 | import dockerflow.checks
|
|
42 | 43 | logger = logging.getLogger(__name__)
|
43 | 44 |
|
44 | 45 |
|
| 46 | +async def async_iter(iter: AsyncIterator[Any]) -> list[Any]: |
| 47 | + return [item async for item in iter] |
| 48 | + |
| 49 | + |
45 | 50 | class QueueItemRetrievalError(Exception):
|
46 | 51 | pass
|
47 | 52 |
|
@@ -160,6 +165,9 @@ def __init__(self, location):
|
160 | 165 | self.location = Path(location)
|
161 | 166 | self.location.mkdir(parents=True, exist_ok=True)
|
162 | 167 |
|
| 168 | + def __repr__(self) -> str: |
| 169 | + return f"FileBackend({self.location})" |
| 170 | + |
163 | 171 | def ping(self):
|
164 | 172 | try:
|
165 | 173 | with tempfile.TemporaryDirectory(dir=self.location) as temp_dir:
|
@@ -222,7 +230,7 @@ async def get(self, bug_id: int) -> AsyncIterator[QueueItem]:
|
222 | 230 | yield QueueItem.parse_file(path)
|
223 | 231 | except (JSONDecodeError, ValidationError) as e:
|
224 | 232 | raise QueueItemRetrievalError(
|
225 |
| - "Unable to load item at path %s from queue", str(path) |
| 233 | + f"Unable to load item at path {path} from queue" |
226 | 234 | ) from e
|
227 | 235 |
|
228 | 236 | async def get_all(self) -> dict[int, AsyncIterator[QueueItem]]:
|
@@ -253,15 +261,31 @@ def ready(self) -> list[dockerflow.checks.CheckMessage]:
|
253 | 261 | TODO: Convert to an async method when Dockerflow's FastAPI integration
|
254 | 262 | can run check asynchronously
|
255 | 263 | """
|
256 |
| - |
| 264 | + results = [] |
257 | 265 | ping_result = self.backend.ping()
|
258 | 266 | if ping_result is False:
|
259 |
| - return [ |
| 267 | + results.append( |
260 | 268 | dockerflow.checks.Error(
|
261 |
| - f"queue with f{str(self.backend)} backend unavailable" |
| 269 | + f"queue with {str(self.backend)} unavailable", |
| 270 | + hint="with FileBackend, check that folder is writable", |
| 271 | + id="queue.backend.ping", |
262 | 272 | )
|
263 |
| - ] |
264 |
| - return [] |
| 273 | + ) |
| 274 | + |
| 275 | + try: |
| 276 | + bugs_items = asyncio.run(self.retrieve()) |
| 277 | + for items in bugs_items.values(): |
| 278 | + asyncio.run(async_iter(items)) |
| 279 | + except Exception as exc: |
| 280 | + results.append( |
| 281 | + dockerflow.checks.Error( |
| 282 | + f"queue with {str(self.backend)} cannot be retrieved", |
| 283 | + hint=f"invalid data: {exc}", |
| 284 | + id="queue.backend.retrieve", |
| 285 | + ) |
| 286 | + ) |
| 287 | + |
| 288 | + return results |
265 | 289 |
|
266 | 290 | async def postpone(self, payload: bugzilla.WebhookRequest) -> None:
|
267 | 291 | """
|
|
0 commit comments