Skip to content

Commit 1e5433d

Browse files
committed
Managers now use name instead of manager_name when appropriate
1 parent 71f1144 commit 1e5433d

8 files changed

Lines changed: 134 additions & 117 deletions

File tree

alchemiscale/compute/api.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def register_computeservice(
114114
if compute_manager_id:
115115
manager_name = process_compute_manager_id_string(
116116
compute_manager_id
117-
).manager_name
117+
).name
118118
else:
119119
manager_name = None
120120

@@ -126,7 +126,13 @@ def register_computeservice(
126126
manager_name=manager_name,
127127
)
128128

129-
compute_service_id_ = n4js.register_computeservice(csreg)
129+
try:
130+
compute_service_id_ = n4js.register_computeservice(csreg)
131+
except ValueError as e:
132+
raise HTTPException(
133+
status_code=http_status.HTTP_422_UNPROCESSABLE_ENTITY,
134+
detail=str(e),
135+
)
130136

131137
return compute_service_id_
132138

@@ -412,15 +418,10 @@ def process_compute_manager_id_string(
412418
"""Try creating a ComputeManagerID from a string representation. Raise HTTPException."""
413419
try:
414420
compute_manager_id = ComputeManagerID(compute_manager_id_string)
415-
except ValueError as e:
416-
raise HTTPException(
417-
status_code=http_status.HTTP_422_UNPROCESSABLE_ENTITY,
418-
details=str(e),
419-
)
420421
except Exception as e:
421422
raise HTTPException(
422-
status_code=http_status.HTTP_500_INTERNAL_SERVER_ERROR,
423-
details=str(e),
423+
status_code=http_status.HTTP_422_UNPROCESSABLE_ENTITY,
424+
detail=str(e),
424425
)
425426

426427
return compute_manager_id
@@ -436,7 +437,7 @@ def register_computemanager(
436437

437438
now = datetime.datetime.now(tz=datetime.UTC)
438439
cm_registration = ComputeManagerRegistration(
439-
manager_name=compute_manager_id.manager_name,
440+
name=compute_manager_id.name,
440441
uuid=compute_manager_id.uuid,
441442
registered=now,
442443
last_status_update=now,
@@ -445,7 +446,14 @@ def register_computemanager(
445446
saturation=0,
446447
)
447448

448-
compute_manager_id_ = n4js.register_computemanager(cm_registration)
449+
try:
450+
compute_manager_id_ = n4js.register_computemanager(cm_registration)
451+
except ValueError as e:
452+
raise HTTPException(
453+
status_code=http_status.HTTP_422_UNPROCESSABLE_ENTITY,
454+
detail=str(e),
455+
)
456+
449457
return compute_manager_id_
450458

451459

@@ -513,12 +521,12 @@ def update_status_computemanager(
513521
except ValueError as e:
514522
raise HTTPException(
515523
status_code=http_status.HTTP_400_BAD_REQUEST,
516-
details=str(e),
524+
detail=str(e),
517525
)
518526
except Exception as e:
519527
raise HTTPException(
520528
status_code=http_status.HTTP_500_INTERNAL_SERVER_ERROR,
521-
details=str(e),
529+
detail=str(e),
522530
)
523531

524532
return compute_manager_id

alchemiscale/compute/manager.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class ComputeManager:
2626

2727
def __init__(self, settings: ComputeManagerSettings):
2828
self.settings = settings
29-
self.compute_manager_id = ComputeManagerID.new_from_manager_name(
29+
self.compute_manager_id = ComputeManagerID.new_from_name(
3030
self.settings.name
3131
)
3232
self.client = AlchemiscaleComputeManagerClient(
@@ -35,6 +35,8 @@ def __init__(self, settings: ComputeManagerSettings):
3535
key=self.settings.key,
3636
)
3737

38+
self._stop = False
39+
3840
logger = logging.getLogger("AlchemiscaleComputeManager")
3941
logger.setLevel(self.settings.loglevel)
4042

@@ -66,8 +68,7 @@ def start(self, max_cycles: int | None = None):
6668
self._stop = False
6769
try:
6870
count = 0
69-
while not self._stop:
70-
self.cycle()
71+
while self.cycle():
7172
count += 1
7273
if max_cycles and count >= max_cycles:
7374
break
@@ -90,7 +91,14 @@ def create_compute_services(self, data: dict) -> int:
9091
"""
9192
raise NotImplementedError
9293

93-
def cycle(self):
94+
def stop(self):
95+
self._stop = True
96+
97+
def cycle(self) -> bool:
98+
99+
if self._stop:
100+
return False
101+
94102
instruction, data = self.client.get_instruction(self.compute_manager_id)
95103
match instruction:
96104
case ComputeManagerInstruction.OK:
@@ -107,17 +115,17 @@ def cycle(self):
107115
f"Created {new_services} new compute service(s)"
108116
)
109117
else:
110-
self.logger.info(f"No new compute services created")
118+
self.logger.info("No new compute services created")
111119
case ComputeManagerInstruction.SKIP:
112120
total_services = len(data["compute_service_ids"])
113-
self.logger.info(f"Received skip instruction")
121+
self.logger.info("Received skip instruction")
114122
case ComputeManagerInstruction.SHUTDOWN:
115123
shutdown_message = data["message"]
116124
self.logger.info(f'Received shutdown message: "{shutdown_message}"')
117-
self._stop = True
118-
return
125+
return False
119126
self.client.update_status(
120127
self.compute_manager_id,
121128
ComputeManagerStatus.OK,
122129
saturation=total_services / self.settings.max_compute_services,
123130
)
131+
return True

alchemiscale/storage/models.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ def __init__(self, _value):
7979
if len(parts) != 6:
8080
# this currently only supports field-separated hex uuid4s
8181
raise ValueError(
82-
"ComputeManagerID must have the form `{manager_name}-{uuid}` with a field-separated hex"
82+
"ComputeManagerID must have the form `{name}-{uuid}` with a field-separated hex"
8383
)
8484

85-
self._manager_name = parts[0]
85+
self._name = parts[0]
8686
self._uuid = "-".join(parts[1:])
8787

88-
if not self.manager_name.isalnum():
88+
if not self.name.isalnum():
8989
raise ValueError("ComputeManagerID only allows alpha-numeric names")
9090

9191
try:
@@ -94,21 +94,21 @@ def __init__(self, _value):
9494
raise ValueError("Could not interpret the provided UUID.")
9595

9696
@classmethod
97-
def new_from_manager_name(cls, manager_name: str):
98-
return cls(f"{manager_name}-{uuid4()}")
97+
def new_from_name(cls, name: str):
98+
return cls(f"{name}-{uuid4()}")
9999

100100
def to_dict(self):
101-
return {"manager_name": self.manager_name, "uuid": self.uuid}
101+
return {"name": self.name, "uuid": self.uuid}
102102

103103
@classmethod
104104
def from_dict(cls, dct):
105-
manager_name = dct["manager_name"]
105+
name = dct["name"]
106106
uuid = dct["uuid"]
107-
return cls(manager_name + "-" + uuid)
107+
return cls(name + "-" + uuid)
108108

109109
@property
110-
def manager_name(self) -> str:
111-
return self._manager_name
110+
def name(self) -> str:
111+
return self._name
112112

113113
@property
114114
def uuid(self) -> str:
@@ -117,18 +117,19 @@ def uuid(self) -> str:
117117

118118
class ComputeManagerRegistration(BaseModel):
119119

120-
manager_name: str
120+
name: str
121121
uuid: str
122122
last_status_update: datetime.datetime
123123
status: str
124124
detail: str
125125
saturation: float
126+
registered: datetime.datetime
126127

127128
def __repr__(self): # pragma: no cover
128129
return f"<ComputeManagerRegistration('{str(self)}')>"
129130

130131
def __str__(self):
131-
return "-".join([self.manager_name, self.uuid])
132+
return "-".join([self.name, self.uuid])
132133

133134
def to_dict(self):
134135
return self.model_dump()
@@ -138,7 +139,7 @@ def from_dict(cls, dct):
138139
return cls(**dct)
139140

140141
def to_compute_manager_id(self):
141-
return ComputeManagerID("-".join([self.manager_name, self.uuid]))
142+
return ComputeManagerID("-".join([self.name, self.uuid]))
142143

143144

144145
class TaskProvenance(BaseModel):

0 commit comments

Comments
 (0)