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";
26 import { FilterBuilder } from "services/api/filter-builder";
27 import { CommonResourceServiceError, getCommonResourceServiceError } from "services/common-service/common-resource-service";
29 export const loadProcess = (containerRequestUuid: string) =>
30 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<Process | undefined> => {
31 let containerRequest: ContainerRequestResource | undefined = undefined;
33 containerRequest = await services.containerRequestService.get(containerRequestUuid);
34 dispatch<any>(updateResources([containerRequest]));
39 if (containerRequest.outputUuid) {
41 const collection = await services.collectionService.get(containerRequest.outputUuid, false);
42 dispatch<any>(updateResources([collection]));
46 if (containerRequest.containerUuid) {
47 let container: ContainerResource | undefined = undefined;
49 container = await services.containerService.get(containerRequest.containerUuid, false);
50 dispatch<any>(updateResources([container]));
54 if (container && container.runtimeUserUuid) {
55 const runtimeUser = await services.userService.get(container.runtimeUserUuid, false);
56 dispatch<any>(updateResources([runtimeUser]));
60 return { containerRequest, container };
62 return { containerRequest };
65 export const loadContainers = (containerUuids: string[], loadMounts: boolean = true) =>
66 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
68 filters: new FilterBuilder().addIn('uuid', containerUuids).getFilters(),
69 limit: containerUuids.length,
72 args.select = containerFieldsNoMounts;
74 const { items } = await services.containerService.list(args);
75 dispatch<any>(updateResources(items));
79 // Until the api supports unselecting fields, we need a list of all other fields to omit mounts
80 const containerFieldsNoMounts = [
93 "interactive_session_started",
99 "modified_by_client_uuid",
100 "modified_by_user_uuid",
103 "output_storage_classes",
108 "runtime_auth_scopes",
109 "runtime_constraints",
112 "scheduling_parameters",
119 export const cancelRunningWorkflow = (uuid: string) =>
120 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
122 const process = await services.containerRequestService.update(uuid, { priority: 0 });
123 dispatch<any>(updateResources([process]));
124 if (process.containerUuid) {
125 const container = await services.containerService.get(process.containerUuid, false);
126 dispatch<any>(updateResources([container]));
130 throw new Error('Could not cancel the process.');
134 export const resumeOnHoldWorkflow = (uuid: string) =>
135 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
137 const process = await services.containerRequestService.update(uuid, { priority: 500 });
138 dispatch<any>(updateResources([process]));
139 if (process.containerUuid) {
140 const container = await services.containerService.get(process.containerUuid, false);
141 dispatch<any>(updateResources([container]));
145 throw new Error('Could not resume the process.');
149 export const startWorkflow = (uuid: string) =>
150 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
152 const process = await services.containerRequestService.update(uuid, { state: ContainerRequestState.COMMITTED });
154 dispatch<any>(updateResources([process]));
155 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process started', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
157 dispatch<any>(snackbarActions.OPEN_SNACKBAR({ message: `Failed to start process`, kind: SnackbarKind.ERROR }));
160 dispatch<any>(snackbarActions.OPEN_SNACKBAR({ message: `Failed to start process`, kind: SnackbarKind.ERROR }));
164 export const reRunProcess = (processUuid: string, workflowUuid: string) =>
165 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
166 const process = getResource<any>(processUuid)(getState().resources);
167 const workflows = getState().runProcessPanel.searchWorkflows;
168 const workflow = workflows.find(workflow => workflow.uuid === workflowUuid);
169 if (workflow && process) {
170 const mainWf = getWorkflow(process.mounts[MOUNT_PATH_CWL_WORKFLOW]);
171 if (mainWf) { mainWf.inputs = getInputs(process); }
172 const stringifiedDefinition = JSON.stringify(process.mounts[MOUNT_PATH_CWL_WORKFLOW].content);
173 const newWorkflow = { ...workflow, definition: stringifiedDefinition };
175 const owner = getResource<ProjectResource | UserResource>(workflow.ownerUuid)(getState().resources);
176 const basicInitialData: RunProcessBasicFormData = { name: `Copy of: ${process.name}`, description: process.description, owner };
177 dispatch<any>(initialize(RUN_PROCESS_BASIC_FORM, basicInitialData));
179 const advancedInitialData: RunProcessAdvancedFormData = {
180 output: process.outputName,
181 runtime: process.schedulingParameters.max_run_time,
182 ram: process.runtimeConstraints.ram,
183 vcpus: process.runtimeConstraints.vcpus,
184 keep_cache_ram: process.runtimeConstraints.keep_cache_ram,
185 acr_container_image: process.containerImage
187 dispatch<any>(initialize(RUN_PROCESS_ADVANCED_FORM, advancedInitialData));
189 dispatch<any>(navigateToRunProcess);
190 dispatch<any>(goToStep(1));
191 dispatch(runProcessPanelActions.SET_STEP_CHANGED(true));
192 dispatch(runProcessPanelActions.SET_SELECTED_WORKFLOW(newWorkflow));
194 dispatch<any>(snackbarActions.OPEN_SNACKBAR({ message: `You can't re-run this process`, kind: SnackbarKind.ERROR }));
199 * Fetches raw inputs from containerRequest mounts with fallback to properties
200 * Returns undefined if containerRequest not loaded
201 * Returns {} if inputs not found in mounts or props
203 export const getRawInputs = (data: any): WorkflowInputsData | undefined => {
204 if (!data) { return undefined; }
205 const mountInput = data.mounts?.[MOUNT_PATH_CWL_INPUT]?.content;
206 const propsInput = data.properties?.cwl_input;
207 if (!mountInput && !propsInput) { return {}; }
208 return (mountInput || propsInput);
211 export const getInputs = (data: any): CommandInputParameter[] => {
212 // Definitions from mounts are needed so we return early if missing
213 if (!data || !data.mounts || !data.mounts[MOUNT_PATH_CWL_WORKFLOW]) { return []; }
214 const content = getRawInputs(data) as any;
215 // Only escape if content is falsy to allow displaying definitions if no inputs are present
216 // (Don't check raw content length)
217 if (!content) { return []; }
219 const inputs = getWorkflowInputs(data.mounts[MOUNT_PATH_CWL_WORKFLOW].content);
220 return inputs ? inputs.map(
226 default: content[it.id],
227 value: content[it.id.split('/').pop()] || [],
235 * Fetches raw outputs from containerRequest properties
236 * Assumes containerRequest is loaded
238 export const getRawOutputs = (data: any): CommandInputParameter[] | undefined => {
239 if (!data || !data.properties || !data.properties.cwl_output) { return undefined; }
240 return (data.properties.cwl_output);
243 export type InputCollectionMount = {
248 export const getInputCollectionMounts = (data: any): InputCollectionMount[] => {
249 if (!data || !data.mounts) { return []; }
250 return Object.keys(data.mounts)
255 .filter(mount => mount.kind === 'collection' &&
256 mount.portable_data_hash &&
260 pdh: mount.portable_data_hash,
264 export const getOutputParameters = (data: any): CommandOutputParameter[] => {
265 if (!data || !data.mounts || !data.mounts[MOUNT_PATH_CWL_WORKFLOW]) { return []; }
266 const outputs = getWorkflowOutputs(data.mounts[MOUNT_PATH_CWL_WORKFLOW].content);
267 return outputs ? outputs.map(
279 export const openRemoveProcessDialog = (uuid: string) =>
280 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
281 dispatch(dialogActions.OPEN_DIALOG({
282 id: REMOVE_PROCESS_DIALOG,
284 title: 'Remove process permanently',
285 text: 'Are you sure you want to remove this process?',
286 confirmButtonLabel: 'Remove',
292 export const REMOVE_PROCESS_DIALOG = 'removeProcessDialog';
294 export const removeProcessPermanently = (uuid: string) =>
295 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
297 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...', kind: SnackbarKind.INFO }));
298 await services.containerRequestService.delete(uuid, false);
299 dispatch(projectPanelActions.REQUEST_ITEMS());
300 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removed.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
302 const error = getCommonResourceServiceError(e);
303 if (error === CommonResourceServiceError.PERMISSION_ERROR_FORBIDDEN) {
304 dispatch(snackbarActions.OPEN_SNACKBAR({ message: `Access denied`, hideDuration: 2000, kind: SnackbarKind.ERROR }));
306 dispatch(snackbarActions.OPEN_SNACKBAR({ message: `Deletion failed`, hideDuration: 2000, kind: SnackbarKind.ERROR }));