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