1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Dispatch } from 'redux';
6 import { unionize, ofType, UnionOf } from "common/unionize";
7 import { ServiceRepository } from "services/services";
8 import { RootState } from 'store/store';
9 import { getUserUuid } from "common/getuser";
10 import { WorkflowResource, WorkflowRunnerResources, getWorkflow, getWorkflowInputs, parseWorkflowDefinition } from 'models/workflow';
11 import { getFormValues, initialize } from 'redux-form';
12 import { RUN_PROCESS_BASIC_FORM, RunProcessBasicFormData } from 'views/run-process-panel/run-process-basic-form';
13 import { RUN_PROCESS_INPUTS_FORM } from 'views/run-process-panel/run-process-inputs-form';
14 import { WorkflowInputsData } from 'models/workflow';
15 import { createWorkflowMounts } from 'models/process';
16 import { ContainerRequestState } from 'models/container-request';
17 import { navigateTo } from '../navigation/navigation-action';
19 RunProcessAdvancedFormData, RUN_PROCESS_ADVANCED_FORM, VCPUS_FIELD,
20 KEEP_CACHE_RAM_FIELD, RAM_FIELD, RUNTIME_FIELD, OUTPUT_FIELD, RUNNER_IMAGE_FIELD
21 } from 'views/run-process-panel/run-process-advanced-form';
22 import { dialogActions } from 'store/dialog/dialog-actions';
23 import { setBreadcrumbs } from 'store/breadcrumbs/breadcrumbs-actions';
25 export const runProcessPanelActions = unionize({
26 SET_PROCESS_PATHNAME: ofType<string>(),
27 SET_PROCESS_OWNER_UUID: ofType<string>(),
28 SET_CURRENT_STEP: ofType<number>(),
29 SET_STEP_CHANGED: ofType<boolean>(),
30 SET_WORKFLOWS: ofType<WorkflowResource[]>(),
31 SET_SELECTED_WORKFLOW: ofType<WorkflowResource>(),
32 SET_WORKFLOW_PRESETS: ofType<WorkflowResource[]>(),
33 SELECT_WORKFLOW_PRESET: ofType<WorkflowResource>(),
34 SEARCH_WORKFLOWS: ofType<string>(),
35 RESET_RUN_PROCESS_PANEL: ofType<{}>(),
38 export interface RunProcessSecondStepDataFormProps {
43 export const SET_WORKFLOW_DIALOG = 'setWorkflowDialog';
44 export const RUN_PROCESS_SECOND_STEP_FORM_NAME = 'runProcessSecondStepFormName';
46 export type RunProcessPanelAction = UnionOf<typeof runProcessPanelActions>;
48 export const loadRunProcessPanel = () =>
49 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
51 dispatch(setBreadcrumbs([{ label: 'Run Process' }]));
52 const response = await services.workflowService.list();
53 dispatch(runProcessPanelActions.SET_WORKFLOWS(response.items));
59 export const openSetWorkflowDialog = (workflow: WorkflowResource) =>
60 (dispatch: Dispatch, getState: () => RootState) => {
61 const selectedWorkflow = getState().runProcessPanel.selectedWorkflow;
62 const isStepChanged = getState().runProcessPanel.isStepChanged;
63 if (isStepChanged && selectedWorkflow && selectedWorkflow.uuid !== workflow.uuid) {
64 dispatch(dialogActions.OPEN_DIALOG({
65 id: SET_WORKFLOW_DIALOG,
67 title: 'Form will be cleared',
68 text: 'Changing a workflow will clean all input fields in next step.',
69 confirmButtonLabel: 'Change Workflow',
74 dispatch<any>(setWorkflow(workflow, false));
78 export const getWorkflowRunnerSettings = (workflow: WorkflowResource) => {
79 const advancedFormValues = {};
80 Object.assign(advancedFormValues, DEFAULT_ADVANCED_FORM_VALUES);
82 const wf = getWorkflow(parseWorkflowDefinition(workflow));
83 const hints = wf ? wf.hints : undefined;
85 const resc = hints.find(item => item.class === 'http://arvados.org/cwl#WorkflowRunnerResources') as WorkflowRunnerResources | undefined;
87 if (resc.ramMin) { advancedFormValues[RAM_FIELD] = resc.ramMin * (1024 * 1024); }
88 if (resc.coresMin) { advancedFormValues[VCPUS_FIELD] = resc.coresMin; }
89 if (resc.keep_cache) { advancedFormValues[KEEP_CACHE_RAM_FIELD] = resc.keep_cache * (1024 * 1024); }
90 if (resc.acrContainerImage) { advancedFormValues[RUNNER_IMAGE_FIELD] = resc.acrContainerImage; }
93 return advancedFormValues;
96 export const setWorkflow = (workflow: WorkflowResource, isWorkflowChanged = true) =>
97 (dispatch: Dispatch<any>, getState: () => RootState) => {
98 const isStepChanged = getState().runProcessPanel.isStepChanged;
100 const advancedFormValues = getWorkflowRunnerSettings(workflow);
102 if (isStepChanged && isWorkflowChanged) {
103 dispatch(runProcessPanelActions.SET_STEP_CHANGED(false));
104 dispatch(runProcessPanelActions.SET_SELECTED_WORKFLOW(workflow));
105 dispatch<any>(loadPresets(workflow.uuid));
106 dispatch(initialize(RUN_PROCESS_ADVANCED_FORM, advancedFormValues));
108 if (!isWorkflowChanged) {
109 dispatch(runProcessPanelActions.SET_SELECTED_WORKFLOW(workflow));
110 dispatch<any>(loadPresets(workflow.uuid));
111 dispatch(initialize(RUN_PROCESS_ADVANCED_FORM, advancedFormValues));
115 export const loadPresets = (workflowUuid: string) =>
116 async (dispatch: Dispatch<any>, _: () => RootState, { workflowService }: ServiceRepository) => {
117 const { items } = await workflowService.presets(workflowUuid);
118 dispatch(runProcessPanelActions.SET_WORKFLOW_PRESETS(items));
121 export const selectPreset = (preset: WorkflowResource) =>
122 (dispatch: Dispatch<any>) => {
123 dispatch(runProcessPanelActions.SELECT_WORKFLOW_PRESET(preset));
124 const inputs = getWorkflowInputs(parseWorkflowDefinition(preset)) || [];
125 const values = inputs.reduce((values, input) => ({
127 [input.id]: input.default,
129 dispatch(initialize(RUN_PROCESS_INPUTS_FORM, values));
132 export const goToStep = (step: number) =>
133 (dispatch: Dispatch) => {
135 dispatch(runProcessPanelActions.SET_STEP_CHANGED(true));
137 dispatch(runProcessPanelActions.SET_CURRENT_STEP(step));
140 export const runProcess = async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
141 const state = getState();
142 const basicForm = getFormValues(RUN_PROCESS_BASIC_FORM)(state) as RunProcessBasicFormData;
143 const inputsForm = getFormValues(RUN_PROCESS_INPUTS_FORM)(state) as WorkflowInputsData;
144 const userUuid = getUserUuid(getState());
145 if (!userUuid) { return; }
146 const { processOwnerUuid, selectedWorkflow } = state.runProcessPanel;
147 const ownerUUid = processOwnerUuid ? processOwnerUuid : userUuid;
148 if (selectedWorkflow) {
149 const advancedForm = getFormValues(RUN_PROCESS_ADVANCED_FORM)(state) as RunProcessAdvancedFormData || getWorkflowRunnerSettings(selectedWorkflow);
150 const newProcessData = {
151 ownerUuid: ownerUUid,
152 name: basicForm.name,
153 description: basicForm.description,
154 state: ContainerRequestState.COMMITTED,
155 mounts: createWorkflowMounts(selectedWorkflow, normalizeInputKeys(inputsForm)),
156 runtimeConstraints: {
158 vcpus: advancedForm[VCPUS_FIELD],
159 ram: (advancedForm[KEEP_CACHE_RAM_FIELD] + advancedForm[RAM_FIELD]),
161 schedulingParameters: {
162 max_run_time: advancedForm[RUNTIME_FIELD]
164 containerImage: advancedForm[RUNNER_IMAGE_FIELD],
165 cwd: '/var/spool/cwl',
167 'arvados-cwl-runner',
170 `--project-uuid=${ownerUUid}`,
171 '/var/lib/cwl/workflow.json#main',
172 '/var/lib/cwl/cwl.input.json'
174 outputPath: '/var/spool/cwl',
176 outputName: advancedForm[OUTPUT_FIELD] ? advancedForm[OUTPUT_FIELD] : undefined,
178 workflowUuid: selectedWorkflow.uuid,
179 workflowName: selectedWorkflow.name
182 const newProcess = await services.containerRequestService.create(newProcessData);
183 dispatch(navigateTo(newProcess.uuid));
187 const DEFAULT_ADVANCED_FORM_VALUES: Partial<RunProcessAdvancedFormData> = {
189 [RAM_FIELD]: 1073741824,
190 [KEEP_CACHE_RAM_FIELD]: 268435456,
191 [RUNNER_IMAGE_FIELD]: "arvados/jobs"
194 const normalizeInputKeys = (inputs: WorkflowInputsData): WorkflowInputsData =>
195 Object.keys(inputs).reduce((normalizedInputs, key) => ({
197 [key.split('/').slice(1).join('/')]: inputs[key],
199 export const searchWorkflows = (term: string) => runProcessPanelActions.SEARCH_WORKFLOWS(term);