Skip to content

Commit

Permalink
Add task status type annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
davelopez committed Sep 20, 2023
1 parent 0daa4aa commit bea9a81
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
8 changes: 7 additions & 1 deletion client/src/schema/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8688,6 +8688,12 @@ export interface components {
| "Page"
| "StoredWorkflow"
| "Visualization";
/**
* TaskState
* @description Enum representing the possible states of a task.
* @enum {string}
*/
TaskState: "PENDING" | "STARTED" | "RETRY" | "FAILURE" | "SUCCESS";
/** ToolDataDetails */
ToolDataDetails: {
/**
Expand Down Expand Up @@ -16677,7 +16683,7 @@ export interface operations {
/** @description String indicating task state. */
200: {
content: {
"application/json": string;
"application/json": components["schemas"]["TaskState"];
};
};
/** @description Validation Error */
Expand Down
27 changes: 24 additions & 3 deletions lib/galaxy/managers/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,32 @@
ABCMeta,
abstractmethod,
)
from enum import Enum
from typing import cast
from uuid import UUID

from celery.result import AsyncResult


class TaskState(str, Enum):
"""Enum representing the possible states of a task."""

PENDING = "PENDING"
"""The task is waiting for execution."""

STARTED = "STARTED"
"""The task has been started."""

RETRY = "RETRY"
"""The task is to be retried, possibly because of failure."""

FAILURE = "FAILURE"
"""The task raised an exception, or has exceeded the retry limit."""

SUCCESS = "SUCCESS"
"""The task executed successfully."""


class AsyncTasksManager(metaclass=ABCMeta):
@abstractmethod
def is_ready(self, task_uuid: UUID) -> bool:
Expand All @@ -28,7 +49,7 @@ def has_failed(self, task_uuid: UUID) -> bool:
"""Return `True` if the task failed."""

@abstractmethod
def get_state(self, task_uuid: UUID) -> str:
def get_state(self, task_uuid: UUID) -> TaskState:
"""Returns the current state of the task as a string."""


Expand All @@ -50,9 +71,9 @@ def has_failed(self, task_uuid: UUID) -> bool:
"""Return `True` if the task failed."""
return self._get_result(task_uuid).failed()

def get_state(self, task_uuid: UUID) -> str:
def get_state(self, task_uuid: UUID) -> TaskState:
"""Returns the tasks current state as a string."""
return str(self._get_result(task_uuid).state)
return cast(TaskState, str(self._get_result(task_uuid).state))

def _get_result(self, task_uuid: UUID) -> AsyncResult:
return AsyncResult(str(task_uuid))
7 changes: 5 additions & 2 deletions lib/galaxy/webapps/galaxy/api/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import logging
from uuid import UUID

from galaxy.managers.tasks import AsyncTasksManager
from galaxy.managers.tasks import (
AsyncTasksManager,
TaskState,
)
from . import (
depends,
Router,
Expand All @@ -24,5 +27,5 @@ class FastAPITasks:
summary="Determine state of task ID",
response_description="String indicating task state.",
)
def state(self, task_id: UUID) -> str:
def state(self, task_id: UUID) -> TaskState:
return self.manager.get_state(task_id)

0 comments on commit bea9a81

Please sign in to comment.