Skip to content

Commit

Permalink
Add signal and abort controller to agent metadata and TakeAction butt…
Browse files Browse the repository at this point in the history
…on (#103217)
  • Loading branch information
academo committed Jun 29, 2021
1 parent 09bd630 commit ac17ab1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,14 @@ export const getCaseIdsFromAlertId = async ({
*
* @param host id
*/
export const getHostMetadata = async ({ agentId }: { agentId: string }): Promise<HostInfo> =>
export const getHostMetadata = async ({
agentId,
signal,
}: {
agentId: string;
signal?: AbortSignal;
}): Promise<HostInfo> =>
KibanaServices.get().http.fetch<HostInfo>(
resolvePathVariables(HOST_METADATA_GET_ROUTE, { id: agentId }),
{ method: 'get' }
{ method: 'GET', signal }
);
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,23 @@ export const useHostIsolationStatus = ({
const { addError } = useAppToasts();

useEffect(() => {
const abortCtrl = new AbortController();
// isMounted tracks if a component is mounted before changing state
let isMounted = true;
let fleetAgentId: string;
const fetchData = async () => {
try {
const metadataResponse = await getHostMetadata({ agentId });
const metadataResponse = await getHostMetadata({ agentId, signal: abortCtrl.signal });
if (isMounted) {
setIsIsolated(isEndpointHostIsolated(metadataResponse.metadata));
setAgentStatus(metadataResponse.host_status);
fleetAgentId = metadataResponse.metadata.elastic.agent.id;
}
} catch (error) {
// don't show self-aborted requests errors to the user
if (error.name === 'AbortError') {
return;
}
addError(error.message, { title: ISOLATION_STATUS_FAILURE });
}

Expand Down Expand Up @@ -80,6 +85,7 @@ export const useHostIsolationStatus = ({
return () => {
// updates to show component is unmounted
isMounted = false;
abortCtrl.abort();
};
}, [addError, agentId]);
return { loading, isIsolated, agentStatus, pendingIsolation, pendingUnisolation };
Expand Down

0 comments on commit ac17ab1

Please sign in to comment.