Skip to content

Commit db87288

Browse files
committed
Add option for okdata status to wait
Add a `--watch` option to the `okdata status` to watch the status of a dataset upload.
1 parent d080f2d commit db87288

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## ?.?.?
2+
3+
* The `okdata status` command can now be set to watch the status of a dataset
4+
upload with the `--watch` option.
5+
16
## 3.1.0
27

38
* Added support for Python 3.12.

okdata/cli/commands/datasets/datasets.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,8 @@ def upload_file(self, source, target):
354354
summary = [f"Uploaded file to dataset: {dataset_id}", str(out)]
355355
if res["trace_id"]:
356356
summary += [
357-
"\nYou can check the data processing status by running:\n",
358-
f" okdata status {res['trace_id']}",
357+
"\nYou can watch the data processing status by running:\n",
358+
f" okdata status {res['trace_id']} --watch",
359359
]
360360
self.print("\n".join(summary))
361361

okdata/cli/commands/status.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from time import sleep
2+
13
from okdata.sdk.status import Status
24

35
from okdata.cli.command import BaseCommand, BASE_COMMAND_OPTIONS
@@ -8,15 +10,17 @@ class StatusCommand(BaseCommand):
810
__doc__ = f"""Oslo :: Status
911
1012
Usage:
11-
okdata status <trace_id> [options --history]
13+
okdata status <trace_id> [options]
1214
1315
Examples:
1416
okdata status trace-id-from-system
15-
okdata status trace-id-from-system --format=json | jq ".done"
17+
okdata status trace-id-from-system --watch
1618
okdata status trace-id-from-system --history
19+
okdata status trace-id-from-system --format=json | jq ".done"
1720
1821
Options:{BASE_COMMAND_OPTIONS}
1922
--history
23+
--watch
2024
"""
2125

2226
def __init__(self):
@@ -28,7 +32,7 @@ def login(self):
2832
def handler(self):
2933
self.log.info("StatusCommand.handler()")
3034
if self.arg("trace_id"):
31-
self.status_for_id()
35+
self.status_for_id(self.arg("trace_id"))
3236
else:
3337
self.print("Invalid command")
3438

@@ -84,11 +88,35 @@ def full_history_for_status(self, trace_id, trace_events):
8488
{"error": 1, "message": "No trace events found"},
8589
)
8690

87-
def status_for_id(self):
88-
trace_id = self.arg("trace_id")
91+
def get_trace_events(self, trace_id):
8992
self.log.info(f"Looking up status: {trace_id}")
9093
trace_events = self.sdk.get_status(trace_id)
91-
trace_events = StatusCommand.get_error_messages(trace_events)
94+
return StatusCommand.get_error_messages(trace_events)
95+
96+
def wait_until_done(self, trace_id):
97+
"""Wait until status for `trace_id` is ready, then return the trace."""
98+
trace_events = self.get_trace_events(trace_id)
99+
trace_status = StatusCommand.find_latest_event(trace_events)["trace_status"]
100+
101+
if trace_status != "FINISHED":
102+
print("Waiting for upload to finish", end="", flush=True)
103+
104+
while trace_status != "FINISHED":
105+
print(".", end="", flush=True)
106+
sleep(2)
107+
trace_events = self.get_trace_events(trace_id)
108+
trace_status = StatusCommand.find_latest_event(trace_events)["trace_status"]
109+
print()
110+
111+
return trace_events
112+
113+
def status_for_id(self, trace_id):
114+
trace_events = (
115+
self.wait_until_done(trace_id)
116+
if self.opt("watch")
117+
else self.get_trace_events(trace_id)
118+
)
119+
92120
if self.opt("history"):
93121
self.full_history_for_status(trace_id, trace_events)
94122
else:

0 commit comments

Comments
 (0)