Add reducer and actions related to GetTaskInfo

This commit is contained in:
Ken Hibino
2021-07-18 06:06:51 -07:00
parent 8ff8ec57bc
commit 0f1e5ec78b
3 changed files with 104 additions and 0 deletions

View File

@@ -47,11 +47,16 @@ import {
archivePendingTask,
batchArchivePendingTasks,
archiveAllPendingTasks,
TaskInfo,
getTaskInfo,
} from "../api";
import { Dispatch } from "redux";
import { toErrorString, toErrorStringWithHttpStatus } from "../utils";
// List of tasks related action types.
export const GET_TASK_INFO_BEGIN = "GET_TASK_INFO_BEGIN";
export const GET_TASK_INFO_SUCCESS = "GET_TASK_INFO_SUCCESS";
export const GET_TASK_INFO_ERROR = "GET_TASK_INFO_ERROR";
export const LIST_ACTIVE_TASKS_BEGIN = "LIST_ACTIVE_TASKS_BEGIN";
export const LIST_ACTIVE_TASKS_SUCCESS = "LIST_ACTIVE_TASKS_SUCCESS";
export const LIST_ACTIVE_TASKS_ERROR = "LIST_ACTIVE_TASKS_ERROR";
@@ -209,6 +214,20 @@ export const DELETE_ALL_ARCHIVED_TASKS_SUCCESS =
export const DELETE_ALL_ARCHIVED_TASKS_ERROR =
"DELETE_ALL_ARCHIVED_TASKS_ERROR";
interface GetTaskInfoBeginAction {
type: typeof GET_TASK_INFO_BEGIN;
}
interface GetTaskInfoErrorAction {
type: typeof GET_TASK_INFO_ERROR;
error: string; // error description
}
interface GetTaskInfoSuccessAction {
type: typeof GET_TASK_INFO_SUCCESS;
payload: TaskInfo;
}
interface ListActiveTasksBeginAction {
type: typeof LIST_ACTIVE_TASKS_BEGIN;
queue: string;
@@ -894,6 +913,9 @@ interface DeleteAllArchivedTasksErrorAction {
// Union of all tasks related action types.
export type TasksActionTypes =
| GetTaskInfoBeginAction
| GetTaskInfoErrorAction
| GetTaskInfoSuccessAction
| ListActiveTasksBeginAction
| ListActiveTasksSuccessAction
| ListActiveTasksErrorAction
@@ -1009,6 +1031,25 @@ export type TasksActionTypes =
| DeleteAllArchivedTasksSuccessAction
| DeleteAllArchivedTasksErrorAction;
export function getTaskInfoAsync(qname: string, id: string) {
return async (dispatch: Dispatch<TasksActionTypes>) => {
dispatch({ type: GET_TASK_INFO_BEGIN });
try {
const response = await getTaskInfo(qname, id);
dispatch({
type: GET_TASK_INFO_SUCCESS,
payload: response,
})
} catch (error) {
console.error("getTaskInfoAsync: ", toErrorStringWithHttpStatus(error));
dispatch({
type: GET_TASK_INFO_ERROR,
error: toErrorString(error),
})
}
}
}
export function listActiveTasksAsync(
qname: string,
pageOpts?: PaginationOptions

View File

@@ -245,6 +245,19 @@ interface BaseTask {
payload: string;
}
export interface TaskInfo {
id: string;
queue: string;
type: string;
max_retry: number;
retried: number;
last_failed_at: string;
error_message: string;
next_process_at: string;
timeout_seconds: number;
deadline: string;
}
export interface ActiveTask extends BaseTask {
id: string;
queue: string;
@@ -369,6 +382,15 @@ export async function listQueueStats(): Promise<ListQueueStatsResponse> {
return resp.data;
}
export async function getTaskInfo(qname: string, id: string): Promise<TaskInfo> {
const url = `${BASE_URL}/queues/${qname}/tasks/${id}`;
const resp = await axios({
method: "get",
url,
});
return resp.data;
}
export async function listActiveTasks(
qname: string,
pageOpts?: PaginationOptions

View File

@@ -114,6 +114,9 @@ import {
BATCH_DELETE_PENDING_TASKS_SUCCESS,
BATCH_ARCHIVE_PENDING_TASKS_ERROR,
BATCH_DELETE_PENDING_TASKS_ERROR,
GET_TASK_INFO_BEGIN,
GET_TASK_INFO_ERROR,
GET_TASK_INFO_SUCCESS,
} from "../actions/tasksActions";
import {
ActiveTask,
@@ -121,6 +124,7 @@ import {
PendingTask,
RetryTask,
ScheduledTask,
TaskInfo,
} from "../api";
export interface ActiveTaskExtended extends ActiveTask {
@@ -193,6 +197,11 @@ interface TasksState {
error: string;
data: ArchivedTaskExtended[];
};
taskInfo: {
loading: boolean;
error: string;
data?: TaskInfo;
},
}
const initialState: TasksState = {
@@ -231,6 +240,10 @@ const initialState: TasksState = {
error: "",
data: [],
},
taskInfo: {
loading: false,
error: "",
}
};
function tasksReducer(
@@ -238,6 +251,34 @@ function tasksReducer(
action: TasksActionTypes
): TasksState {
switch (action.type) {
case GET_TASK_INFO_BEGIN:
return {
...state,
taskInfo: {
loading: true,
error: "",
},
}
case GET_TASK_INFO_ERROR:
return {
...state,
taskInfo: {
loading: false,
error: action.error,
},
};
case GET_TASK_INFO_SUCCESS:
return {
...state,
taskInfo: {
loading: false,
error: "",
data: action.payload,
},
};
case LIST_ACTIVE_TASKS_BEGIN:
return {
...state,