21535: added timeouts to test spec Arvados-DCO-1.1-Signed-off-by: Lisa Knox <lisa...
[arvados.git] / services / workbench2 / src / store / processes / process-copy-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Dispatch } from 'redux';
6 import { dialogActions } from 'store/dialog/dialog-actions';
7 import { initialize, startSubmit } from 'redux-form';
8 import { resetPickerProjectTree } from 'store/project-tree-picker/project-tree-picker-actions';
9 import { RootState } from 'store/store';
10 import { ServiceRepository } from 'services/services';
11 import { CopyFormDialogData } from 'store/copy-dialog/copy-dialog';
12 import { getProcess } from 'store/processes/process';
13 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
14 import { initProjectsTreePicker } from 'store/tree-picker/tree-picker-actions';
15 import { ContainerRequestState } from 'models/container-request';
16
17 export const PROCESS_COPY_FORM_NAME = 'processCopyFormName';
18 export const MULTI_PROCESS_COPY_FORM_NAME = 'multiProcessCopyFormName';
19
20 export const openCopyProcessDialog =
21     (resource: { name: string; uuid: string }) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
22         const process = getProcess(resource.uuid)(getState().resources);
23         if (process) {
24             dispatch<any>(resetPickerProjectTree());
25             dispatch<any>(initProjectsTreePicker(PROCESS_COPY_FORM_NAME));
26             const initialData: CopyFormDialogData = { name: `Copy of: ${resource.name}`, uuid: resource.uuid, ownerUuid: '' };
27             dispatch<any>(initialize(PROCESS_COPY_FORM_NAME, initialData));
28             dispatch(dialogActions.OPEN_DIALOG({ id: PROCESS_COPY_FORM_NAME, data: {} }));
29         } else {
30             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process not found', hideDuration: 2000, kind: SnackbarKind.ERROR }));
31         }
32     };
33
34 export const copyProcess = (resource: CopyFormDialogData) => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
35     dispatch(startSubmit(PROCESS_COPY_FORM_NAME));
36     try {
37         const process = await services.containerRequestService.get(resource.uuid);
38         const {
39             command,
40             containerCountMax,
41             containerImage,
42             cwd,
43             description,
44             environment,
45             kind,
46             mounts,
47             outputName,
48             outputPath,
49             outputProperties,
50             outputStorageClasses,
51             outputTtl,
52             properties,
53             runtimeConstraints,
54             schedulingParameters,
55             useExisting,
56         } = process;
57         const newProcess = await services.containerRequestService.create({
58             command,
59             containerCountMax,
60             containerImage,
61             cwd,
62             description,
63             environment,
64             kind,
65             mounts,
66             name: resource.name,
67             outputName,
68             outputPath,
69             outputProperties,
70             outputStorageClasses,
71             outputTtl,
72             ownerUuid: resource.ownerUuid,
73             priority: 500,
74             properties,
75             runtimeConstraints,
76             schedulingParameters,
77             state: ContainerRequestState.UNCOMMITTED,
78             useExisting,
79         });
80         dispatch(dialogActions.CLOSE_DIALOG({ id: PROCESS_COPY_FORM_NAME }));
81         return newProcess;
82     } catch (e) {
83         dispatch(dialogActions.CLOSE_DIALOG({ id: PROCESS_COPY_FORM_NAME }));
84         throw new Error('Could not copy the process.');
85     }
86 };