Skip to content

Commit

Permalink
add cancel query fix
Browse files Browse the repository at this point in the history
Signed-off-by: Shenoy Pratik <[email protected]>
  • Loading branch information
ps48 committed Mar 21, 2024
1 parent bf51a31 commit e941606
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 53 deletions.
107 changes: 55 additions & 52 deletions common/utils/async_query_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const executeAsyncQuery = (
) => {
let jobId: string | undefined;
let isQueryFulfilled = false;
let isQueryCancelled = false;
const http = coreRefs.http!;

const getJobId = () => {
Expand Down Expand Up @@ -73,67 +74,69 @@ export const executeAsyncQuery = (
};

const pollQueryStatus = (id: string, callback: PollingCallback) => {
http
.get(ASYNC_QUERY_JOB_ENDPOINT + id)
.then((res: AsyncApiResponse) => {
const status = res.data.resp.status.toLowerCase();
const errorDetailsMessage = res.data.resp.error ?? '';
switch (status) {
case AsyncQueryStatus.Pending:
case AsyncQueryStatus.Running:
case AsyncQueryStatus.Scheduled:
case AsyncQueryStatus.Waiting:
callback({ ...res });
setTimeout(() => pollQueryStatus(id, callback), POLL_INTERVAL_MS);
break;
!isQueryCancelled &&
http
.get(ASYNC_QUERY_JOB_ENDPOINT + id)
.then((res: AsyncApiResponse) => {
const status = res.data.resp.status.toLowerCase();
const errorDetailsMessage = res.data.resp.error ?? '';
switch (status) {
case AsyncQueryStatus.Pending:
case AsyncQueryStatus.Running:
case AsyncQueryStatus.Scheduled:
case AsyncQueryStatus.Waiting:
callback({ ...res });
setTimeout(() => pollQueryStatus(id, callback), POLL_INTERVAL_MS);
break;

case AsyncQueryStatus.Failed:
case AsyncQueryStatus.Cancelled:
isQueryFulfilled = true;
case AsyncQueryStatus.Failed:
case AsyncQueryStatus.Cancelled:
isQueryFulfilled = true;

if (status === AsyncQueryStatus.Failed) {
RaiseErrorToast({
errorToastMessage: 'Query failed',
errorDetailsMessage,
});
}
if (onErrorCallback) {
onErrorCallback(errorDetailsMessage);
}
callback({ ...res });
break;
if (status === AsyncQueryStatus.Failed) {
RaiseErrorToast({
errorToastMessage: 'Query failed',
errorDetailsMessage,
});
}
if (onErrorCallback) {
onErrorCallback(errorDetailsMessage);
}
callback({ ...res });
break;

case AsyncQueryStatus.Success:
isQueryFulfilled = true;
callback({ ...res });
break;
case AsyncQueryStatus.Success:
isQueryFulfilled = true;
callback({ ...res });
break;

default:
console.error('Unrecognized status:', status);
RaiseErrorToast({
errorToastMessage: 'Unrecognized status recieved',
errorDetailsMessage: 'Unrecognized status recieved - ' + errorDetailsMessage,
});
if (onErrorCallback) {
onErrorCallback(errorDetailsMessage);
}
callback({ ...res });
}
})
.catch((err) => {
console.error('Error occurred while polling query status:', err);
isQueryFulfilled = true;
callback({
data: {
ok: true,
resp: { status: AsyncQueryStatus.Failed, error: 'Failed to query status' },
},
default:
console.error('Unrecognized status:', status);
RaiseErrorToast({
errorToastMessage: 'Unrecognized status recieved',
errorDetailsMessage: 'Unrecognized status recieved - ' + errorDetailsMessage,
});
if (onErrorCallback) {
onErrorCallback(errorDetailsMessage);
}
callback({ ...res });
}
})
.catch((err) => {
console.error('Error occurred while polling query status:', err);
isQueryFulfilled = true;
callback({
data: {
ok: true,
resp: { status: AsyncQueryStatus.Failed, error: 'Failed to query status' },
},
});
});
});
};

const cancelQuery = () => {
if (jobId && !isQueryFulfilled) {
isQueryCancelled = true;
http.delete(ASYNC_QUERY_JOB_ENDPOINT + jobId).catch((err) => {
console.error('Error occurred while cancelling query:', err);
RaiseErrorToast({
Expand Down
10 changes: 9 additions & 1 deletion public/components/Main/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,15 @@ export class Main extends React.Component<MainProps, MainState> {
});
}
);
this.setState({ cancelQueryHandler: cancelQuery });

const cancelQueryRequester = () => {
this.setState({
asyncLoading: false,
asyncLoadingStatus: AsyncQueryStatus.Cancelled,
});
cancelQuery();
};
this.setState({ cancelQueryHandler: cancelQueryRequester });
});
}
};
Expand Down

0 comments on commit e941606

Please sign in to comment.