import { cancelActiveTask, deleteRetryTask, listActiveTasks, ListActiveTasksResponse, listDeadTasks, ListDeadTasksResponse, listPendingTasks, ListPendingTasksResponse, listRetryTasks, ListRetryTasksResponse, listScheduledTasks, ListScheduledTasksResponse, PaginationOptions, } from "../api"; import { Dispatch } from "redux"; // List of tasks related action types. 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"; export const LIST_PENDING_TASKS_BEGIN = "LIST_PENDING_TASKS_BEGIN"; export const LIST_PENDING_TASKS_SUCCESS = "LIST_PENDING_TASKS_SUCCESS"; export const LIST_PENDING_TASKS_ERROR = "LIST_PENDING_TASKS_ERROR"; export const LIST_SCHEDULED_TASKS_BEGIN = "LIST_SCHEDULED_TASKS_BEGIN"; export const LIST_SCHEDULED_TASKS_SUCCESS = "LIST_SCHEDULED_TASKS_SUCCESS"; export const LIST_SCHEDULED_TASKS_ERROR = "LIST_SCHEDULED_TASKS_ERROR"; export const LIST_RETRY_TASKS_BEGIN = "LIST_RETRY_TASKS_BEGIN"; export const LIST_RETRY_TASKS_SUCCESS = "LIST_RETRY_TASKS_SUCCESS"; export const LIST_RETRY_TASKS_ERROR = "LIST_RETRY_TASKS_ERROR"; export const LIST_DEAD_TASKS_BEGIN = "LIST_DEAD_TASKS_BEGIN"; export const LIST_DEAD_TASKS_SUCCESS = "LIST_DEAD_TASKS_SUCCESS"; export const LIST_DEAD_TASKS_ERROR = "LIST_DEAD_TASKS_ERROR"; export const CANCEL_ACTIVE_TASK_BEGIN = "CANCEL_ACTIVE_TASK_BEGIN"; export const CANCEL_ACTIVE_TASK_SUCCESS = "CANCEL_ACTIVE_TASK_SUCCESS"; export const CANCEL_ACTIVE_TASK_ERROR = "CANCEL_ACTIVE_TASK_ERROR"; export const DELETE_RETRY_TASK_BEGIN = "DELETE_RETRY_TASK_BEGIN"; export const DELETE_RETRY_TASK_SUCCESS = "DELETE_RETRY_TASK_SUCCESS"; export const DELETE_RETRY_TASK_ERROR = "DELETE_RETRY_TASK_ERROR"; interface ListActiveTasksBeginAction { type: typeof LIST_ACTIVE_TASKS_BEGIN; queue: string; } interface ListActiveTasksSuccessAction { type: typeof LIST_ACTIVE_TASKS_SUCCESS; queue: string; payload: ListActiveTasksResponse; } interface ListActiveTasksErrorAction { type: typeof LIST_ACTIVE_TASKS_ERROR; queue: string; error: string; // error description } interface ListPendingTasksBeginAction { type: typeof LIST_PENDING_TASKS_BEGIN; queue: string; } interface ListPendingTasksSuccessAction { type: typeof LIST_PENDING_TASKS_SUCCESS; queue: string; payload: ListPendingTasksResponse; } interface ListPendingTasksErrorAction { type: typeof LIST_PENDING_TASKS_ERROR; queue: string; error: string; // error description } interface ListScheduledTasksBeginAction { type: typeof LIST_SCHEDULED_TASKS_BEGIN; queue: string; } interface ListScheduledTasksSuccessAction { type: typeof LIST_SCHEDULED_TASKS_SUCCESS; queue: string; payload: ListScheduledTasksResponse; } interface ListScheduledTasksErrorAction { type: typeof LIST_SCHEDULED_TASKS_ERROR; queue: string; error: string; // error description } interface ListRetryTasksBeginAction { type: typeof LIST_RETRY_TASKS_BEGIN; queue: string; } interface ListRetryTasksSuccessAction { type: typeof LIST_RETRY_TASKS_SUCCESS; queue: string; payload: ListRetryTasksResponse; } interface ListRetryTasksErrorAction { type: typeof LIST_RETRY_TASKS_ERROR; queue: string; error: string; // error description } interface ListDeadTasksBeginAction { type: typeof LIST_DEAD_TASKS_BEGIN; queue: string; } interface ListDeadTasksSuccessAction { type: typeof LIST_DEAD_TASKS_SUCCESS; queue: string; payload: ListDeadTasksResponse; } interface ListDeadTasksErrorAction { type: typeof LIST_DEAD_TASKS_ERROR; queue: string; error: string; // error description } interface CancelActiveTaskBeginAction { type: typeof CANCEL_ACTIVE_TASK_BEGIN; queue: string; taskId: string; } interface CancelActiveTaskSuccessAction { type: typeof CANCEL_ACTIVE_TASK_SUCCESS; queue: string; taskId: string; } interface CancelActiveTaskErrorAction { type: typeof CANCEL_ACTIVE_TASK_ERROR; queue: string; taskId: string; error: string; } interface DeleteRetryTaskBeginAction { type: typeof DELETE_RETRY_TASK_BEGIN; queue: string; taskKey: string; } interface DeleteRetryTaskSuccessAction { type: typeof DELETE_RETRY_TASK_SUCCESS; queue: string; taskKey: string; } interface DeleteRetryTaskErrorAction { type: typeof DELETE_RETRY_TASK_ERROR; queue: string; taskKey: string; error: string; } // Union of all tasks related action types. export type TasksActionTypes = | ListActiveTasksBeginAction | ListActiveTasksSuccessAction | ListActiveTasksErrorAction | ListPendingTasksBeginAction | ListPendingTasksSuccessAction | ListPendingTasksErrorAction | ListScheduledTasksBeginAction | ListScheduledTasksSuccessAction | ListScheduledTasksErrorAction | ListRetryTasksBeginAction | ListRetryTasksSuccessAction | ListRetryTasksErrorAction | ListDeadTasksBeginAction | ListDeadTasksSuccessAction | ListDeadTasksErrorAction | CancelActiveTaskBeginAction | CancelActiveTaskSuccessAction | CancelActiveTaskErrorAction | DeleteRetryTaskBeginAction | DeleteRetryTaskSuccessAction | DeleteRetryTaskErrorAction; export function listActiveTasksAsync( qname: string, pageOpts?: PaginationOptions ) { return async (dispatch: Dispatch) => { dispatch({ type: LIST_ACTIVE_TASKS_BEGIN, queue: qname }); try { const response = await listActiveTasks(qname, pageOpts); dispatch({ type: LIST_ACTIVE_TASKS_SUCCESS, queue: qname, payload: response, }); } catch { dispatch({ type: LIST_ACTIVE_TASKS_ERROR, queue: qname, error: `Could not retreive active tasks data for queue: ${qname}`, }); } }; } export function listPendingTasksAsync( qname: string, pageOpts?: PaginationOptions ) { return async (dispatch: Dispatch) => { dispatch({ type: LIST_PENDING_TASKS_BEGIN, queue: qname }); try { const response = await listPendingTasks(qname, pageOpts); dispatch({ type: LIST_PENDING_TASKS_SUCCESS, queue: qname, payload: response, }); } catch { dispatch({ type: LIST_PENDING_TASKS_ERROR, queue: qname, error: `Could not retreive pending tasks data for queue: ${qname}`, }); } }; } export function listScheduledTasksAsync( qname: string, pageOpts?: PaginationOptions ) { return async (dispatch: Dispatch) => { dispatch({ type: LIST_SCHEDULED_TASKS_BEGIN, queue: qname }); try { const response = await listScheduledTasks(qname, pageOpts); dispatch({ type: LIST_SCHEDULED_TASKS_SUCCESS, queue: qname, payload: response, }); } catch { dispatch({ type: LIST_SCHEDULED_TASKS_ERROR, queue: qname, error: `Could not retreive scheduled tasks data for queue: ${qname}`, }); } }; } export function listRetryTasksAsync( qname: string, pageOpts?: PaginationOptions ) { return async (dispatch: Dispatch) => { dispatch({ type: LIST_RETRY_TASKS_BEGIN, queue: qname }); try { const response = await listRetryTasks(qname, pageOpts); dispatch({ type: LIST_RETRY_TASKS_SUCCESS, queue: qname, payload: response, }); } catch { dispatch({ type: LIST_RETRY_TASKS_ERROR, queue: qname, error: `Could not retreive retry tasks data for queue: ${qname}`, }); } }; } export function listDeadTasksAsync( qname: string, pageOpts?: PaginationOptions ) { return async (dispatch: Dispatch) => { dispatch({ type: LIST_DEAD_TASKS_BEGIN, queue: qname }); try { const response = await listDeadTasks(qname, pageOpts); dispatch({ type: LIST_DEAD_TASKS_SUCCESS, queue: qname, payload: response, }); } catch { dispatch({ type: LIST_DEAD_TASKS_ERROR, queue: qname, error: `Could not retreive dead tasks data for queue: ${qname}`, }); } }; } export function cancelActiveTaskAsync(queue: string, taskId: string) { return async (dispatch: Dispatch) => { dispatch({ type: CANCEL_ACTIVE_TASK_BEGIN, queue, taskId }); try { await cancelActiveTask(queue, taskId); dispatch({ type: CANCEL_ACTIVE_TASK_SUCCESS, queue, taskId }); } catch { dispatch({ type: CANCEL_ACTIVE_TASK_ERROR, error: `Could not cancel task: ${taskId}`, queue, taskId, }); } }; } export function deleteRetryTaskAsync(queue: string, taskKey: string) { return async (dispatch: Dispatch) => { dispatch({ type: DELETE_RETRY_TASK_BEGIN, queue, taskKey }); try { await deleteRetryTask(queue, taskKey); dispatch({ type: DELETE_RETRY_TASK_SUCCESS, queue, taskKey }); } catch (error) { console.error("deleteRetryTaskAsync: ", error); dispatch({ type: DELETE_RETRY_TASK_ERROR, error: `Could not delete task: ${taskKey}`, queue, taskKey, }); } }; }