github.com/wfusion/gofusion@v1.1.14/common/infra/asynq/asynqmon/ui/src/actions/queuesActions.ts (about) 1 import { Dispatch } from "redux"; 2 import { 3 deleteQueue, 4 listQueues, 5 ListQueuesResponse, 6 pauseQueue, 7 resumeQueue, 8 } from "../api"; 9 import { toErrorString, toErrorStringWithHttpStatus } from "../utils"; 10 11 // List of queue related action types. 12 export const LIST_QUEUES_BEGIN = "LIST_QUEUES_BEGIN"; 13 export const LIST_QUEUES_SUCCESS = "LIST_QUEUES_SUCCESS"; 14 export const LIST_QUEUES_ERROR = "LIST_QUEUES_ERROR"; 15 export const DELETE_QUEUE_BEGIN = "DELETE_QUEUE_BEGIN"; 16 export const DELETE_QUEUE_SUCCESS = "DELETE_QUEUE_SUCCESS"; 17 export const DELETE_QUEUE_ERROR = "DELETE_QUEUE_ERROR"; 18 export const PAUSE_QUEUE_BEGIN = "PAUSE_QUEUE_BEGIN"; 19 export const PAUSE_QUEUE_SUCCESS = "PAUSE_QUEUE_SUCCESS"; 20 export const PAUSE_QUEUE_ERROR = "PAUSE_QUEUE_ERROR"; 21 export const RESUME_QUEUE_BEGIN = "RESUME_QUEUE_BEGIN"; 22 export const RESUME_QUEUE_SUCCESS = "RESUME_QUEUE_SUCCESS"; 23 export const RESUME_QUEUE_ERROR = "RESUME_QUEUE_ERROR"; 24 25 interface ListQueuesBeginAction { 26 type: typeof LIST_QUEUES_BEGIN; 27 } 28 29 interface ListQueuesSuccessAction { 30 type: typeof LIST_QUEUES_SUCCESS; 31 payload: ListQueuesResponse; 32 } 33 34 interface ListQueuesErrorAction { 35 type: typeof LIST_QUEUES_ERROR; 36 error: string; 37 } 38 39 interface DeleteQueueBeginAction { 40 type: typeof DELETE_QUEUE_BEGIN; 41 queue: string; // name of the queue 42 } 43 44 interface DeleteQueueSuccessAction { 45 type: typeof DELETE_QUEUE_SUCCESS; 46 queue: string; // name of the queue 47 } 48 49 interface DeleteQueueErrorAction { 50 type: typeof DELETE_QUEUE_ERROR; 51 queue: string; // name of the queue 52 error: string; // error description 53 } 54 55 interface PauseQueueBeginAction { 56 type: typeof PAUSE_QUEUE_BEGIN; 57 queue: string; // name of the queue 58 } 59 60 interface PauseQueueSuccessAction { 61 type: typeof PAUSE_QUEUE_SUCCESS; 62 queue: string; // name of the queue 63 } 64 65 interface PauseQueueErrorAction { 66 type: typeof PAUSE_QUEUE_ERROR; 67 queue: string; // name of the queue 68 error: string; // error description 69 } 70 71 interface ResumeQueueBeginAction { 72 type: typeof RESUME_QUEUE_BEGIN; 73 queue: string; // name of the queue 74 } 75 76 interface ResumeQueueSuccessAction { 77 type: typeof RESUME_QUEUE_SUCCESS; 78 queue: string; // name of the queue 79 } 80 81 interface ResumeQueueErrorAction { 82 type: typeof RESUME_QUEUE_ERROR; 83 queue: string; // name of the queue 84 error: string; // error description 85 } 86 87 // Union of all queues related action types. 88 export type QueuesActionTypes = 89 | ListQueuesBeginAction 90 | ListQueuesSuccessAction 91 | ListQueuesErrorAction 92 | DeleteQueueBeginAction 93 | DeleteQueueSuccessAction 94 | DeleteQueueErrorAction 95 | PauseQueueBeginAction 96 | PauseQueueSuccessAction 97 | PauseQueueErrorAction 98 | ResumeQueueBeginAction 99 | ResumeQueueSuccessAction 100 | ResumeQueueErrorAction; 101 102 export function listQueuesAsync() { 103 return async (dispatch: Dispatch<QueuesActionTypes>) => { 104 dispatch({ type: LIST_QUEUES_BEGIN }); 105 try { 106 const response = await listQueues(); 107 dispatch({ 108 type: LIST_QUEUES_SUCCESS, 109 payload: response, 110 }); 111 } catch (error) { 112 console.error(`listQueuesAsync: ${toErrorStringWithHttpStatus(error)}`); 113 dispatch({ 114 type: LIST_QUEUES_ERROR, 115 error: toErrorString(error), 116 }); 117 } 118 }; 119 } 120 121 export function deleteQueueAsync(qname: string) { 122 return async (dispatch: Dispatch<QueuesActionTypes>) => { 123 dispatch({ 124 type: DELETE_QUEUE_BEGIN, 125 queue: qname, 126 }); 127 try { 128 await deleteQueue(qname); 129 // FIXME: this action doesn't get dispatched when server stalls 130 dispatch({ 131 type: DELETE_QUEUE_SUCCESS, 132 queue: qname, 133 }); 134 } catch (error) { 135 console.error(error); 136 dispatch({ 137 type: DELETE_QUEUE_ERROR, 138 queue: qname, 139 error: toErrorString(error), 140 }); 141 } 142 }; 143 } 144 145 export function pauseQueueAsync(qname: string) { 146 return async (dispatch: Dispatch<QueuesActionTypes>) => { 147 dispatch({ type: PAUSE_QUEUE_BEGIN, queue: qname }); 148 try { 149 await pauseQueue(qname); 150 dispatch({ type: PAUSE_QUEUE_SUCCESS, queue: qname }); 151 } catch (error) { 152 console.error("pauseQueueAsynq: ", toErrorStringWithHttpStatus(error)); 153 dispatch({ 154 type: PAUSE_QUEUE_ERROR, 155 queue: qname, 156 error: toErrorString(error), 157 }); 158 } 159 }; 160 } 161 162 export function resumeQueueAsync(qname: string) { 163 return async (dispatch: Dispatch<QueuesActionTypes>) => { 164 dispatch({ type: RESUME_QUEUE_BEGIN, queue: qname }); 165 try { 166 await resumeQueue(qname); 167 dispatch({ type: RESUME_QUEUE_SUCCESS, queue: qname }); 168 } catch (error) { 169 console.error("resumeQueueAsync: ", toErrorStringWithHttpStatus(error)); 170 dispatch({ 171 type: RESUME_QUEUE_ERROR, 172 queue: qname, 173 error: toErrorString(error), 174 }); 175 } 176 }; 177 }