20000: Add run button to resume cancelled processes (priority 0)
[arvados.git] / src / store / processes / processes-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Dispatch } from "redux";
6 import { RootState } from 'store/store';
7 import { ServiceRepository } from 'services/services';
8 import { updateResources } from 'store/resources/resources-actions';
9 import { Process } from './process';
10 import { dialogActions } from 'store/dialog/dialog-actions';
11 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
12 import { projectPanelActions } from 'store/project-panel/project-panel-action';
13 import { navigateToRunProcess } from 'store/navigation/navigation-action';
14 import { goToStep, runProcessPanelActions } from 'store/run-process-panel/run-process-panel-actions';
15 import { getResource } from 'store/resources/resources';
16 import { initialize } from "redux-form";
17 import { RUN_PROCESS_BASIC_FORM, RunProcessBasicFormData } from "views/run-process-panel/run-process-basic-form";
18 import { RunProcessAdvancedFormData, RUN_PROCESS_ADVANCED_FORM } from "views/run-process-panel/run-process-advanced-form";
19 import { MOUNT_PATH_CWL_WORKFLOW, MOUNT_PATH_CWL_INPUT } from 'models/process';
20 import { CommandInputParameter, getWorkflow, getWorkflowInputs, getWorkflowOutputs, WorkflowInputsData } from "models/workflow";
21 import { ProjectResource } from "models/project";
22 import { UserResource } from "models/user";
23 import { CommandOutputParameter } from "cwlts/mappings/v1.0/CommandOutputParameter";
24 import { ContainerResource } from "models/container";
25 import { ContainerRequestResource, ContainerRequestState } from "models/container-request";
26
27 export const loadProcess = (containerRequestUuid: string) =>
28     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<Process | undefined> => {
29         let containerRequest: ContainerRequestResource | undefined = undefined;
30         try {
31             containerRequest = await services.containerRequestService.get(containerRequestUuid);
32             dispatch<any>(updateResources([containerRequest]));
33         } catch {
34             return undefined;
35         }
36
37         if (containerRequest.outputUuid) {
38             try {
39                 const collection = await services.collectionService.get(containerRequest.outputUuid, false);
40                 dispatch<any>(updateResources([collection]));
41             } catch {}
42         }
43
44         if (containerRequest.containerUuid) {
45             let container: ContainerResource | undefined = undefined;
46             try {
47                 container = await services.containerService.get(containerRequest.containerUuid, false);
48                 dispatch<any>(updateResources([container]));
49             } catch {}
50
51             try{
52                 if (container && container.runtimeUserUuid) {
53                     const runtimeUser = await services.userService.get(container.runtimeUserUuid, false);
54                     dispatch<any>(updateResources([runtimeUser]));
55                 }
56             } catch {}
57
58             return { containerRequest, container };
59         }
60         return { containerRequest };
61     };
62
63 export const loadContainers = (filters: string, loadMounts: boolean = true) =>
64     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
65         let args: any = { filters };
66         if (!loadMounts) {
67             args.select = containerFieldsNoMounts;
68         }
69         const { items } = await services.containerService.list(args);
70         dispatch<any>(updateResources(items));
71         return items;
72     };
73
74 // Until the api supports unselecting fields, we need a list of all other fields to omit mounts
75 const containerFieldsNoMounts = [
76     "auth_uuid",
77     "command",
78     "container_image",
79     "cost",
80     "created_at",
81     "cwd",
82     "environment",
83     "etag",
84     "exit_code",
85     "finished_at",
86     "gateway_address",
87     "href",
88     "interactive_session_started",
89     "kind",
90     "lock_count",
91     "locked_by_uuid",
92     "log",
93     "modified_at",
94     "modified_by_client_uuid",
95     "modified_by_user_uuid",
96     "output_path",
97     "output_properties",
98     "output_storage_classes",
99     "output",
100     "owner_uuid",
101     "priority",
102     "progress",
103     "runtime_auth_scopes",
104     "runtime_constraints",
105     "runtime_status",
106     "runtime_user_uuid",
107     "scheduling_parameters",
108     "started_at",
109     "state",
110     "uuid",
111 ]
112
113 export const cancelRunningWorkflow = (uuid: string) =>
114     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
115         try {
116             const process = await services.containerRequestService.update(uuid, { priority: 0 });
117             dispatch<any>(updateResources([process]));
118             return process;
119         } catch (e) {
120             throw new Error('Could not cancel the process.');
121         }
122     };
123
124 export const resumeOnHoldWorkflow = (uuid: string) =>
125     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
126         try {
127             const process = await services.containerRequestService.update(uuid, { priority: 500 });
128             dispatch<any>(updateResources([process]));
129             return process;
130         } catch (e) {
131             throw new Error('Could not resume the process.');
132         }
133     };
134
135 export const startWorkflow = (uuid: string) =>
136     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
137         try {
138             const process = await services.containerRequestService.update(uuid, { state: ContainerRequestState.COMMITTED });
139             if (process) {
140                 dispatch<any>(updateResources([process]));
141                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process started', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
142             } else {
143                 dispatch<any>(snackbarActions.OPEN_SNACKBAR({ message: `Failed to start process`, kind: SnackbarKind.ERROR }));
144             }
145         } catch (e) {
146             dispatch<any>(snackbarActions.OPEN_SNACKBAR({ message: `Failed to start process`, kind: SnackbarKind.ERROR }));
147         }
148     };
149
150 export const reRunProcess = (processUuid: string, workflowUuid: string) =>
151     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
152         const process = getResource<any>(processUuid)(getState().resources);
153         const workflows = getState().runProcessPanel.searchWorkflows;
154         const workflow = workflows.find(workflow => workflow.uuid === workflowUuid);
155         if (workflow && process) {
156             const mainWf = getWorkflow(process.mounts[MOUNT_PATH_CWL_WORKFLOW]);
157             if (mainWf) { mainWf.inputs = getInputs(process); }
158             const stringifiedDefinition = JSON.stringify(process.mounts[MOUNT_PATH_CWL_WORKFLOW].content);
159             const newWorkflow = { ...workflow, definition: stringifiedDefinition };
160
161             const owner = getResource<ProjectResource | UserResource>(workflow.ownerUuid)(getState().resources);
162             const basicInitialData: RunProcessBasicFormData = { name: `Copy of: ${process.name}`, description: process.description, owner };
163             dispatch<any>(initialize(RUN_PROCESS_BASIC_FORM, basicInitialData));
164
165             const advancedInitialData: RunProcessAdvancedFormData = {
166                 output: process.outputName,
167                 runtime: process.schedulingParameters.max_run_time,
168                 ram: process.runtimeConstraints.ram,
169                 vcpus: process.runtimeConstraints.vcpus,
170                 keep_cache_ram: process.runtimeConstraints.keep_cache_ram,
171                 acr_container_image: process.containerImage
172             };
173             dispatch<any>(initialize(RUN_PROCESS_ADVANCED_FORM, advancedInitialData));
174
175             dispatch<any>(navigateToRunProcess);
176             dispatch<any>(goToStep(1));
177             dispatch(runProcessPanelActions.SET_STEP_CHANGED(true));
178             dispatch(runProcessPanelActions.SET_SELECTED_WORKFLOW(newWorkflow));
179         } else {
180             dispatch<any>(snackbarActions.OPEN_SNACKBAR({ message: `You can't re-run this process`, kind: SnackbarKind.ERROR }));
181         }
182     };
183
184 /*
185  * Fetches raw inputs from containerRequest mounts with fallback to properties
186  * Returns undefined if containerRequest not loaded
187  * Returns {} if inputs not found in mounts or props
188  */
189 export const getRawInputs = (data: any): WorkflowInputsData | undefined => {
190     if (!data) { return undefined; }
191     const mountInput = data.mounts?.[MOUNT_PATH_CWL_INPUT]?.content;
192     const propsInput = data.properties?.cwl_input;
193     if (!mountInput && !propsInput) { return {}; }
194     return (mountInput || propsInput);
195 }
196
197 export const getInputs = (data: any): CommandInputParameter[] => {
198     // Definitions from mounts are needed so we return early if missing
199     if (!data || !data.mounts || !data.mounts[MOUNT_PATH_CWL_WORKFLOW]) { return []; }
200     const content  = getRawInputs(data) as any;
201     // Only escape if content is falsy to allow displaying definitions if no inputs are present
202     // (Don't check raw content length)
203     if (!content) { return []; }
204
205     const inputs = getWorkflowInputs(data.mounts[MOUNT_PATH_CWL_WORKFLOW].content);
206     return inputs ? inputs.map(
207         (it: any) => (
208             {
209                 type: it.type,
210                 id: it.id,
211                 label: it.label,
212                 default: content[it.id],
213                 value: content[it.id.split('/').pop()] || [],
214                 doc: it.doc
215             }
216         )
217     ) : [];
218 };
219
220 /*
221  * Fetches raw outputs from containerRequest properties
222  * Assumes containerRequest is loaded
223  */
224 export const getRawOutputs = (data: any): CommandInputParameter[] | undefined => {
225     if (!data || !data.properties || !data.properties.cwl_output) { return undefined; }
226     return (data.properties.cwl_output);
227 }
228
229 export type InputCollectionMount = {
230     path: string;
231     pdh: string;
232 }
233
234 export const getInputCollectionMounts = (data: any): InputCollectionMount[] => {
235     if (!data || !data.mounts) { return []; }
236     return Object.keys(data.mounts)
237         .map(key => ({
238             ...data.mounts[key],
239             path: key,
240         }))
241         .filter(mount => mount.kind === 'collection' &&
242                 mount.portable_data_hash &&
243                 mount.path)
244         .map(mount => ({
245             path: mount.path,
246             pdh: mount.portable_data_hash,
247         }));
248 };
249
250 export const getOutputParameters = (data: any): CommandOutputParameter[] => {
251     if (!data || !data.mounts || !data.mounts[MOUNT_PATH_CWL_WORKFLOW]) { return []; }
252     const outputs = getWorkflowOutputs(data.mounts[MOUNT_PATH_CWL_WORKFLOW].content);
253     return outputs ? outputs.map(
254         (it: any) => (
255             {
256                 type: it.type,
257                 id: it.id,
258                 label: it.label,
259                 doc: it.doc
260             }
261         )
262     ) : [];
263 };
264
265 export const openRemoveProcessDialog = (uuid: string) =>
266     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
267         dispatch(dialogActions.OPEN_DIALOG({
268             id: REMOVE_PROCESS_DIALOG,
269             data: {
270                 title: 'Remove process permanently',
271                 text: 'Are you sure you want to remove this process?',
272                 confirmButtonLabel: 'Remove',
273                 uuid
274             }
275         }));
276     };
277
278 export const REMOVE_PROCESS_DIALOG = 'removeProcessDialog';
279
280 export const removeProcessPermanently = (uuid: string) =>
281     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
282         dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...', kind: SnackbarKind.INFO }));
283         await services.containerRequestService.delete(uuid);
284         dispatch(projectPanelActions.REQUEST_ITEMS());
285         dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removed.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
286     };