-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
206 additions
and
2 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,174 @@ | ||
from __future__ import annotations | ||
|
||
from typing import List, overload | ||
|
||
from . import resources, urls | ||
|
||
|
||
class Task(resources.Resource): | ||
@property | ||
def id(self) -> str: | ||
"""The task identifier. | ||
Returns | ||
------- | ||
str | ||
""" | ||
return self["id"] | ||
|
||
@property | ||
def is_finished(self) -> bool: | ||
"""The task state. | ||
If True, the task has completed. The task may have exited successfully | ||
or have failed. Inspect the exit_code to determine if the task finished | ||
successfully or not. | ||
Returns | ||
------- | ||
bool | ||
""" | ||
return self.get("finished", False) | ||
|
||
@property | ||
def output(self) -> List[str]: | ||
"""Process output. | ||
The process output produced by the task. | ||
Returns | ||
------- | ||
List[str] | ||
""" | ||
return self["output"] | ||
|
||
@property | ||
def error_code(self) -> int | None: | ||
"""The error code. | ||
The error code produced by the task. A non-zero value represent an | ||
error. A zero value represents no error. | ||
Returns | ||
------- | ||
int | None | ||
Non-zero value indicates an error. | ||
""" | ||
return self["code"] if self.is_finished else None | ||
|
||
@property | ||
def error_message(self) -> str | None: | ||
"""The error message. | ||
Returns | ||
------- | ||
str | None | ||
Human readable error message, or None on success or not finished. | ||
""" | ||
return self.get("error") if self.is_finished else None | ||
|
||
@property | ||
def result(self) -> dict | None: | ||
"""The task result. | ||
Returns | ||
------- | ||
dict | None | ||
""" | ||
return self.get("result") | ||
|
||
# CRUD Methods | ||
|
||
@overload | ||
def update(self, first: int, wait: int, **kwargs) -> None: | ||
"""Update the task. | ||
Parameters | ||
---------- | ||
first : int, default 0 | ||
Line to start output on. | ||
wait : int, default 0 | ||
Maximum number of seconds to wait for the task to complete. | ||
""" | ||
... | ||
|
||
@overload | ||
def update(self, *args, **kwargs) -> None: | ||
"""Update the task.""" | ||
... | ||
|
||
def update(self, *args, **kwargs) -> None: | ||
"""Update the task.""" | ||
params = dict(*args, **kwargs) | ||
path = f"v1/tasks/{self.id}" | ||
url = urls.append(self.config.url, path) | ||
response = self.session.get(url, params=params) | ||
result = response.json() | ||
super().update(**result) | ||
|
||
def wait_for(self, sleep: int = 1) -> None: | ||
"""Wait for the task to finish. | ||
Parameters | ||
---------- | ||
sleep : int, optional | ||
Maximum number of seconds to wait between task status checks. | ||
""" | ||
while not self.is_finished: | ||
self.update(wait=sleep) | ||
|
||
|
||
class Tasks(resources.Resources): | ||
@overload | ||
def get(self, id: str, first: int, wait: int) -> Task: | ||
"""Get a task. | ||
Parameters | ||
---------- | ||
id : str | ||
Task identifier. | ||
first : int, default 0 | ||
Line to start output on. | ||
wait : int, default 0 | ||
Maximum number of seconds to wait for the task to complete. | ||
Returns | ||
------- | ||
Task | ||
_description_ | ||
""" | ||
... | ||
|
||
@overload | ||
def get(self, id: str, *args, **kwargs) -> Task: | ||
"""Get a task. | ||
Parameters | ||
---------- | ||
id : str | ||
Task identifier. | ||
Returns | ||
------- | ||
Task | ||
""" | ||
... | ||
|
||
def get(self, id: str, *args, **kwargs) -> Task: | ||
"""Get a task. | ||
Parameters | ||
---------- | ||
id : str | ||
Task identifier. | ||
Returns | ||
------- | ||
Task | ||
""" | ||
params = dict(*args, **kwargs) | ||
path = f"v1/tasks/{id}" | ||
url = urls.append(self.config.url, path) | ||
response = self.session.get(url, params=params) | ||
result = response.json() | ||
return Task(self.config, self.session, **result) |