-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathJobInvocationActions.js
More file actions
139 lines (129 loc) · 4.1 KB
/
Copy pathJobInvocationActions.js
File metadata and controls
139 lines (129 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { translate as __, sprintf } from 'foremanReact/common/I18n';
import { addToast } from 'foremanReact/components/ToastsList';
import { APIActions, get } from 'foremanReact/redux/API';
import {
CANCEL_JOB,
CANCEL_RECURRING_LOGIC,
CHANGE_ENABLED_RECURRING_LOGIC,
JOB_INVOCATION_KEY,
STATUS,
} from './JobInvocationConstants';
const FINISHED_STATUSES = [STATUS.FAILED, STATUS.SUCCEEDED, STATUS.CANCELLED];
export const isJobFinished = statusLabel =>
FINISHED_STATUSES.includes(statusLabel);
const extractErrorMessage = response =>
// eslint-disable-next-line camelcase
response?.data?.error?.full_messages?.[0] ||
response?.data?.error?.message ||
'Unknown error.';
const fetchJobInvocation = (dispatch, url, params = {}, pollTimeoutRef = { current: null }) => {
dispatch(
get({
key: JOB_INVOCATION_KEY,
params: { include_hosts: false, include_permissions: true, ...params },
url,
handleSuccess: ({ data }) => {
if (!isJobFinished(data.status_label)) {
pollTimeoutRef.current = setTimeout(
() => fetchJobInvocation(dispatch, url, {}, pollTimeoutRef),
5000
);
} else {
pollTimeoutRef.current = null;
}
},
handleError: () => {
pollTimeoutRef.current = null;
},
errorToast: ({ response }) => extractErrorMessage(response),
})
);
};
export const getJobInvocation = (url, pollTimeoutRef) => dispatch => {
stopJobInvocationPolling(pollTimeoutRef);
fetchJobInvocation(dispatch, url, { include_permissions: true }, pollTimeoutRef);
};
export const stopJobInvocationPolling = pollTimeoutRef => {
clearTimeout(pollTimeoutRef.current);
pollTimeoutRef.current = null;
};
export const cancelJob = (jobId, force) => dispatch => {
const infoToast = () =>
force
? sprintf(__('Trying to abort the job %s.'), jobId)
: sprintf(__('Trying to cancel the job %s.'), jobId);
const errorToast = response =>
force
? sprintf(__('Could not abort the job %s: %s'), jobId, response)
: sprintf(__('Could not cancel the job %s: %s'), jobId, response);
const url = force
? `/job_invocations/${jobId}/cancel?force=true`
: `/job_invocations/${jobId}/cancel`;
dispatch(
APIActions.post({
url,
key: CANCEL_JOB,
errorToast: ({ response }) => errorToast(extractErrorMessage(response)),
handleSuccess: () => {
dispatch(
addToast({
key: `cancel-job-error`,
type: 'info',
message: infoToast(),
})
);
},
})
);
};
export const enableRecurringLogic = (
recurrenceId,
enabled,
jobId
) => dispatch => {
const successToast = () =>
enabled
? sprintf(__('Recurring logic %s disabled successfully.'), recurrenceId)
: sprintf(__('Recurring logic %s enabled successfully.'), recurrenceId);
const errorToast = response =>
enabled
? sprintf(
__(`Could not disable recurring logic %s: ${response}`),
recurrenceId
)
: sprintf(
__(`Could not enable recurring logic %s: ${response}`),
recurrenceId
);
const url = `/foreman_tasks/api/recurring_logics/${recurrenceId}`;
dispatch(
APIActions.put({
url,
key: CHANGE_ENABLED_RECURRING_LOGIC,
params: { recurring_logic: { enabled: !enabled } },
successToast,
errorToast: ({ response }) => errorToast(extractErrorMessage(response)),
handleSuccess: () => {
fetchJobInvocation(dispatch, `/api/job_invocations/${jobId}`);
},
})
);
};
export const cancelRecurringLogic = recurrenceId => dispatch => {
const successToast = () =>
sprintf(__('Recurring logic %s cancelled successfully.'), recurrenceId);
const errorToast = response =>
sprintf(
__(`Could not cancel recurring logic %s: ${response}`),
recurrenceId
);
const url = `/foreman_tasks/recurring_logics/${recurrenceId}/cancel`;
dispatch(
APIActions.post({
url,
key: CANCEL_RECURRING_LOGIC,
successToast,
errorToast: ({ response }) => errorToast(extractErrorMessage(response)),
})
);
};