1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { RootStore } from 'store/store';
6 import { AuthService } from 'services/auth-service/auth-service';
7 import { Config } from 'common/config';
8 import { WebSocketService } from './websocket-service';
9 import { ResourceEventMessage } from './resource-event-message';
10 import { ResourceKind } from 'models/resource';
11 import { loadProcess } from 'store/processes/processes-actions';
12 import { getProcess, getSubprocesses } from 'store/processes/process';
13 import { LogEventType } from 'models/log';
14 import { addProcessLogsPanelItem } from 'store/process-logs-panel/process-logs-panel-actions';
15 import { subprocessPanelActions } from "store/subprocess-panel/subprocess-panel-actions";
16 import { projectPanelActions } from "store/project-panel/project-panel-action";
17 import { getProjectPanelCurrentUuid } from 'store/project-panel/project-panel-action';
18 import { allProcessesPanelActions } from 'store/all-processes-panel/all-processes-panel-action';
19 import { loadCollection } from 'store/workbench/workbench-actions';
20 import { matchAllProcessesRoute, matchProjectRoute, matchProcessRoute } from 'routes/routes';
22 export const initWebSocket = (config: Config, authService: AuthService, store: RootStore) => {
23 if (config.websocketUrl) {
24 const webSocketService = new WebSocketService(config.websocketUrl, authService);
25 webSocketService.setMessageListener(messageListener(store));
26 webSocketService.connect();
28 console.warn("WARNING: Websocket ExternalURL is not set on the cluster config.");
32 const messageListener = (store: RootStore) => (message: ResourceEventMessage) => {
33 if (message.eventType === LogEventType.CREATE || message.eventType === LogEventType.UPDATE) {
34 const state = store.getState();
35 const location = state.router.location ? state.router.location.pathname : '';
36 switch (message.objectKind) {
37 case ResourceKind.COLLECTION:
38 const currentCollection = state.collectionPanel.item;
39 if (currentCollection && currentCollection.uuid === message.objectUuid) {
40 store.dispatch(loadCollection(message.objectUuid));
43 case ResourceKind.CONTAINER_REQUEST:
44 if (matchProcessRoute(location)) {
45 if (state.processPanel.containerRequestUuid === message.objectUuid) {
46 store.dispatch(loadProcess(message.objectUuid));
48 const proc = getProcess(state.processPanel.containerRequestUuid)(state.resources);
49 if (proc && proc.container && proc.container.uuid === message.properties["new_attributes"]["requesting_container_uuid"]) {
50 store.dispatch(subprocessPanelActions.REQUEST_ITEMS());
54 // fall through, this will happen for container requests as well.
55 case ResourceKind.CONTAINER:
56 if (matchProcessRoute(location)) {
57 // refresh only if this is a subprocess of the currently displayed process.
58 const subproc = getSubprocesses(state.processPanel.containerRequestUuid)(state.resources);
59 for (const sb of subproc) {
60 if (sb.containerRequest.uuid === message.objectUuid || (sb.container && sb.container.uuid === message.objectUuid)) {
61 store.dispatch(subprocessPanelActions.REQUEST_ITEMS());
66 if (matchAllProcessesRoute(location)) {
67 store.dispatch(allProcessesPanelActions.REQUEST_ITEMS());
69 if (matchProjectRoute(location) && message.objectOwnerUuid === getProjectPanelCurrentUuid(state)) {
70 store.dispatch(projectPanelActions.REQUEST_ITEMS());
77 return store.dispatch(addProcessLogsPanelItem(message as ResourceEventMessage<{ text: string }>));