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 { WorkflowResource } from '~/models/workflow';
10 import { getFormValues } from 'redux-form';
11 import { RUN_PROCESS_BASIC_FORM, RunProcessBasicFormData } from '~/views/run-process-panel/run-process-basic-form';
12 import { RUN_PROCESS_INPUTS_FORM } from '~/views/run-process-panel/run-process-inputs-form';
13 import { WorkflowInputsData } from '~/models/workflow';
14 import { createWorkflowMounts } from '~/models/process';
15 import { ContainerRequestState } from '~/models/container-request';
16 import { navigateToProcess } from '../navigation/navigation-action';
17 import { RunProcessAdvancedFormData, RUN_PROCESS_ADVANCED_FORM } from '~/views/run-process-panel/run-process-advanced-form';
18 import { isItemNotInProject, isProjectOrRunProcessRoute } from '~/store/projects/project-create-actions';
19 import { dialogActions } from '~/store/dialog/dialog-actions';
20 import { setBreadcrumbs } from '~/store/breadcrumbs/breadcrumbs-actions';
22 export const runProcessPanelActions = unionize({
23 SET_PROCESS_OWNER_UUID: ofType<string>(),
24 SET_CURRENT_STEP: ofType<number>(),
25 SET_STEP_CHANGED: ofType<boolean>(),
26 SET_WORKFLOWS: ofType<WorkflowResource[]>(),
27 SET_SELECTED_WORKFLOW: ofType<WorkflowResource>(),
28 SEARCH_WORKFLOWS: ofType<string>(),
29 RESET_RUN_PROCESS_PANEL: ofType<{}>(),
32 export interface RunProcessSecondStepDataFormProps {
37 export const SET_WORKFLOW_DIALOG = 'setWorkflowDialog';
38 export const RUN_PROCESS_SECOND_STEP_FORM_NAME = 'runProcessSecondStepFormName';
40 export type RunProcessPanelAction = UnionOf<typeof runProcessPanelActions>;
42 export const loadRunProcessPanel = () =>
43 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
45 dispatch(setBreadcrumbs([{ label: 'Run Process' }]));
46 dispatch(runProcessPanelActions.RESET_RUN_PROCESS_PANEL());
47 const response = await services.workflowService.list();
48 dispatch(runProcessPanelActions.SET_WORKFLOWS(response.items));
54 export const openSetWorkflowDialog = (workflow: WorkflowResource) =>
55 (dispatch: Dispatch, getState: () => RootState) => {
56 const selectedWorkflow = getState().runProcessPanel.selectedWorkflow;
57 const isStepChanged = getState().runProcessPanel.isStepChanged;
58 if (isStepChanged && selectedWorkflow && selectedWorkflow.uuid !== workflow.uuid) {
59 dispatch(dialogActions.OPEN_DIALOG({
60 id: SET_WORKFLOW_DIALOG,
62 title: 'Form will be cleared',
63 text: 'Changing a workflow will clean all input fields in next step.',
64 confirmButtonLabel: 'Change Workflow',
69 dispatch<any>(setWorkflow(workflow, false));
73 export const setWorkflow = (workflow: WorkflowResource, isWorkflowChanged = true) =>
74 (dispatch: Dispatch<any>, getState: () => RootState) => {
75 const isStepChanged = getState().runProcessPanel.isStepChanged;
76 if (isStepChanged && isWorkflowChanged) {
77 dispatch(runProcessPanelActions.SET_STEP_CHANGED(false));
78 dispatch(runProcessPanelActions.SET_SELECTED_WORKFLOW(workflow));
80 if (!isWorkflowChanged) {
81 dispatch(runProcessPanelActions.SET_SELECTED_WORKFLOW(workflow));
85 export const goToStep = (step: number) =>
86 (dispatch: Dispatch, getState: () => RootState) => {
88 dispatch(runProcessPanelActions.SET_STEP_CHANGED(true));
90 dispatch(runProcessPanelActions.SET_CURRENT_STEP(step));
93 export const runProcess = async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
94 const state = getState();
95 const basicForm = getFormValues(RUN_PROCESS_BASIC_FORM)(state) as RunProcessBasicFormData;
96 const inputsForm = getFormValues(RUN_PROCESS_INPUTS_FORM)(state) as WorkflowInputsData;
97 const advancedForm = getFormValues(RUN_PROCESS_ADVANCED_FORM)(state) as RunProcessAdvancedFormData;
98 const userUuid = getState().auth.user!.uuid;
99 const router = getState();
100 const properties = getState().properties;
101 const { processOwnerUuid, selectedWorkflow } = state.runProcessPanel;
102 if (selectedWorkflow) {
103 const newProcessData = {
104 ownerUuid: isItemNotInProject(properties) || !isProjectOrRunProcessRoute(router) ? userUuid : processOwnerUuid,
105 name: basicForm.name,
106 description: basicForm.description,
107 state: ContainerRequestState.COMMITTED,
108 mounts: createWorkflowMounts(selectedWorkflow, normalizeInputKeys(inputsForm)),
109 runtimeConstraints: {
114 containerImage: 'arvados/jobs',
115 cwd: '/var/spool/cwl',
117 'arvados-cwl-runner',
120 `--project-uuid=${processOwnerUuid}`,
121 '/var/lib/cwl/workflow.json#main',
122 '/var/lib/cwl/cwl.input.json'
124 outputPath: '/var/spool/cwl',
126 outputName: advancedForm && advancedForm.output ? advancedForm.output : undefined,
128 const newProcess = await services.containerRequestService.create(newProcessData);
129 dispatch(navigateToProcess(newProcess.uuid));
133 const normalizeInputKeys = (inputs: WorkflowInputsData): WorkflowInputsData =>
134 Object.keys(inputs).reduce((normalizedInputs, key) => ({
136 [key.split('/').slice(1).join('/')]: inputs[key],
138 export const searchWorkflows = (term: string) => runProcessPanelActions.SEARCH_WORKFLOWS(term);