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(orchestrator): resolve bug in workflow instance page assessed by link #1142

Merged
merged 1 commit into from
Jan 30, 2024
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
14 changes: 8 additions & 6 deletions plugins/orchestrator/src/components/WorkflowInstancePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,17 @@ export const WorkflowInstancePage = ({
workflowInstanceRouteRef,
);

const fetchInstance = React.useCallback(async () => {
if (!instanceId && !queryInstanceId) {
return undefined;
}
return await orchestratorApi.getInstance(instanceId || queryInstanceId);
}, [instanceId, orchestratorApi, queryInstanceId]);

const { loading, error, value, restart } = usePolling<
ProcessInstance | undefined
>(
async () => {
if (!instanceId && !queryInstanceId) {
return undefined;
}
return await orchestratorApi.getInstance(instanceId || queryInstanceId);
},
fetchInstance,
SHORT_REFRESH_INTERVAL,
(curValue: ProcessInstance | undefined) =>
!!curValue && curValue.state === 'ACTIVE',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@ export const WorkflowRunsTabContent = () => {
Selector.AllItems,
);

const { loading, error, value } = usePolling(async () => {
const fetchInstances = React.useCallback(async () => {
const instances = await orchestratorApi.getInstances();
const clonedData: WorkflowRunDetail[] = instances.map(
mapProcessInstanceToDetails,
);

return clonedData;
});
}, [orchestratorApi]);

const { loading, error, value } = usePolling(fetchInstances);

const columns = React.useMemo(
(): TableColumn<WorkflowRunDetail>[] => [
Expand Down
15 changes: 15 additions & 0 deletions plugins/orchestrator/src/hooks/usePolling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,19 @@ describe('usePolling', () => {
await act(async () => result.current.restart());
expect(result.current.value).toEqual(ABORTED);
});

test('should refetch after fn property changed', async () => {
const mockAsyncFn = jest.fn().mockResolvedValue(ACTIVE);
const { result, waitForNextUpdate, rerender } = renderHook(
({ fn }: { fn: () => Promise<void> }) =>
usePolling(fn, SHORT_REFRESH_INTERVAL),
{ initialProps: { fn: mockAsyncFn } },
);
await waitForNextUpdate();
const mockAsyncFn2 = jest.fn().mockResolvedValue(COMPLETED);
expect(result.current.value).toEqual(ACTIVE);
rerender({ fn: mockAsyncFn2 });
await waitForNextUpdate();
expect(result.current.value).toEqual(COMPLETED);
});
});
15 changes: 14 additions & 1 deletion plugins/orchestrator/src/hooks/usePolling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const usePolling = <T>(
) => {
const config = useSWRConfig();

const prevFn = React.useRef(fn);
const uniqueKey = React.useMemo<string>(() => {
return uuid.v4();
}, []);
Expand All @@ -38,6 +39,18 @@ const usePolling = <T>(
},
});

const restart = React.useCallback(
() => config.mutate(uniqueKey),
[config, uniqueKey],
);

React.useEffect(() => {
if (prevFn.current !== fn) {
restart();
prevFn.current = fn;
}
}, [fn, restart]);

React.useEffect(() => {
// clean cache after unmount, no need to store the data globally
return () => config.cache.delete(uniqueKey);
Expand All @@ -48,7 +61,7 @@ const usePolling = <T>(
value: data,
error,
loading: isLoading,
restart: () => config.mutate(uniqueKey),
restart,
};
};

Expand Down
Loading