Skip to content

Commit dee0e71

Browse files
author
simonrempel
committed
Adds monitoring of upload task
1 parent 0591e38 commit dee0e71

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

refinery/__init__.py

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import os.path
99
from tqdm import tqdm
1010
import spacy
11+
import time
12+
from refinery import settings
1113

1214

1315
class Client:
@@ -148,7 +150,9 @@ def get_record_export(
148150
)
149151

150152
else:
151-
msg.warn("There are no attributes that can be tokenized in this project.")
153+
msg.warn(
154+
"There are no attributes that can be tokenized in this project."
155+
)
152156

153157
if download_to is not None:
154158
df.to_json(download_to, orient="records")
@@ -214,8 +218,48 @@ def post_file_import(
214218
file_name,
215219
)
216220
if success:
217-
msg.good(f"Uploaded {path} to your project.")
218-
return True
221+
msg.good(f"Uploaded {path} to object storage.")
222+
upload_task_id = (
223+
upload_task_id.split("/")[-1]
224+
if "/" in upload_task_id
225+
else upload_task_id
226+
)
227+
self.__monitor_task(upload_task_id)
228+
219229
else:
220-
msg.fail(f"Could not upload {path} to your project.")
221-
return False
230+
msg_text = f"Could not upload {path} to your project."
231+
msg.fail(msg_text)
232+
raise exceptions.FileImportError(msg_text)
233+
234+
def __monitor_task(self, upload_task_id):
235+
do_monitoring = True
236+
idx = 0
237+
last_progress = 0.0
238+
with tqdm(
239+
total=100.00,
240+
colour="green",
241+
bar_format="{desc}: {percentage:.2f}%|{bar:10}| {n:.2f}/{total_fmt}",
242+
) as pbar:
243+
pbar.set_description_str(desc="PENDING", refresh=True)
244+
while do_monitoring:
245+
idx += 1
246+
task = self.__get_task(upload_task_id)
247+
task_progress = task.get("progress") if task.get("progress") else 0.0
248+
task_state = task.get("state") if task.get("state") else "FAILED"
249+
progress = task_progress - last_progress
250+
last_progress = task_progress
251+
pbar.update(progress)
252+
pbar.set_description_str(desc=task_state, refresh=True)
253+
if task_state == "DONE" or task_state == "FAILED":
254+
do_monitoring = False
255+
if idx >= 100:
256+
raise exceptions.FileImportError(
257+
"Timeout while upload, please check the upload progress in the UI."
258+
)
259+
time.sleep(0.5)
260+
261+
def __get_task(self, upload_task_id):
262+
api_response = api_calls.get_request(
263+
settings.get_task(self.project_id, upload_task_id), self.session_token
264+
)
265+
return api_response

refinery/settings.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,7 @@ def get_import_url(project_id: str) -> str:
4646

4747
def get_base_config(project_id: str) -> str:
4848
return f"{get_project_url(project_id)}/import/base_config"
49+
50+
51+
def get_task(project_id: str, task_id: str) -> str:
52+
return f"{get_project_url(project_id)}/import/task/{task_id}"

0 commit comments

Comments
 (0)