Skip to content

Commit 18e51db

Browse files
committed
fix: support "in progress" status for lib upload
When uploading a library archive file during the creation of a new library, the code prior to this commit did not properly handle the "In Progress" state, which is when the celery task doing the archive processing is actively running. Note that this is distinct from the "Pending" state, which is when the task is waiting in the queue to be run (which in practice should almost never happen unless there is an operational issue). Since celery tasks run in-process during local development, the task was always finished by the time that the browser made a call to check on the status. The problem only happened on slower sandboxes, where processing truly runs asynchronously and might take a few seconds. Because this case wasn't handled, the frontend would never poll for updates either, so the upload was basically lost as far as the user was concerned.
1 parent 4a1d0a2 commit 18e51db

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

src/library-authoring/create-library/CreateLibrary.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,8 @@ export const CreateLibrary = ({
253253

254254
{(restoreTaskId || isError || restoreMutation.isError) && (
255255
<div className="mb-4">
256-
{restoreStatus?.state === LibraryRestoreStatus.Pending && (
256+
{(restoreStatus?.state === LibraryRestoreStatus.Pending
257+
|| restoreStatus?.state === LibraryRestoreStatus.InProgress) && (
257258
<Alert variant="info">
258259
{intl.formatMessage(messages.restoreInProgress)}
259260
</Alert>

src/library-authoring/create-library/data/apiHooks.test.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,34 @@ describe('create library apiHooks', () => {
173173
expect(axiosMock.history.get[0].url).toEqual(`http://localhost:18010/api/libraries/v2/restore/?task_id=${taskId}`);
174174
});
175175

176+
it('should handle in-progress status with refetch interval', async () => {
177+
const taskId = 'in-progress-task-id';
178+
const inProgressResult = {
179+
state: LibraryRestoreStatus.InProgress,
180+
result: null,
181+
error: null,
182+
error_log: null,
183+
};
184+
185+
const expectedResult = {
186+
state: LibraryRestoreStatus.InProgress,
187+
result: null,
188+
error: null,
189+
errorLog: null,
190+
};
191+
192+
axiosMock.onGet(`http://localhost:18010/api/libraries/v2/restore/?task_id=${taskId}`).reply(200, inProgressResult);
193+
194+
const { result } = renderHook(() => useGetLibraryRestoreStatus(taskId), { wrapper });
195+
196+
await waitFor(() => {
197+
expect(result.current.isLoading).toBeFalsy();
198+
});
199+
200+
expect(result.current.data).toEqual(expectedResult);
201+
expect(axiosMock.history.get[0].url).toEqual(`http://localhost:18010/api/libraries/v2/restore/?task_id=${taskId}`);
202+
});
203+
176204
it('should handle failed status', async () => {
177205
const taskId = 'failed-task-id';
178206
const failedResult = {

src/library-authoring/create-library/data/apiHooks.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ export const useGetLibraryRestoreStatus = (taskId: string) => useQuery<GetLibrar
4141
queryKey: libraryRestoreQueryKeys.restoreStatus(taskId),
4242
queryFn: () => getLibraryRestoreStatus(taskId),
4343
enabled: !!taskId, // Only run the query if taskId is provided
44-
refetchInterval: (query) => (query.state.data?.state === LibraryRestoreStatus.Pending ? 2000 : false),
44+
refetchInterval: (query) => (
45+
(query.state.data?.state === LibraryRestoreStatus.Pending
46+
|| query.state.data?.state === LibraryRestoreStatus.InProgress
47+
) ? 2000 : false),
4548
});
4649

4750
export const useCreateLibraryRestore = () => useMutation<CreateLibraryRestoreResponse, Error, File>({

src/library-authoring/create-library/data/restoreConstants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export interface GetLibraryRestoreStatusResponse {
3232

3333
export enum LibraryRestoreStatus {
3434
Pending = 'Pending',
35+
InProgress = 'In Progress',
3536
Succeeded = 'Succeeded',
3637
Failed = 'Failed',
3738
}

0 commit comments

Comments
 (0)