Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Socket Initialization on user login #1109

Merged
merged 1 commit into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't the linter catch this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was a formatting issue, for some reason previous formatting tasks didn't catch 🤷‍♂️

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CDimonaco can we investigate? are we checking the formatting?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes in the CI we have the correct step, I will investigate further.

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),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if this breaks?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The application will crash, it's an unrecoverable error (no sockets no frontend basically), for the initial data fetching the error handling is the same as all the api requests

]);
}

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