-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: API documentation for Task Result type #524
Merged
kairoaraujo
merged 4 commits into
repository-service-tuf:main
from
kairoaraujo:fix_documentation_api
Feb 9, 2024
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9d7310c
fix: API docs Task and improve consistency
kairoaraujo cb5bf33
docs: Include descriptions in the Swagger (Model)
kairoaraujo ddd900a
fixup! fix: API docs Task and improve consistency
kairoaraujo c55f15c
fixup! fixup! fix: API docs Task and improve consistency
kairoaraujo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
|
||
import enum | ||
from datetime import datetime | ||
from typing import Any, Dict, Optional, Union | ||
from typing import Any, Dict, Optional | ||
|
||
from celery import states | ||
from pydantic import BaseModel, Field | ||
|
@@ -42,25 +42,24 @@ class GetParameters(BaseModel): | |
task_id: str | ||
|
||
|
||
class TaskDetails(BaseModel): | ||
message: str = Field(description="Result detail description") | ||
class TaskResult(BaseModel): | ||
message: Optional[str] = Field(description="Result detail description") | ||
error: Optional[str] = Field( | ||
description=( | ||
"If the task status result is `False` shows an error message" | ||
) | ||
) | ||
details: Optional[Dict[str, Any]] = Field( | ||
description="Any releavant information from task" | ||
status: Optional[bool] = Field( | ||
description="Task result status. `True` Success | `False` Failure", | ||
exclude=True, | ||
) | ||
|
||
|
||
class TaskResult(BaseModel): | ||
status: bool = Field( | ||
description="Task result status. `True` Success | `False` Failure" | ||
task: Optional[TaskName] = Field(description="Task name by worker") | ||
last_update: Optional[datetime] = Field( | ||
description="Last time task was updated" | ||
) | ||
details: Optional[Dict[str, Any]] = Field( | ||
description="Relevant result details" | ||
) | ||
task: TaskName = Field(description="Task name by worker") | ||
last_update: datetime = Field(description="Last time task was updated") | ||
details: TaskDetails = Field(description="Relevant result details") | ||
|
||
|
||
class TasksData(BaseModel): | ||
|
@@ -70,21 +69,22 @@ class TasksData(BaseModel): | |
"The Celery task state. Note: It isn't the task result status.\n\n" | ||
"`PENDING`: Task state is unknown (assumed pending since you know " | ||
"the id).\n\n" | ||
"`RECEIVED`: Task received by a worker (only used in events).\n\n" | ||
"`STARTED`: Task started by a worker.\n\n" | ||
"`RUNNING`: Task is running.\n\n" | ||
"`RECEIVED`: Task received by a RSTUF Worker (only used in " | ||
"events).\n\n" | ||
"`SUCCESS`: Task succeeded.\n\n" | ||
"`FAILURE`: Task failed.\n\n" | ||
"`STARTED`: Task started by a RSTUF Worker.\n\n" | ||
"`RUNNING`: Task is running on RSTUF Worker.\n\n" | ||
"`FAILURE`: Task failed (unexpected).\n\n" | ||
"`REVOKED`: Task revoked.\n\n" | ||
"`RETRY`: Task is waiting for retry.\n\n" | ||
"`ERRORED`: Task errored. RSTUF identified an error while " | ||
"processing the task.\n\n" | ||
"`REVOKED`: Task revoked.\n\n" | ||
"`REJECTED`: Task was rejected (only used in events).\n\n" | ||
"`IGNORED`: Task was ignored." | ||
) | ||
) | ||
result: Optional[Union[Any, TaskResult]] = Field( | ||
description=( | ||
"Task result details (state `SUCCESS` uses schema `TaskResult)" | ||
) | ||
result: Optional[TaskResult] = Field( | ||
description=("Task result if available.") | ||
) | ||
|
||
|
||
|
@@ -99,7 +99,6 @@ class Config: | |
"state": TaskState.SUCCESS, | ||
"result": { | ||
"task": TaskName.ADD_TARGETS, | ||
"status": True, | ||
"last_update": "2023-11-17T09:54:15.762882", | ||
"message": "Target(s) Added", | ||
"details": { | ||
|
@@ -133,9 +132,16 @@ def get(task_id: str) -> Response: | |
task_state = task.state | ||
task_result = task.result | ||
|
||
# Celery FAILURE task, we include the task result (exception) as an error | ||
# and default message as critical failure executing the task. | ||
if isinstance(task.result, Exception): | ||
task_result = {"message": str(task.result)} | ||
elif task_state == TaskState.SUCCESS and not task_result.get( | ||
task_result = { | ||
"message": f"Task failure: {str(task.result)}", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: IMO you can omit "Task failure: ". The info is redundant with the state. |
||
} | ||
|
||
# If the task state is SUCCESS and the task result is False we considere | ||
# it an errored task. | ||
if task_state == TaskState.SUCCESS and not task_result.get( | ||
"status", False | ||
): | ||
task_state = TaskState.ERRORED | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this API user facing documentation? They don't know about task status, right?