|
1 |
| -from typing import TYPE_CHECKING |
| 1 | +from typing import TYPE_CHECKING, Optional |
2 | 2 |
|
3 | 3 | import aiohttp
|
| 4 | +from aiohttp import ClientResponseError |
4 | 5 | from aleph_message.models import ItemHash
|
5 | 6 |
|
6 | 7 | from aleph.sdk.conf import settings
|
@@ -39,15 +40,18 @@ async def get_nodes(self) -> SchedulerNodes:
|
39 | 40 |
|
40 | 41 | return SchedulerNodes.model_validate(raw)
|
41 | 42 |
|
42 |
| - async def get_allocation(self, vm_hash: ItemHash) -> AllocationItem: |
| 43 | + async def get_allocation(self, vm_hash: ItemHash) -> Optional[AllocationItem]: |
43 | 44 | """
|
44 | 45 | Fetch allocation information for a given VM hash.
|
45 | 46 | """
|
46 | 47 | url = f"{sanitize_url(settings.SCHEDULER_URL)}/api/v0/allocation/{vm_hash}"
|
47 |
| - |
48 |
| - async with aiohttp.ClientSession() as session: |
49 |
| - async with session.get(url) as resp: |
50 |
| - resp.raise_for_status() |
51 |
| - payload = await resp.json() |
52 |
| - |
53 |
| - return AllocationItem.model_validate(payload) |
| 48 | + try: |
| 49 | + async with aiohttp.ClientSession() as session: |
| 50 | + async with session.get(url) as resp: |
| 51 | + resp.raise_for_status() |
| 52 | + payload = await resp.json() |
| 53 | + return AllocationItem.model_validate(payload) |
| 54 | + except ClientResponseError as e: |
| 55 | + if e.status == 404: # Allocation can't be find on scheduler |
| 56 | + return None |
| 57 | + raise e |
0 commit comments