Skip to content

Commit 66e3658

Browse files
authored
Start implementing nowait parameter for stop engine (#115)
1 parent d241cca commit 66e3658

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

src/firebolt/model/engine.py

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ class EngineKey(FireboltBaseModel):
3838
engine_id: str
3939

4040

41+
def wait(seconds: int, timeout_time: float, error_message: str, verbose: bool) -> None:
42+
time.sleep(seconds)
43+
if time.time() > timeout_time:
44+
raise TimeoutError(error_message)
45+
if verbose:
46+
print(".", end="")
47+
48+
4149
class EngineSettings(FireboltBaseModel):
4250
"""
4351
Engine Settings.
@@ -204,13 +212,6 @@ def start(
204212
"""
205213
timeout_time = time.time() + wait_timeout_seconds
206214

207-
def wait(seconds: int, error_message: str) -> None:
208-
time.sleep(seconds)
209-
if time.time() > timeout_time:
210-
raise TimeoutError(error_message)
211-
if verbose:
212-
print(".", end="")
213-
214215
engine = self.get_latest()
215216
if (
216217
engine.current_status_summary
@@ -238,9 +239,11 @@ def wait(seconds: int, error_message: str) -> None:
238239
):
239240
wait(
240241
seconds=5,
242+
timeout_time=timeout_time,
241243
error_message=f"Engine "
242244
f"(engine_id={engine.engine_id}, name={engine.name}) "
243245
f"did not stop within {wait_timeout_seconds} seconds.",
246+
verbose=True,
244247
)
245248
engine = engine.get_latest()
246249

@@ -260,7 +263,9 @@ def wait(seconds: int, error_message: str) -> None:
260263
}:
261264
wait(
262265
seconds=5,
266+
timeout_time=timeout_time,
263267
error_message=f"Could not start engine within {wait_timeout_seconds} seconds.", # noqa: E501
268+
verbose=verbose,
264269
)
265270
previous_status_summary = engine.current_status_summary
266271
engine = engine.get_latest()
@@ -283,18 +288,38 @@ def _send_start(self) -> Engine:
283288
)
284289

285290
@check_attached_to_database
286-
def stop(self) -> Engine:
291+
def stop(
292+
self, wait_for_stop: bool = False, wait_timeout_seconds: int = 3600
293+
) -> Engine:
287294
"""Stop an Engine running on Firebolt."""
295+
timeout_time = time.time() + wait_timeout_seconds
296+
288297
response = self._service.client.post(
289298
url=ACCOUNT_ENGINE_STOP_URL.format(
290299
account_id=self._service.account_id, engine_id=self.engine_id
291300
)
292301
)
293302
logger.info(f"Stopping Engine (engine_id={self.engine_id}, name={self.name})")
294-
return Engine.parse_obj_with_service(
303+
304+
engine = Engine.parse_obj_with_service(
295305
obj=response.json()["engine"], engine_service=self._service
296306
)
297307

308+
while wait_for_stop and engine.current_status_summary not in {
309+
EngineStatusSummary.ENGINE_STATUS_SUMMARY_STOPPED,
310+
EngineStatusSummary.ENGINE_STATUS_SUMMARY_FAILED,
311+
}:
312+
wait(
313+
seconds=5,
314+
timeout_time=timeout_time,
315+
error_message=f"Could not stop engine within {wait_timeout_seconds} seconds.", # noqa: E501
316+
verbose=False,
317+
)
318+
319+
engine = engine.get_latest()
320+
321+
return engine
322+
298323
def delete(self) -> Engine:
299324
"""Delete an Engine from Firebolt."""
300325
response = self._service.client.delete(

0 commit comments

Comments
 (0)