Skip to content

Commit 0fbbfb5

Browse files
authored
Fix: Client Services Scheduler to handle 404 (#224)
* fix: `InstanceWithScheduler` to allow Optional `AllocationItem` * fix: scheduler service `get_allocation` to handle 404 * fix: instance service `get_instance_executions_info` to handle allocations being None
1 parent 30a2a2e commit 0fbbfb5

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

src/aleph/sdk/client/services/instance.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ async def _fetch(
114114
if isinstance(alloc, InstanceManual):
115115
crn_url = sanitize_url(alloc.crn_url)
116116
else:
117+
if not alloc.allocations:
118+
return str(item_hash), None
117119
crn_url = sanitize_url(alloc.allocations.node.url)
118120

119121
if not crn_url:

src/aleph/sdk/client/services/scheduler.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from typing import TYPE_CHECKING
1+
from typing import TYPE_CHECKING, Optional
22

33
import aiohttp
4+
from aiohttp import ClientResponseError
45
from aleph_message.models import ItemHash
56

67
from aleph.sdk.conf import settings
@@ -40,15 +41,18 @@ async def get_nodes(self) -> SchedulerNodes:
4041

4142
return SchedulerNodes.model_validate(raw)
4243

43-
async def get_allocation(self, vm_hash: ItemHash) -> AllocationItem:
44+
async def get_allocation(self, vm_hash: ItemHash) -> Optional[AllocationItem]:
4445
"""
4546
Fetch allocation information for a given VM hash.
4647
"""
4748
url = f"{sanitize_url(settings.SCHEDULER_URL)}/api/v0/allocation/{vm_hash}"
48-
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-
54-
return AllocationItem.model_validate(payload)
49+
try:
50+
async with aiohttp.ClientSession() as session:
51+
async with session.get(url) as resp:
52+
resp.raise_for_status()
53+
payload = await resp.json()
54+
return AllocationItem.model_validate(payload)
55+
except ClientResponseError as e:
56+
if e.status == 404: # Allocation can't be find on scheduler
57+
return None
58+
raise e

src/aleph/sdk/types.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,9 @@ class AllocationItem(BaseModel):
174174

175175
class InstanceWithScheduler(BaseModel):
176176
source: Literal["scheduler"]
177-
allocations: AllocationItem # Case Scheduler
177+
allocations: Optional[
178+
AllocationItem
179+
] # Case Scheduler (None == allocation can't be find on scheduler)
178180

179181

180182
class InstanceManual(BaseModel):

0 commit comments

Comments
 (0)