Skip to content

Commit

Permalink
feat(api): expose information of current processing stage and downloa…
Browse files Browse the repository at this point in the history
…d status
  • Loading branch information
xyb committed Sep 18, 2024
1 parent 633db08 commit 1a07fb5
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ The creation API will return the task object:
"total_size": 0,
"largest_file": null,
"largest_file_size": null,
"is_downloading": False,
"current_progessing_stage": "waiting_assign",
"done": false,
"failed": false,
"recoverable": false,
Expand Down
21 changes: 12 additions & 9 deletions task/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,17 @@ def get_current_step(self):
return name

@property
def current_step(self):
def current_progressing_stage(self) -> str:
return self.get_current_step()

@property
def is_downloading(self):
return self.current_step == "downloading_files"
def is_downloading(self) -> bool:
return self.current_progressing_stage in [
"downloading_files",
"downloading_samplings",
]

def get_resume_method_name(self):
def get_resume_method_name(self) -> str:
resume_methods = {
"waiting_assign": "restart",
"transferring": "restart",
Expand All @@ -304,11 +307,11 @@ def get_resume_method_name(self):
if step_name:
return resume_methods[step_name]

def inc_retry_times(self):
def inc_retry_times(self) -> None:
self.retry_times += 1
self.save()

def schedule_resume(self):
def schedule_resume(self) -> None:
if not self.failed:
return
method_name = self.get_resume_method_name()
Expand All @@ -317,12 +320,12 @@ def schedule_resume(self):
method()

@classmethod
def schedule_resume_failed(cls):
def schedule_resume_failed(cls) -> None:
for task in cls.filter_failed():
task.schedule_resume()

@property
def done(self):
def done(self) -> bool:
if self.failed:
return False
if not self.full_downloaded_at:
Expand All @@ -332,7 +335,7 @@ def done(self):
return True

@property
def recoverable(self):
def recoverable(self) -> bool:
if not self.failed:
return False

Expand Down
2 changes: 2 additions & 0 deletions task/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class Meta:
"total_size",
"largest_file",
"largest_file_size",
"is_downloading",
"current_progressing_stage",
"done",
"failed",
"recoverable",
Expand Down
37 changes: 36 additions & 1 deletion task/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,42 @@ def test_create_task(self):

self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(Task.objects.count(), 2)
assert response.json()["full_download_now"] is False
data = response.json()
assert data["full_download_now"] is False
assert data["current_progressing_stage"] == "waiting_assign"
assert data["is_downloading"] is False
assert set(data.keys()) == {
"callback",
"captcha_required",
"captcha_url",
"captcha",
"created_at",
"current_progressing_stage",
"done",
"failed",
"file_listed_at",
"finished_at",
"full_download_now",
"full_downloaded_at",
"id",
"is_downloading",
"largest_file_size",
"largest_file",
"message",
"path",
"recoverable",
"retry_times",
"sample_downloaded_at",
"sample_path",
"shared_id",
"shared_link",
"shared_password",
"started_at",
"status",
"total_files",
"total_size",
"transfer_completed_at",
}

def test_create_task_full_download_now(self):
url = reverse("task-list")
Expand Down

0 comments on commit 1a07fb5

Please sign in to comment.