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 { getResource } from 'store/resources/resources';
25 import { ProjectResource } from "models/project";
26 import { UserResource } from "models/user";
28 export const runProcessPanelActions = unionize({
29 SET_PROCESS_PATHNAME: ofType<string>(),
30 SET_PROCESS_OWNER_UUID: ofType<string>(),
31 SET_CURRENT_STEP: ofType<number>(),
32 SET_STEP_CHANGED: ofType<boolean>(),
33 SET_WORKFLOWS: ofType<WorkflowResource[]>(),
34 SET_SELECTED_WORKFLOW: ofType<WorkflowResource>(),
35 SET_WORKFLOW_PRESETS: ofType<WorkflowResource[]>(),
36 SELECT_WORKFLOW_PRESET: ofType<WorkflowResource>(),
37 SEARCH_WORKFLOWS: ofType<string>(),
38 RESET_RUN_PROCESS_PANEL: ofType<{}>(),
41 export interface RunProcessSecondStepDataFormProps {
46 export const SET_WORKFLOW_DIALOG = 'setWorkflowDialog';
47 export const RUN_PROCESS_SECOND_STEP_FORM_NAME = 'runProcessSecondStepFormName';
49 export type RunProcessPanelAction = UnionOf<typeof runProcessPanelActions>;
51 export const loadRunProcessPanel = () =>
52 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
54 dispatch(setBreadcrumbs([{ label: 'Run Process' }]));
55 const response = await services.workflowService.list();
56 dispatch(runProcessPanelActions.SET_WORKFLOWS(response.items));
62 export const openSetWorkflowDialog = (workflow: WorkflowResource) =>
63 (dispatch: Dispatch, getState: () => RootState) => {
64 const selectedWorkflow = getState().runProcessPanel.selectedWorkflow;
65 const isStepChanged = getState().runProcessPanel.isStepChanged;
66 if (isStepChanged && selectedWorkflow && selectedWorkflow.uuid !== workflow.uuid) {
67 dispatch(dialogActions.OPEN_DIALOG({
68 id: SET_WORKFLOW_DIALOG,
70 title: 'Form will be cleared',
71 text: 'Changing a workflow will clean all input fields in next step.',
72 confirmButtonLabel: 'Change Workflow',
77 dispatch<any>(setWorkflow(workflow, false));
81 export const getWorkflowRunnerSettings = (workflow: WorkflowResource) => {
82 const advancedFormValues = {};
83 Object.assign(advancedFormValues, DEFAULT_ADVANCED_FORM_VALUES);
85 const wf = getWorkflow(parseWorkflowDefinition(workflow));
86 const hints = wf ? wf.hints : undefined;
88 const resc = hints.find(item => item.class === 'http://arvados.org/cwl#WorkflowRunnerResources') as WorkflowRunnerResources | undefined;
90 if (resc.ramMin) { advancedFormValues[RAM_FIELD] = resc.ramMin * (1024 * 1024); }
91 if (resc.coresMin) { advancedFormValues[VCPUS_FIELD] = resc.coresMin; }
92 if (resc.keep_cache) { advancedFormValues[KEEP_CACHE_RAM_FIELD] = resc.keep_cache * (1024 * 1024); }
93 if (resc.acrContainerImage) { advancedFormValues[RUNNER_IMAGE_FIELD] = resc.acrContainerImage; }
96 return advancedFormValues;
99 export const setWorkflow = (workflow: WorkflowResource, isWorkflowChanged = true) =>
100 (dispatch: Dispatch<any>, getState: () => RootState) => {
101 const isStepChanged = getState().runProcessPanel.isStepChanged;
103 const advancedFormValues = getWorkflowRunnerSettings(workflow);
105 let owner = getResource<ProjectResource | UserResource>(getState().runProcessPanel.processOwnerUuid)(getState().resources);
106 if (!owner || !owner.canWrite) {
110 if (isStepChanged && isWorkflowChanged) {
111 dispatch(runProcessPanelActions.SET_STEP_CHANGED(false));
112 dispatch(runProcessPanelActions.SET_SELECTED_WORKFLOW(workflow));
113 dispatch<any>(loadPresets(workflow.uuid));
114 dispatch(initialize(RUN_PROCESS_BASIC_FORM, { name: workflow.name, owner }));
115 dispatch(initialize(RUN_PROCESS_ADVANCED_FORM, advancedFormValues));
117 if (!isWorkflowChanged) {
118 dispatch(runProcessPanelActions.SET_SELECTED_WORKFLOW(workflow));
119 dispatch<any>(loadPresets(workflow.uuid));
120 dispatch(initialize(RUN_PROCESS_BASIC_FORM, { name: workflow.name, owner }));
121 dispatch(initialize(RUN_PROCESS_ADVANCED_FORM, advancedFormValues));
125 export const loadPresets = (workflowUuid: string) =>
126 async (dispatch: Dispatch<any>, _: () => RootState, { workflowService }: ServiceRepository) => {
127 const { items } = await workflowService.presets(workflowUuid);
128 dispatch(runProcessPanelActions.SET_WORKFLOW_PRESETS(items));
131 export const selectPreset = (preset: WorkflowResource) =>
132 (dispatch: Dispatch<any>) => {
133 dispatch(runProcessPanelActions.SELECT_WORKFLOW_PRESET(preset));
134 const inputs = getWorkflowInputs(parseWorkflowDefinition(preset)) || [];
135 const values = inputs.reduce((values, input) => ({
137 [input.id]: input.default,
139 dispatch(initialize(RUN_PROCESS_INPUTS_FORM, values));
142 export const goToStep = (step: number) =>
143 (dispatch: Dispatch) => {
145 dispatch(runProcessPanelActions.SET_STEP_CHANGED(true));
147 dispatch(runProcessPanelActions.SET_CURRENT_STEP(step));
150 export const runProcess = async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
151 const state = getState();
152 const basicForm = getFormValues(RUN_PROCESS_BASIC_FORM)(state) as RunProcessBasicFormData;
153 const inputsForm = getFormValues(RUN_PROCESS_INPUTS_FORM)(state) as WorkflowInputsData;
154 const userUuid = getUserUuid(getState());
155 if (!userUuid) { return; }
156 const { processOwnerUuid, selectedWorkflow } = state.runProcessPanel;
157 const ownerUUid = basicForm.owner ? basicForm.owner.uuid : (processOwnerUuid ? processOwnerUuid : userUuid);
158 if (selectedWorkflow) {
159 const advancedForm = getFormValues(RUN_PROCESS_ADVANCED_FORM)(state) as RunProcessAdvancedFormData || getWorkflowRunnerSettings(selectedWorkflow);
160 const newProcessData = {
161 ownerUuid: ownerUUid,
162 name: basicForm.name,
163 description: basicForm.description,
164 state: ContainerRequestState.COMMITTED,
165 mounts: createWorkflowMounts(selectedWorkflow, normalizeInputKeys(inputsForm)),
166 runtimeConstraints: {
168 vcpus: advancedForm[VCPUS_FIELD],
169 ram: (advancedForm[KEEP_CACHE_RAM_FIELD] + advancedForm[RAM_FIELD]),
171 schedulingParameters: {
172 max_run_time: advancedForm[RUNTIME_FIELD]
174 containerImage: advancedForm[RUNNER_IMAGE_FIELD],
175 cwd: '/var/spool/cwl',
177 'arvados-cwl-runner',
180 `--project-uuid=${ownerUUid}`,
181 '/var/lib/cwl/workflow.json#main',
182 '/var/lib/cwl/cwl.input.json'
184 outputPath: '/var/spool/cwl',
186 outputName: advancedForm[OUTPUT_FIELD] ? advancedForm[OUTPUT_FIELD] : `Output from ${basicForm.name}`,
188 template_uuid: selectedWorkflow.uuid,
189 workflowName: selectedWorkflow.name
193 const newProcess = await services.containerRequestService.create(newProcessData);
194 dispatch(navigateTo(newProcess.uuid));
198 const DEFAULT_ADVANCED_FORM_VALUES: Partial<RunProcessAdvancedFormData> = {
200 [RAM_FIELD]: 1073741824,
201 [KEEP_CACHE_RAM_FIELD]: 268435456,
202 [RUNNER_IMAGE_FIELD]: "arvados/jobs"
205 const normalizeInputKeys = (inputs: WorkflowInputsData): WorkflowInputsData =>
206 Object.keys(inputs).reduce((normalizedInputs, key) => ({
208 [key.split('/').slice(1).join('/')]: inputs[key],
210 export const searchWorkflows = (term: string) => runProcessPanelActions.SEARCH_WORKFLOWS(term);