Skip to content

Commit 0ebec3e

Browse files
committed
print status details while waiting
1 parent 477596c commit 0ebec3e

1 file changed

Lines changed: 47 additions & 9 deletions

File tree

chi/container.py

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,14 @@ def from_zun_container(cls, zun_container):
113113
return container
114114

115115
@property
116-
def status(self):
116+
def zun_container(self):
117117
if self.id:
118-
container = zun().containers.get(self.id)
119-
self._status = container.status
120-
return self._status
118+
self._zun_container = zun().containers.get(self.id)
119+
return self._zun_container
120+
121+
@property
122+
def status(self):
123+
return getattr(self.zun_container, "status")
121124

122125
def submit(
123126
self,
@@ -211,13 +214,48 @@ def wait(
211214
if show == "widget" and context._is_ipynb():
212215
pb.display()
213216

217+
last_state = {"status": None, "detail": None, "reason": None, "since": time.perf_counter()}
218+
219+
def _print_status_same(current_status, detail, reason, elapsed):
220+
msg = f"\rStill {current_status.lower()} after {elapsed}s"
221+
if detail and detail != "None":
222+
msg += f". \n\tDetail: {detail}"
223+
if reason and reason != "None":
224+
msg += f". \n\tReason: {reason}"
225+
print(msg, flush=True)
226+
227+
def _print_status_changed(old_status, current_status, elapsed):
228+
print(f"Moved from {old_status} to {current_status} after {elapsed}s", flush=True)
229+
214230
def _callback():
215-
# self.status is a property that refreshes itself
216-
# NOTE: zun statuses are title case
217-
if self.status.upper() == status.upper() or self.status == "Error":
218-
print(f"Container has moved to status {self.status}")
231+
zun_container = self.zun_container
232+
now = time.perf_counter()
233+
current_status = getattr(zun_container, "status", None)
234+
detail = getattr(zun_container, "status_detail", None)
235+
reason = getattr(zun_container, "status_reason", None)
236+
237+
elapsed = int(now - last_state["since"])
238+
239+
if last_state["status"] is None:
240+
print(f"Initial state: {current_status}", flush=True)
241+
elif current_status != last_state["status"]:
242+
_print_status_changed(last_state["status"], current_status, elapsed)
243+
else:
244+
if (detail, reason) != (last_state["detail"], last_state["reason"]):
245+
_print_status_same(current_status, detail, reason, elapsed)
246+
last_state["detail"] = detail
247+
last_state["reason"] = reason
248+
return
249+
250+
last_state["status"] = current_status
251+
last_state["detail"] = detail
252+
last_state["reason"] = reason
253+
last_state["since"] = now
254+
255+
if current_status == "Error":
256+
return True
257+
if current_status.upper() == status.upper():
219258
return True
220-
return False
221259

222260
res = pb.wait(_callback, 2 * 60, timeout)
223261
if not res:

0 commit comments

Comments
 (0)