Skip to content

Commit 1d93c8b

Browse files
committed
print status details while waiting
1 parent 2957cff commit 1d93c8b

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
@@ -103,11 +103,14 @@ def from_zun_container(cls, zun_container):
103103
return container
104104

105105
@property
106-
def status(self):
106+
def zun_container(self):
107107
if self.id:
108-
container = zun().containers.get(self.id)
109-
self._status = container.status
110-
return self._status
108+
self._zun_container = zun().containers.get(self.id)
109+
return self._zun_container
110+
111+
@property
112+
def status(self):
113+
return getattr(self.zun_container, "status")
111114

112115
def submit(
113116
self,
@@ -204,13 +207,48 @@ def wait(
204207
if show == "widget" and context._is_ipynb():
205208
pb.display()
206209

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

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

0 commit comments

Comments
 (0)