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

fix errors in log entries polling logic #113

Merged
merged 1 commit into from
Jun 29, 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
46 changes: 26 additions & 20 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,8 @@ const App = () => {

const darkMode = useSelector((state) => state.settings.darkMode);
const abilities = useSelector((state) => state.connection.abilities);
const queryError = useSelector((state) => state.querySettings.error);
const {
fetchingIncidents,
fetchingIncidentNotes,
fetchingIncidentAlerts,
refreshingIncidents,
lastFetchDate,
} = useSelector((state) => state.incidents);
const {
Expand Down Expand Up @@ -155,22 +151,35 @@ const App = () => {
moment.locale(currentUserLocale);
}, [currentUserLocale]);

// use these refs in the polling interval to avoid stale values
// without having to add them to the dependency array
const latestLogEntryDateRef = useRef(latestLogEntryDate);
useEffect(() => {
latestLogEntryDateRef.current = latestLogEntryDate;
}, [latestLogEntryDate]);
const fetchingIncidentsRef = useRef(fetchingIncidents);
useEffect(() => {
fetchingIncidentsRef.current = fetchingIncidents;
}, [fetchingIncidents]);
const fetchingLogEntriesRef = useRef(fetchingLogEntries);
useEffect(() => {
fetchingLogEntriesRef.current = fetchingLogEntries;
}, [fetchingLogEntries]);

// Set up log entry polling
useEffect(
() => {
const pollingInterval = setInterval(() => {
checkConnectionStatus();
if (userAuthorized && abilities.includes(PD_REQUIRED_ABILITY) && !queryError) {
if (fetchingLogEntries) {
if (userAuthorized && abilities.includes(PD_REQUIRED_ABILITY)) {
if (fetchingLogEntriesRef.current) {
// eslint-disable-next-line no-console
console.log('skipping log entries fetch because already fetching');
console.error('skipping log entries fetch because already fetching log entries');
return;
}

if (
!fetchingIncidents
&& !fetchingIncidentNotes
&& !fetchingIncidentAlerts
&& !refreshingIncidents
!fetchingIncidentsRef.current
&& !DEBUG_DISABLE_POLLING
) {
// Determine lookback based on last fetch/refresh of incidents
Expand All @@ -181,24 +190,21 @@ const App = () => {
since = new Date(lastFetchDate - 1000);
}
// If we have a latest log entry date and it's newer than last fetch date, use that
if (latestLogEntryDate && latestLogEntryDate > since) {
since = new Date(latestLogEntryDate - 1000);
if (latestLogEntryDateRef.current && latestLogEntryDateRef.current > since) {
since = new Date(latestLogEntryDateRef.current - 1000);
}
getLogEntriesAsync(since);
} else if (fetchingIncidentsRef.current) {
// eslint-disable-next-line no-console
console.error('skipping log entries fetch because already fetching incidents');
}
}
}, LOG_ENTRIES_POLLING_INTERVAL_SECONDS * 1000);
return () => clearInterval(pollingInterval);
},
// Changes to any of these in the store resets log entries timer
[
userAuthorized,
queryError,
fetchingIncidents,
fetchingIncidentNotes,
fetchingIncidentAlerts,
refreshingIncidents,
latestLogEntryDate,
lastFetchDate,
],
);

Expand Down
12 changes: 10 additions & 2 deletions src/redux/incidents/reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ import {
FILTER_INCIDENTS_LIST_BY_QUERY_ERROR,
} from './actions';

const uniqOnId = (arr) => {
const map = new Map();
arr.forEach((item) => {
map.set(item.id, item);
});
return [...map.values()];
};

const incidents = produce(
(draft, action) => {
switch (action.type) {
Expand Down Expand Up @@ -219,9 +227,9 @@ const incidents = produce(
if (!draft.incidentAlerts[incidentId]) {
draft.incidentAlerts[incidentId] = [];
}
draft.incidentAlerts[incidentId] = draft.incidentAlerts[incidentId].concat(
draft.incidentAlerts[incidentId] = uniqOnId(draft.incidentAlerts[incidentId].concat(
action.incidentAlertsMap[incidentId],
);
));
}
for (let i = 0; i < Object.keys(action.incidentAlertsUnlinkMap).length; i++) {
const incidentId = Object.keys(action.incidentAlertsUnlinkMap)[i];
Expand Down
4 changes: 2 additions & 2 deletions src/util/pd-api-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ export const pdAxiosRequest = async (method, endpoint, params = {}, data = {}) =
return `Token token=${tokenObj.token}`;
})(),
Accept: 'application/vnd.pagerduty+json;version=2',
'content-type': 'application/json; charset=utf-8',
'Content-Type': 'application/json; charset=utf-8',
},
params,
params: { ...params, rand: Math.random().toString(36).substring(2, 7) },
data,
});

Expand Down
Loading