Add redux actions and reducer for scheduler entries

This commit is contained in:
Ken Hibino 2020-12-02 19:44:50 -08:00
parent fbbc414bdf
commit 3e5b145883
4 changed files with 119 additions and 0 deletions

View File

@ -0,0 +1,46 @@
import { Dispatch } from "@reduxjs/toolkit";
import { listSchedulerEntries, ListSchedulerEntriesResponse } from "../api";
// List of scheduler-entry related action types.
export const LIST_SCHEDULER_ENTRIES_BEGIN = "LIST_SCHEDULER_ENTRIES_BEGIN";
export const LIST_SCHEDULER_ENTRIES_SUCCESS = "LIST_SCHEDULER_ENTRIES_SUCCESS";
export const LIST_SCHEDULER_ENTRIES_ERROR = "LIST_SCHEDULER_ENTRIES_ERROR";
interface ListSchedulerEntriesBeginAction {
type: typeof LIST_SCHEDULER_ENTRIES_BEGIN;
}
interface ListSchedulerEntriesSuccessAction {
type: typeof LIST_SCHEDULER_ENTRIES_SUCCESS;
payload: ListSchedulerEntriesResponse;
}
interface ListSchedulerEntriesErrorAction {
type: typeof LIST_SCHEDULER_ENTRIES_ERROR;
error: string; // error description
}
// Union of all scheduler-entry related actions.
export type SchedulerEntriesActionTypes =
| ListSchedulerEntriesBeginAction
| ListSchedulerEntriesSuccessAction
| ListSchedulerEntriesErrorAction;
export function listSchedulerEntriesAsync() {
return async (dispatch: Dispatch<SchedulerEntriesActionTypes>) => {
dispatch({ type: LIST_SCHEDULER_ENTRIES_BEGIN });
try {
const response = await listSchedulerEntries();
dispatch({
type: LIST_SCHEDULER_ENTRIES_SUCCESS,
payload: response,
});
} catch (error) {
console.error(error);
dispatch({
type: LIST_SCHEDULER_ENTRIES_ERROR,
error: "Could not retrieve scheduler entries",
});
}
};
}

View File

@ -37,6 +37,10 @@ export interface ListDeadTasksResponse {
stats: Queue;
}
export interface ListSchedulerEntriesResponse {
entries: SchedulerEntry[];
}
export interface Queue {
queue: string;
paused: boolean;
@ -97,6 +101,16 @@ export interface DeadTask extends BaseTask {
error_message: string;
}
export interface SchedulerEntry {
id: string;
spec: string;
task_type: string;
task_payload: { [key: string]: any };
options: string[];
next_enqueue_at: string;
prev_enqueue_at: string;
}
export interface PaginationOptions extends Record<string, number | undefined> {
size?: number; // size of the page
page?: number; // page number (1 being the first page)
@ -213,3 +227,11 @@ export async function listDeadTasks(
});
return resp.data;
}
export async function listSchedulerEntries(): Promise<ListSchedulerEntriesResponse> {
const resp = await axios({
method: "get",
url: `${BASE_URL}/scheduler_entries`,
});
return resp.data;
}

View File

@ -0,0 +1,49 @@
import {
LIST_SCHEDULER_ENTRIES_BEGIN,
LIST_SCHEDULER_ENTRIES_ERROR,
LIST_SCHEDULER_ENTRIES_SUCCESS,
SchedulerEntriesActionTypes,
} from "../actions/schedulerEntriesActions";
import { SchedulerEntry } from "../api";
interface SchedulerEntriesState {
loading: boolean;
data: SchedulerEntry[];
error: string; // error description
}
const initialState: SchedulerEntriesState = {
loading: false,
data: [],
error: "",
};
function schedulerEntriesReducer(
state = initialState,
action: SchedulerEntriesActionTypes
): SchedulerEntriesState {
switch (action.type) {
case LIST_SCHEDULER_ENTRIES_BEGIN:
return {
...state,
loading: true,
};
case LIST_SCHEDULER_ENTRIES_SUCCESS:
return {
error: "",
loading: false,
data: action.payload.entries,
};
case LIST_SCHEDULER_ENTRIES_ERROR:
// TODO: set error state
return {
...state,
loading: false,
error: action.error,
};
default:
return state;
}
}
export default schedulerEntriesReducer;

View File

@ -2,11 +2,13 @@ import { combineReducers, configureStore } from "@reduxjs/toolkit";
import settingsReducer from "./reducers/settingsReducer";
import queuesReducer from "./reducers/queuesReducer";
import tasksReducer from "./reducers/tasksReducer";
import schedulerEntriesReducer from "./reducers/schedulerEntriesReducer";
const rootReducer = combineReducers({
settings: settingsReducer,
queues: queuesReducer,
tasks: tasksReducer,
schedulerEntries: schedulerEntriesReducer,
});
// AppState is the top-level application state maintained by redux store.