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 { dialogActions } from "store/dialog/dialog-actions";
10 import { snackbarActions, SnackbarKind } from "store/snackbar/snackbar-actions";
11 import { projectPanelDataActions } from "store/project-panel/project-panel-action-bind";
12 import { navigateToRunProcess } from "store/navigation/navigation-action";
13 import { goToStep, runProcessPanelActions } from "store/run-process-panel/run-process-panel-actions";
14 import { getResource } from "store/resources/resources";
15 import { initialize } from "redux-form";
16 import { RUN_PROCESS_BASIC_FORM, RunProcessBasicFormData } from "views/run-process-panel/run-process-basic-form";
17 import { RunProcessAdvancedFormData, RUN_PROCESS_ADVANCED_FORM } from "views/run-process-panel/run-process-advanced-form";
18 import { MOUNT_PATH_CWL_WORKFLOW, MOUNT_PATH_CWL_INPUT } from "models/process";
19 import { CommandInputParameter, getWorkflow, getWorkflowInputs, getWorkflowOutputs, WorkflowInputsData } from "models/workflow";
20 import { ProjectResource } from "models/project";
21 import { UserResource } from "models/user";
22 import { CommandOutputParameter } from "cwlts/mappings/v1.0/CommandOutputParameter";
23 import { ContainerRequestState } from "models/container-request";
24 import { FilterBuilder } from "services/api/filter-builder";
25 import { selectedToArray } from "components/multiselect-toolbar/MultiselectToolbar";
26 import { Resource, ResourceKind } from "models/resource";
27 import { ContextMenuResource } from "store/context-menu/context-menu-actions";
28 import { CommonResourceServiceError, getCommonResourceServiceError } from "services/common-service/common-resource-service";
30 export const loadContainers =
31 (containerUuids: string[], loadMounts: boolean = true) =>
32 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
34 filters: new FilterBuilder().addIn("uuid", containerUuids).getFilters(),
35 limit: containerUuids.length,
38 args.select = containerFieldsNoMounts;
40 const { items } = await services.containerService.list(args);
41 dispatch<any>(updateResources(items));
45 // Until the api supports unselecting fields, we need a list of all other fields to omit mounts
46 const containerFieldsNoMounts = [
59 "interactive_session_started",
65 "modified_by_user_uuid",
68 "output_storage_classes",
73 "runtime_auth_scopes",
74 "runtime_constraints",
77 "scheduling_parameters",
84 export const cancelRunningWorkflow = (uuid: string) => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
86 const process = await services.containerRequestService.update(uuid, { priority: 0 });
87 dispatch<any>(updateResources([process]));
88 if (process.containerUuid) {
89 const container = await services.containerService.get(process.containerUuid, false);
90 dispatch<any>(updateResources([container]));
94 throw new Error("Could not cancel the process.");
98 export const resumeOnHoldWorkflow = (uuid: string) => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
100 const process = await services.containerRequestService.update(uuid, { priority: 500 });
101 dispatch<any>(updateResources([process]));
102 if (process.containerUuid) {
103 const container = await services.containerService.get(process.containerUuid, false);
104 dispatch<any>(updateResources([container]));
108 throw new Error("Could not resume the process.");
112 export const startWorkflow = (uuid: string) => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
114 const process = await services.containerRequestService.update(uuid, { state: ContainerRequestState.COMMITTED });
116 dispatch<any>(updateResources([process]));
117 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Process started", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
119 dispatch<any>(snackbarActions.OPEN_SNACKBAR({ message: `Failed to start process`, kind: SnackbarKind.ERROR }));
122 dispatch<any>(snackbarActions.OPEN_SNACKBAR({ message: `Failed to start process`, kind: SnackbarKind.ERROR }));
126 export const reRunProcess =
127 (processUuid: string, workflowUuid: string) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
128 const process = getResource<any>(processUuid)(getState().resources);
129 const workflows = getState().runProcessPanel.searchWorkflows;
130 const workflow = workflows.find(workflow => workflow.uuid === workflowUuid);
131 if (workflow && process) {
132 const mainWf = getWorkflow(process.mounts[MOUNT_PATH_CWL_WORKFLOW]);
134 mainWf.inputs = getInputs(process);
136 const stringifiedDefinition = JSON.stringify(process.mounts[MOUNT_PATH_CWL_WORKFLOW].content);
137 const newWorkflow = { ...workflow, definition: stringifiedDefinition };
139 const owner = getResource<ProjectResource | UserResource>(workflow.ownerUuid)(getState().resources);
140 const basicInitialData: RunProcessBasicFormData = { name: `Copy of: ${process.name}`, owner };
141 dispatch<any>(initialize(RUN_PROCESS_BASIC_FORM, basicInitialData));
143 const advancedInitialData: RunProcessAdvancedFormData = {
144 description: process.description,
145 output: process.outputName,
146 runtime: process.schedulingParameters.max_run_time,
147 ram: process.runtimeConstraints.ram,
148 vcpus: process.runtimeConstraints.vcpus,
149 keep_cache_ram: process.runtimeConstraints.keep_cache_ram,
150 acr_container_image: process.containerImage,
152 dispatch<any>(initialize(RUN_PROCESS_ADVANCED_FORM, advancedInitialData));
154 dispatch<any>(navigateToRunProcess);
155 dispatch<any>(goToStep(1));
156 dispatch(runProcessPanelActions.SET_STEP_CHANGED(true));
157 dispatch(runProcessPanelActions.SET_SELECTED_WORKFLOW(newWorkflow));
159 dispatch<any>(snackbarActions.OPEN_SNACKBAR({ message: `You can't re-run this process`, kind: SnackbarKind.ERROR }));
164 * Fetches raw inputs from containerRequest mounts with fallback to properties
165 * Returns undefined if containerRequest not loaded
166 * Returns {} if inputs not found in mounts or props
168 export const getRawInputs = (data: any): WorkflowInputsData | undefined => {
172 const mountInput = data.mounts?.[MOUNT_PATH_CWL_INPUT]?.content;
173 const propsInput = data.properties?.cwl_input;
174 if (!mountInput && !propsInput) {
177 return mountInput || propsInput;
180 export const getInputs = (data: any): CommandInputParameter[] => {
181 // Definitions from mounts are needed so we return early if missing
182 if (!data || !data.mounts || !data.mounts[MOUNT_PATH_CWL_WORKFLOW]) {
185 const content = getRawInputs(data) as any;
186 // Only escape if content is falsy to allow displaying definitions if no inputs are present
187 // (Don't check raw content length)
192 const inputs = getWorkflowInputs(data.mounts[MOUNT_PATH_CWL_WORKFLOW].content);
194 ? inputs.map((it: any) => ({
198 default: content[it.id],
199 value: content[it.id.split("/").pop()] || [],
206 * Fetches raw outputs from containerRequest properties
207 * Assumes containerRequest is loaded
209 export const getRawOutputs = (data: any): CommandInputParameter[] | undefined => {
210 if (!data || !data.properties || !data.properties.cwl_output) {
213 return data.properties.cwl_output;
216 export type InputCollectionMount = {
221 export const getInputCollectionMounts = (data: any): InputCollectionMount[] => {
222 if (!data || !data.mounts) {
225 return Object.keys(data.mounts)
230 .filter(mount => mount.kind === "collection" && mount.portable_data_hash && mount.path)
233 pdh: mount.portable_data_hash,
237 export const getOutputParameters = (data: any): CommandOutputParameter[] => {
238 if (!data || !data.mounts || !data.mounts[MOUNT_PATH_CWL_WORKFLOW]) {
241 const outputs = getWorkflowOutputs(data.mounts[MOUNT_PATH_CWL_WORKFLOW].content);
243 ? outputs.map((it: any) => ({
252 export const openRemoveProcessDialog =
253 (resource: ContextMenuResource, numOfProcesses: Number) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
254 const confirmationText =
256 ? "Are you sure you want to remove this process?"
257 : `Are you sure you want to remove these ${numOfProcesses} processes?`;
258 const titleText = numOfProcesses === 1 ? "Remove process permanently" : "Remove processes permanently";
261 dialogActions.OPEN_DIALOG({
262 id: REMOVE_PROCESS_DIALOG,
265 text: confirmationText,
266 confirmButtonLabel: "Remove",
274 export const REMOVE_PROCESS_DIALOG = "removeProcessDialog";
276 export const removeProcessPermanently = (uuid: string) => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
277 const resource = getState().dialog.removeProcessDialog.data.resource;
278 const checkedList = getState().multiselect.checkedList;
280 const uuidsToRemove: string[] = resource.fromContextMenu ? [resource.uuid] : selectedToArray(checkedList);
282 //if no items in checkedlist, default to normal context menu behavior
283 if (!uuidsToRemove.length) uuidsToRemove.push(uuid);
285 const processesToRemove = uuidsToRemove
286 .map(uuid => getResource(uuid)(getState().resources) as Resource)
287 .filter(resource => resource.kind === ResourceKind.PROCESS);
289 for (const process of processesToRemove) {
291 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Removing ...", kind: SnackbarKind.INFO }));
292 await services.containerRequestService.delete(process.uuid, false);
293 dispatch(projectPanelDataActions.REQUEST_ITEMS());
294 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Removed.", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
296 const error = getCommonResourceServiceError(e);
297 if (error === CommonResourceServiceError.PERMISSION_ERROR_FORBIDDEN) {
298 dispatch(snackbarActions.OPEN_SNACKBAR({ message: `Access denied`, hideDuration: 2000, kind: SnackbarKind.ERROR }));
300 dispatch(snackbarActions.OPEN_SNACKBAR({ message: `Deletion failed`, hideDuration: 2000, kind: SnackbarKind.ERROR }));