Skip to content

Commit

Permalink
Socket Initialization on user login (#1109)
Browse files Browse the repository at this point in the history
  • Loading branch information
CDimonaco authored Jan 16, 2023
1 parent d821e98 commit 79e34a2
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 69 deletions.
2 changes: 1 addition & 1 deletion assets/js/lib/api/wanda.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ export const getLastExecutionByGroupID = (groupID) =>
wandaClient.get(`/api/checks/groups/${groupID}/executions/last`);

export const getCatalog = (env) =>
wandaClient.get(`/api/checks/catalog`, { params: env })
wandaClient.get(`/api/checks/catalog`, { params: env });
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// safe to disable phoenix stuff
// eslint-disable-next-line
import { Socket } from 'phoenix';
import { logMessage, logError } from '@lib/log';

export const joinChannel = (channel) => {
Expand All @@ -8,16 +11,9 @@ export const joinChannel = (channel) => {
.receive('timeout', () => logMessage('Networking issue. Still waiting...'));
};

const registerEvents = (store, socket, channelName, events) => {
const channel = socket.channel(channelName, {});
export const initSocketConnection = () => {
const socket = new Socket('/socket', {});
socket.connect();

events.forEach((event) => {
channel.on(event, (payload) =>
store.dispatch({ type: event.toUpperCase(), payload })
);
});

joinChannel(channel);
return socket;
};

export default registerEvents;
2 changes: 1 addition & 1 deletion assets/js/state/actions/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ export const UPDATE_CATALOG = 'UPDATE_CATALOG_NEW';
export const updateCatalog = (env = {}) => ({
type: UPDATE_CATALOG,
payload: env,
});
});
58 changes: 58 additions & 0 deletions assets/js/state/channels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// TODO remove dependency from the store when the fixme is fixed
import { updateLastExecution } from '@state/actions/lastExecutions';
import { joinChannel } from '@lib/network/socket';

const registerEvents = (store, socket, channelName, events) => {
const channel = socket.channel(channelName, {});

events.forEach((event) => {
channel.on(event, (payload) =>
store.dispatch({ type: event.toUpperCase(), payload })
);
});

joinChannel(channel);
};

const processChannelEvents = (reduxStore, socket) => {
registerEvents(reduxStore, socket, 'monitoring:hosts', [
'host_registered',
'host_details_updated',
'heartbeat_succeded',
'heartbeat_failed',
]);
registerEvents(reduxStore, socket, 'monitoring:clusters', [
'cluster_registered',
'cluster_details_updated',
'checks_execution_started',
'checks_execution_completed',
'checks_results_updated',
'cluster_health_changed',
'cluster_cib_last_written_updated',
]);
registerEvents(reduxStore, socket, 'monitoring:sap_systems', [
'sap_system_registered',
'sap_system_health_changed',
'application_instance_registered',
'application_instance_health_changed',
]);
registerEvents(reduxStore, socket, 'monitoring:databases', [
'database_registered',
'database_health_changed',
'database_instance_registered',
'database_instance_health_changed',
'database_instance_system_replication_changed',
]);

// FIXME: This is to overcome the fact that we are generating names with registerEvents
// in the future we want to remove this and use the constants directly,
// since events and actions may have different names and parameters.
const channel = socket.channel('monitoring:executions', {});
channel.on('execution_completed', ({ group_id: groupID }) => {
reduxStore.dispatch(updateLastExecution(groupID));
});

joinChannel(channel);
};

export default processChannelEvents;
53 changes: 0 additions & 53 deletions assets/js/state/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { configureStore } from '@reduxjs/toolkit';
import createSagaMiddleware from 'redux-saga';
// safe to disable phoenix stuff
// eslint-disable-next-line
import { Socket } from 'phoenix';

import { updateLastExecution } from '@state/actions/lastExecutions';
import sapSystemsHealthSummaryReducer from './healthSummary';
import hostsListReducer from './hosts';
import clustersListReducer from './clusters';
Expand All @@ -18,8 +13,6 @@ import lastExecutionsReducer from './lastExecutions';
import liveFeedReducer from './liveFeed';
import settingsReducer from './settings';
import userReducer from './user';
import registerEvents, { joinChannel } from './registerSocketEvents';

import rootSaga from './sagas';

const sagaMiddleware = createSagaMiddleware();
Expand All @@ -44,49 +37,3 @@ export const store = configureStore({
});

sagaMiddleware.run(rootSaga);

const processChannelEvents = (reduxStore) => {
const socket = new Socket('/socket', {});
socket.connect();

registerEvents(reduxStore, socket, 'monitoring:hosts', [
'host_registered',
'host_details_updated',
'heartbeat_succeded',
'heartbeat_failed',
]);
registerEvents(reduxStore, socket, 'monitoring:clusters', [
'cluster_registered',
'cluster_details_updated',
'checks_execution_started',
'checks_execution_completed',
'checks_results_updated',
'cluster_health_changed',
'cluster_cib_last_written_updated',
]);
registerEvents(reduxStore, socket, 'monitoring:sap_systems', [
'sap_system_registered',
'sap_system_health_changed',
'application_instance_registered',
'application_instance_health_changed',
]);
registerEvents(reduxStore, socket, 'monitoring:databases', [
'database_registered',
'database_health_changed',
'database_instance_registered',
'database_instance_health_changed',
'database_instance_system_replication_changed',
]);

// FIXME: This is to overcome the fact that we are generating names with registerEvents
// in the future we want to remove this and use the constants directly,
// since events and actions may have different names and parameters.
const channel = socket.channel('monitoring:executions', {});
channel.on('execution_completed', ({ group_id: groupID }) => {
store.dispatch(updateLastExecution(groupID));
});

joinChannel(channel);
};

processChannelEvents(store);
17 changes: 14 additions & 3 deletions assets/js/state/sagas/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ import {

import { CHECKS_SELECTED } from '@state/actions/cluster';
import { EXECUTION_REQUESTED } from '@state/actions/lastExecutions';
import { initSocketConnection } from '@lib/network/socket';
import processChannelEvents from '@state/channels';
import { store } from '@state';

const notify = ({ text, icon }) => ({
type: 'NOTIFICATION',
Expand Down Expand Up @@ -154,8 +157,16 @@ function* initialDataFetch() {
yield put(stopDatabasesLoading());
}

function* watchInitialDataFetching() {
yield takeLatest('user/setUserAsLogged', initialDataFetch);
function* setupSocketEvents() {
const socket = initSocketConnection();
yield call(processChannelEvents, store, socket);
}

function* watchUserLoggedIn() {
yield all([
takeLatest('user/setUserAsLogged', initialDataFetch),
takeLatest('user/setUserAsLogged', setupSocketEvents),
]);
}

function* watchResetState() {
Expand Down Expand Up @@ -637,7 +648,7 @@ function* watchClustrConnectionSettings() {

export default function* rootSaga() {
yield all([
watchInitialDataFetching(),
watchUserLoggedIn(),
watchResetState(),
watchHostRegistered(),
watchHostDetailsUpdated(),
Expand Down

0 comments on commit 79e34a2

Please sign in to comment.