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';
24 import { matchProjectRoute } from '~/routes/routes';
26 export const runProcessPanelActions = unionize({
27 SET_PROCESS_PATHNAME: ofType<string>(),
28 SET_PROCESS_OWNER_UUID: ofType<string>(),
29 SET_CURRENT_STEP: ofType<number>(),
30 SET_STEP_CHANGED: ofType<boolean>(),
31 SET_WORKFLOWS: ofType<WorkflowResource[]>(),
32 SET_SELECTED_WORKFLOW: ofType<WorkflowResource>(),
33 SET_WORKFLOW_PRESETS: ofType<WorkflowResource[]>(),
34 SELECT_WORKFLOW_PRESET: ofType<WorkflowResource>(),
35 SEARCH_WORKFLOWS: ofType<string>(),
36 RESET_RUN_PROCESS_PANEL: ofType<{}>(),
39 export interface RunProcessSecondStepDataFormProps {
44 export const SET_WORKFLOW_DIALOG = 'setWorkflowDialog';
45 export const RUN_PROCESS_SECOND_STEP_FORM_NAME = 'runProcessSecondStepFormName';
47 export type RunProcessPanelAction = UnionOf<typeof runProcessPanelActions>;
49 export const loadRunProcessPanel = () =>
50 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
52 dispatch(setBreadcrumbs([{ label: 'Run Process' }]));
53 const response = await services.workflowService.list();
54 dispatch(runProcessPanelActions.SET_WORKFLOWS(response.items));
60 export const openSetWorkflowDialog = (workflow: WorkflowResource) =>
61 (dispatch: Dispatch, getState: () => RootState) => {
62 const selectedWorkflow = getState().runProcessPanel.selectedWorkflow;
63 const isStepChanged = getState().runProcessPanel.isStepChanged;
64 if (isStepChanged && selectedWorkflow && selectedWorkflow.uuid !== workflow.uuid) {
65 dispatch(dialogActions.OPEN_DIALOG({
66 id: SET_WORKFLOW_DIALOG,
68 title: 'Form will be cleared',
69 text: 'Changing a workflow will clean all input fields in next step.',
70 confirmButtonLabel: 'Change Workflow',
75 dispatch<any>(setWorkflow(workflow, false));
79 export const getWorkflowRunnerSettings = (workflow: WorkflowResource) => {
80 const advancedFormValues = {};
81 Object.assign(advancedFormValues, DEFAULT_ADVANCED_FORM_VALUES);
83 const wf = getWorkflow(parseWorkflowDefinition(workflow));
84 const hints = wf ? wf.hints : undefined;
86 const resc = hints.find(item => item.class === 'http://arvados.org/cwl#WorkflowRunnerResources') as WorkflowRunnerResources | undefined;
88 if (resc.ramMin) { advancedFormValues[RAM_FIELD] = resc.ramMin; }
89 if (resc.coresMin) { advancedFormValues[VCPUS_FIELD] = resc.coresMin; }
90 if (resc.keep_cache) { advancedFormValues[KEEP_CACHE_RAM_FIELD] = resc.keep_cache; }
91 if (resc.acrContainerImage) { advancedFormValues[RUNNER_IMAGE_FIELD] = resc.acrContainerImage; }
94 return advancedFormValues;
97 export const setWorkflow = (workflow: WorkflowResource, isWorkflowChanged = true) =>
98 (dispatch: Dispatch<any>, getState: () => RootState) => {
99 const isStepChanged = getState().runProcessPanel.isStepChanged;
101 const advancedFormValues = getWorkflowRunnerSettings(workflow);
103 if (isStepChanged && isWorkflowChanged) {
104 dispatch(runProcessPanelActions.SET_STEP_CHANGED(false));
105 dispatch(runProcessPanelActions.SET_SELECTED_WORKFLOW(workflow));
106 dispatch<any>(loadPresets(workflow.uuid));
107 dispatch(initialize(RUN_PROCESS_ADVANCED_FORM, advancedFormValues));
109 if (!isWorkflowChanged) {
110 dispatch(runProcessPanelActions.SET_SELECTED_WORKFLOW(workflow));
111 dispatch<any>(loadPresets(workflow.uuid));
112 dispatch(initialize(RUN_PROCESS_ADVANCED_FORM, advancedFormValues));
116 export const loadPresets = (workflowUuid: string) =>
117 async (dispatch: Dispatch<any>, _: () => RootState, { workflowService }: ServiceRepository) => {
118 const { items } = await workflowService.presets(workflowUuid);
119 dispatch(runProcessPanelActions.SET_WORKFLOW_PRESETS(items));
122 export const selectPreset = (preset: WorkflowResource) =>
123 (dispatch: Dispatch<any>) => {
124 dispatch(runProcessPanelActions.SELECT_WORKFLOW_PRESET(preset));
125 const inputs = getWorkflowInputs(parseWorkflowDefinition(preset)) || [];
126 const values = inputs.reduce((values, input) => ({
128 [input.id]: input.default,
130 dispatch(initialize(RUN_PROCESS_INPUTS_FORM, values));
133 export const goToStep = (step: number) =>
134 (dispatch: Dispatch) => {
136 dispatch(runProcessPanelActions.SET_STEP_CHANGED(true));
138 dispatch(runProcessPanelActions.SET_CURRENT_STEP(step));
141 export const runProcess = async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
142 const state = getState();
143 const basicForm = getFormValues(RUN_PROCESS_BASIC_FORM)(state) as RunProcessBasicFormData;
144 const inputsForm = getFormValues(RUN_PROCESS_INPUTS_FORM)(state) as WorkflowInputsData;
145 const userUuid = getUserUuid(getState());
146 if (!userUuid) { return; }
147 const pathname = getState().runProcessPanel.processPathname;
148 const { processOwnerUuid, selectedWorkflow } = state.runProcessPanel;
149 const ownerUUid = !matchProjectRoute(pathname) ? userUuid : processOwnerUuid;
150 if (selectedWorkflow) {
151 const advancedForm = getFormValues(RUN_PROCESS_ADVANCED_FORM)(state) as RunProcessAdvancedFormData || getWorkflowRunnerSettings(selectedWorkflow);
152 const newProcessData = {
153 ownerUuid: ownerUUid,
154 name: basicForm.name,
155 description: basicForm.description,
156 state: ContainerRequestState.COMMITTED,
157 mounts: createWorkflowMounts(selectedWorkflow, normalizeInputKeys(inputsForm)),
158 runtimeConstraints: {
160 vcpus: advancedForm[VCPUS_FIELD],
161 ram: advancedForm[RAM_FIELD],
163 schedulingParameters: {
164 max_run_time: advancedForm[RUNTIME_FIELD]
166 containerImage: advancedForm[RUNNER_IMAGE_FIELD],
167 cwd: '/var/spool/cwl',
169 'arvados-cwl-runner',
172 `--project-uuid=${ownerUUid}`,
173 '/var/lib/cwl/workflow.json#main',
174 '/var/lib/cwl/cwl.input.json'
176 outputPath: '/var/spool/cwl',
178 outputName: advancedForm[OUTPUT_FIELD] ? advancedForm[OUTPUT_FIELD] : undefined,
180 workflowUuid: selectedWorkflow.uuid,
181 workflowName: selectedWorkflow.name
184 const newProcess = await services.containerRequestService.create(newProcessData);
185 dispatch(navigateTo(newProcess.uuid));
189 export const DEFAULT_ADVANCED_FORM_VALUES: Partial<RunProcessAdvancedFormData> = {
191 [RAM_FIELD]: 1073741824,
192 [RUNNER_IMAGE_FIELD]: "arvados/jobs"
195 const normalizeInputKeys = (inputs: WorkflowInputsData): WorkflowInputsData =>
196 Object.keys(inputs).reduce((normalizedInputs, key) => ({
198 [key.split('/').slice(1).join('/')]: inputs[key],
200 export const searchWorkflows = (term: string) => runProcessPanelActions.SEARCH_WORKFLOWS(term);