1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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";
27 export const loadProcess = (containerRequestUuid: string) =>
28 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<Process | undefined> => {
29 let containerRequest: ContainerRequestResource | undefined = undefined;
31 containerRequest = await services.containerRequestService.get(containerRequestUuid);
32 dispatch<any>(updateResources([containerRequest]));
37 if (containerRequest.outputUuid) {
39 const collection = await services.collectionService.get(containerRequest.outputUuid, false);
40 dispatch<any>(updateResources([collection]));
44 if (containerRequest.containerUuid) {
45 let container: ContainerResource | undefined = undefined;
47 container = await services.containerService.get(containerRequest.containerUuid, false);
48 dispatch<any>(updateResources([container]));
52 if (container && container.runtimeUserUuid) {
53 const runtimeUser = await services.userService.get(container.runtimeUserUuid, false);
54 dispatch<any>(updateResources([runtimeUser]));
58 return { containerRequest, container };
60 return { containerRequest };
63 export const loadContainers = (filters: string, loadMounts: boolean = true) =>
64 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
65 let args: any = { filters };
67 args.select = containerFieldsNoMounts;
69 const { items } = await services.containerService.list(args);
70 dispatch<any>(updateResources(items));
74 // Until the api supports unselecting fields, we need a list of all other fields to omit mounts
75 const containerFieldsNoMounts = [
88 "interactive_session_started",
94 "modified_by_client_uuid",
95 "modified_by_user_uuid",
98 "output_storage_classes",
103 "runtime_auth_scopes",
104 "runtime_constraints",
107 "scheduling_parameters",
113 export const cancelRunningWorkflow = (uuid: string) =>
114 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
116 const process = await services.containerRequestService.update(uuid, { priority: 0 });
117 dispatch<any>(updateResources([process]));
118 if (process.containerUuid) {
119 const container = await services.containerService.get(process.containerUuid, false);
120 dispatch<any>(updateResources([container]));
124 throw new Error('Could not cancel the process.');
128 export const resumeOnHoldWorkflow = (uuid: string) =>
129 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
131 const process = await services.containerRequestService.update(uuid, { priority: 500 });
132 dispatch<any>(updateResources([process]));
133 if (process.containerUuid) {
134 const container = await services.containerService.get(process.containerUuid, false);
135 dispatch<any>(updateResources([container]));
139 throw new Error('Could not resume the process.');
143 export const startWorkflow = (uuid: string) =>
144 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
146 const process = await services.containerRequestService.update(uuid, { state: ContainerRequestState.COMMITTED });
148 dispatch<any>(updateResources([process]));
149 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process started', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
151 dispatch<any>(snackbarActions.OPEN_SNACKBAR({ message: `Failed to start process`, kind: SnackbarKind.ERROR }));
154 dispatch<any>(snackbarActions.OPEN_SNACKBAR({ message: `Failed to start process`, kind: SnackbarKind.ERROR }));
158 export const reRunProcess = (processUuid: string, workflowUuid: string) =>
159 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
160 const process = getResource<any>(processUuid)(getState().resources);
161 const workflows = getState().runProcessPanel.searchWorkflows;
162 const workflow = workflows.find(workflow => workflow.uuid === workflowUuid);
163 if (workflow && process) {
164 const mainWf = getWorkflow(process.mounts[MOUNT_PATH_CWL_WORKFLOW]);
165 if (mainWf) { mainWf.inputs = getInputs(process); }
166 const stringifiedDefinition = JSON.stringify(process.mounts[MOUNT_PATH_CWL_WORKFLOW].content);
167 const newWorkflow = { ...workflow, definition: stringifiedDefinition };
169 const owner = getResource<ProjectResource | UserResource>(workflow.ownerUuid)(getState().resources);
170 const basicInitialData: RunProcessBasicFormData = { name: `Copy of: ${process.name}`, description: process.description, owner };
171 dispatch<any>(initialize(RUN_PROCESS_BASIC_FORM, basicInitialData));
173 const advancedInitialData: RunProcessAdvancedFormData = {
174 output: process.outputName,
175 runtime: process.schedulingParameters.max_run_time,
176 ram: process.runtimeConstraints.ram,
177 vcpus: process.runtimeConstraints.vcpus,
178 keep_cache_ram: process.runtimeConstraints.keep_cache_ram,
179 acr_container_image: process.containerImage
181 dispatch<any>(initialize(RUN_PROCESS_ADVANCED_FORM, advancedInitialData));
183 dispatch<any>(navigateToRunProcess);
184 dispatch<any>(goToStep(1));
185 dispatch(runProcessPanelActions.SET_STEP_CHANGED(true));
186 dispatch(runProcessPanelActions.SET_SELECTED_WORKFLOW(newWorkflow));
188 dispatch<any>(snackbarActions.OPEN_SNACKBAR({ message: `You can't re-run this process`, kind: SnackbarKind.ERROR }));
193 * Fetches raw inputs from containerRequest mounts with fallback to properties
194 * Returns undefined if containerRequest not loaded
195 * Returns {} if inputs not found in mounts or props
197 export const getRawInputs = (data: any): WorkflowInputsData | undefined => {
198 if (!data) { return undefined; }
199 const mountInput = data.mounts?.[MOUNT_PATH_CWL_INPUT]?.content;
200 const propsInput = data.properties?.cwl_input;
201 if (!mountInput && !propsInput) { return {}; }
202 return (mountInput || propsInput);
205 export const getInputs = (data: any): CommandInputParameter[] => {
206 // Definitions from mounts are needed so we return early if missing
207 if (!data || !data.mounts || !data.mounts[MOUNT_PATH_CWL_WORKFLOW]) { return []; }
208 const content = getRawInputs(data) as any;
209 // Only escape if content is falsy to allow displaying definitions if no inputs are present
210 // (Don't check raw content length)
211 if (!content) { return []; }
213 const inputs = getWorkflowInputs(data.mounts[MOUNT_PATH_CWL_WORKFLOW].content);
214 return inputs ? inputs.map(
220 default: content[it.id],
221 value: content[it.id.split('/').pop()] || [],
229 * Fetches raw outputs from containerRequest properties
230 * Assumes containerRequest is loaded
232 export const getRawOutputs = (data: any): CommandInputParameter[] | undefined => {
233 if (!data || !data.properties || !data.properties.cwl_output) { return undefined; }
234 return (data.properties.cwl_output);
237 export type InputCollectionMount = {
242 export const getInputCollectionMounts = (data: any): InputCollectionMount[] => {
243 if (!data || !data.mounts) { return []; }
244 return Object.keys(data.mounts)
249 .filter(mount => mount.kind === 'collection' &&
250 mount.portable_data_hash &&
254 pdh: mount.portable_data_hash,
258 export const getOutputParameters = (data: any): CommandOutputParameter[] => {
259 if (!data || !data.mounts || !data.mounts[MOUNT_PATH_CWL_WORKFLOW]) { return []; }
260 const outputs = getWorkflowOutputs(data.mounts[MOUNT_PATH_CWL_WORKFLOW].content);
261 return outputs ? outputs.map(
273 export const openRemoveProcessDialog = (uuid: string) =>
274 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
275 dispatch(dialogActions.OPEN_DIALOG({
276 id: REMOVE_PROCESS_DIALOG,
278 title: 'Remove process permanently',
279 text: 'Are you sure you want to remove this process?',
280 confirmButtonLabel: 'Remove',
286 export const REMOVE_PROCESS_DIALOG = 'removeProcessDialog';
288 export const removeProcessPermanently = (uuid: string) =>
289 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
290 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...', kind: SnackbarKind.INFO }));
291 await services.containerRequestService.delete(uuid);
292 dispatch(projectPanelActions.REQUEST_ITEMS());
293 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removed.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));