mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-22 07:56:18 -05:00
A directory rename is finished by the file worker, not the request (#950)
* A directory rename is finished by the file worker, not the request
A directory rename queued a move, waited for it, then removed the old
directory and updated nested Collections itself. A rename that outlasted
Sanic's RESPONSE_TIMEOUT (60 s) was cancelled while waiting: the move still
finished, but the old directory and its emptied subdirectories were left
behind, nested Collections kept their old directories, and the browser was
told the rename failed.
The move job now finishes the rename (nested Collections, then removing the
old directory, empty directories only), and the request returns once the
move is queued. A failure is reported by the job's event. An empty
directory can be renamed (it raised before). The worker status carries
`rename_from`, so the Files page progress bar labels the rename, and the
file browser fetches again when the file worker is done.
A directory rename no longer queues a refresh of its parent: the file
worker runs the newest task first, so it scanned the directory before the
rename.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* A queued rename returns its job, and the file browser waits for it
Review fixes:
- The cancellation test patched `wait_for_job`, which this path no longer
calls, so it cancelled nothing. It now asserts the invariant: the
request only queues the rename (it never waits, and nothing has moved
when it returns), and the file worker alone finishes it.
- The file browser inferred the end of a rename from the file worker's
polled status. A short job can start and finish between two polls, and
the first idle poll (before the job started) spent the "fetch once"
token, so the table could keep the old name. POST /api/files/rename now
returns `{"job_id": ...}` (null when the rename is already done), GET
/api/files/jobs/<job_id> reports pending/complete/failed, and the file
browser polls that job before fetching.
- useBrowseFiles ignores a fetch that settles after a newer one, so a
listing requested before a rename cannot replace the one after it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
c7bafaa7fb
commit
aff5f1eb1f
16 changed files with 532 additions and 96 deletions
|
|
@ -1927,13 +1927,31 @@ export async function searchDirectories(path) {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the file worker job id of a queued rename (a directory), or null when the rename is already done.
|
||||
*/
|
||||
export async function renamePath(path, newName) {
|
||||
const body = {path, new_name: newName};
|
||||
const response = await apiPost(`${API_URI}/files/rename`, body);
|
||||
if (!response.ok) {
|
||||
const message = await getErrorMessage(response, 'Failed to rename!');
|
||||
toast({type: 'error', title: 'Error', description: message, time: 5000});
|
||||
return null;
|
||||
}
|
||||
const json = await response.json();
|
||||
return json['job_id'] || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a file worker job's status ('pending', 'complete', or 'failed'), or null if it is unknown.
|
||||
*/
|
||||
export async function fetchFileWorkerJobStatus(jobId) {
|
||||
const response = await apiGet(`${API_URI}/files/jobs/${encodeURIComponent(jobId)}`);
|
||||
if (!response.ok) {
|
||||
return null;
|
||||
}
|
||||
const json = await response.json();
|
||||
return json['status'];
|
||||
}
|
||||
|
||||
export async function movePaths(destination, paths) {
|
||||
|
|
|
|||
|
|
@ -23,7 +23,15 @@ import {
|
|||
import React, {useContext, useEffect, useState} from "react";
|
||||
import {IconArrowsMove, IconChevronsUp, IconCursorText, IconTags} from "@tabler/icons-react";
|
||||
import {useHotkeys} from "react-hotkeys-hook";
|
||||
import {deleteFile, ignoreDirectory, makeDirectory, movePaths, renamePath, unignoreDirectory} from "../api";
|
||||
import {
|
||||
deleteFile,
|
||||
fetchFileWorkerJobStatus,
|
||||
ignoreDirectory,
|
||||
makeDirectory,
|
||||
movePaths,
|
||||
renamePath,
|
||||
unignoreDirectory
|
||||
} from "../api";
|
||||
import _ from 'lodash';
|
||||
import {SortableTable} from "./SortableTable";
|
||||
import {useBrowseFiles, useElementWidth, useMediaDirectory, useWROLMode} from "../hooks/customHooks";
|
||||
|
|
@ -34,6 +42,7 @@ import {BulkTagModal} from "./BulkTagModal";
|
|||
import {InlineErrorBoundary} from "./ErrorBoundary";
|
||||
import {TaggedDeleteConfirmModal} from "./TaggedDeleteConfirmModal";
|
||||
import {UploadModal} from "./Upload";
|
||||
import {useFileWorkerStatus} from "../contexts/FileWorkerStatusContext";
|
||||
|
||||
function depthIndentation(path) {
|
||||
// Repeated spaces for every folder a path is in.
|
||||
|
|
@ -228,6 +237,8 @@ function SkeletonFileRow({depth}) {
|
|||
// Below this footer width (px) the action buttons are icon-only; at or above it they expand to show labels.
|
||||
// The eight labeled buttons need ~1020px to fit on a single row; this leaves a small buffer.
|
||||
const FOOTER_LABELS_MIN_WIDTH = 1050;
|
||||
// How often a queued rename's job is checked.
|
||||
const RENAME_JOB_POLL_MS = 500;
|
||||
|
||||
export function FileBrowser() {
|
||||
const [selectedPaths, setSelectedPaths] = React.useState([]);
|
||||
|
|
@ -328,6 +339,46 @@ export function FileBrowser() {
|
|||
}
|
||||
}, [browseFiles]);
|
||||
|
||||
// A move finishes in the file worker after its request returns (the Files page shows its progress), so fetch
|
||||
// the files again when the file worker is done.
|
||||
const {status: fileWorkerStatus, setFastPolling} = useFileWorkerStatus();
|
||||
const fileWorkerBusy = !!fileWorkerStatus && fileWorkerStatus.status !== 'idle';
|
||||
const fileWorkerWasBusy = React.useRef(false);
|
||||
useEffect(() => {
|
||||
if (fileWorkerWasBusy.current && !fileWorkerBusy) {
|
||||
fetchFiles();
|
||||
}
|
||||
fileWorkerWasBusy.current = fileWorkerBusy;
|
||||
}, [fileWorkerBusy]);
|
||||
|
||||
// A queued rename (a directory) returns its job; wait for that job, rather than inferring it from the file
|
||||
// worker's status (a short job can start and finish between two polls).
|
||||
const mountedRef = React.useRef(true);
|
||||
useEffect(() => () => {
|
||||
mountedRef.current = false;
|
||||
}, []);
|
||||
const afterRename = async (jobId) => {
|
||||
if (!jobId) {
|
||||
await reset();
|
||||
return;
|
||||
}
|
||||
setSelectedPaths([]);
|
||||
setFastPolling(true); // Update the progress bar while the rename runs.
|
||||
try {
|
||||
while (mountedRef.current) {
|
||||
await new Promise(resolve => setTimeout(resolve, RENAME_JOB_POLL_MS));
|
||||
if (await fetchFileWorkerJobStatus(jobId) !== 'pending') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
setFastPolling(false);
|
||||
}
|
||||
if (mountedRef.current) {
|
||||
await reset();
|
||||
}
|
||||
}
|
||||
|
||||
// Clear loading states when file data updates
|
||||
useEffect(() => {
|
||||
if (loadingFolders.size > 0) {
|
||||
|
|
@ -413,7 +464,7 @@ export function FileBrowser() {
|
|||
onClose={() => setRenameOpen(false)}
|
||||
onPending={setPending}
|
||||
path={selectedPaths[0]}
|
||||
onSubmit={reset}
|
||||
onSubmit={afterRename}
|
||||
/> : null}
|
||||
<Button icon={IconArrowsMove}
|
||||
color='teal'
|
||||
|
|
@ -619,15 +670,16 @@ export function RenameModal({open, onClose, path, onSubmit, onPending}) {
|
|||
if (e) {
|
||||
e.preventDefault();
|
||||
}
|
||||
let jobId = null;
|
||||
try {
|
||||
console.log(`Renaming: ${path} to ${value}`);
|
||||
onPending(true);
|
||||
await renamePath(path, value);
|
||||
jobId = await renamePath(path, value);
|
||||
} finally {
|
||||
onClose();
|
||||
onPending(false);
|
||||
if (onSubmit) {
|
||||
await onSubmit();
|
||||
await onSubmit(jobId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,10 +6,13 @@ import {FileBrowser, filterBrowseFiles} from './FileBrowser';
|
|||
import {renderWithProviders} from '../test-utils';
|
||||
import {SettingsContext} from '../contexts/contexts';
|
||||
import {FilePreviewContext} from './FilePreview';
|
||||
import {FileWorkerStatusContext} from '../contexts/FileWorkerStatusContext';
|
||||
import {fileWorkerStatusFixture} from '../test-fixtures';
|
||||
|
||||
// Mock the API functions
|
||||
jest.mock('../api', () => ({
|
||||
deleteFile: jest.fn(),
|
||||
fetchFileWorkerJobStatus: jest.fn(),
|
||||
ignoreDirectory: jest.fn(),
|
||||
makeDirectory: jest.fn(),
|
||||
movePaths: jest.fn(),
|
||||
|
|
@ -59,8 +62,13 @@ jest.mock('react-dropzone', () => ({
|
|||
// Import mocked hooks for manipulation
|
||||
import {useBrowseFiles, useElementWidth, useWROLMode} from '../hooks/customHooks';
|
||||
|
||||
// Set by the wrapper so a test can change the file worker's status, as a poll would.
|
||||
let setFileWorkerStatus;
|
||||
|
||||
// Create wrapper with all required contexts
|
||||
const AllContextsWrapper = ({children}) => {
|
||||
const [fileWorkerStatus, setStatus] = React.useState({status: 'idle'});
|
||||
setFileWorkerStatus = setStatus;
|
||||
const settingsValue = {
|
||||
settings: {ignored_directories: []},
|
||||
fetchSettings: jest.fn(),
|
||||
|
|
@ -71,11 +79,13 @@ const AllContextsWrapper = ({children}) => {
|
|||
};
|
||||
|
||||
return (
|
||||
<SettingsContext.Provider value={settingsValue}>
|
||||
<FilePreviewContext.Provider value={filePreviewValue}>
|
||||
{children}
|
||||
</FilePreviewContext.Provider>
|
||||
</SettingsContext.Provider>
|
||||
<FileWorkerStatusContext.Provider value={fileWorkerStatusFixture({status: fileWorkerStatus})}>
|
||||
<SettingsContext.Provider value={settingsValue}>
|
||||
<FilePreviewContext.Provider value={filePreviewValue}>
|
||||
{children}
|
||||
</FilePreviewContext.Provider>
|
||||
</SettingsContext.Provider>
|
||||
</FileWorkerStatusContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
|
|
@ -399,6 +409,77 @@ describe('FileBrowser', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('File worker changes', () => {
|
||||
it('fetches the files again when the file worker finishes', async () => {
|
||||
const mockFetchFiles = jest.fn();
|
||||
useBrowseFiles.mockReturnValue({
|
||||
browseFiles: mockBrowseFiles,
|
||||
openFolders: [],
|
||||
setOpenFolders: jest.fn(),
|
||||
fetchFiles: mockFetchFiles,
|
||||
});
|
||||
renderFileBrowser(<FileBrowser/>);
|
||||
|
||||
await act(async () => setFileWorkerStatus({status: 'moving', rename_from: '/media/wrolpi/testdir'}));
|
||||
expect(mockFetchFiles).not.toHaveBeenCalled();
|
||||
|
||||
await act(async () => setFileWorkerStatus({status: 'idle'}));
|
||||
expect(mockFetchFiles).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
const renameTestdir = async () => {
|
||||
// Select testdir/ (the second checkbox) and rename it.
|
||||
await act(async () => fireEvent.click(screen.getAllByRole('checkbox')[1]));
|
||||
const renameButton = document.querySelector('.tabler-icon-cursor-text').closest('button');
|
||||
await act(async () => fireEvent.click(renameButton));
|
||||
const nameInput = await screen.findByDisplayValue(/^testdir/);
|
||||
await act(async () => fireEvent.change(nameInput, {target: {value: 'newdir'}}));
|
||||
await act(async () => fireEvent.click(screen.getByRole('button', {name: 'Rename'})));
|
||||
};
|
||||
|
||||
it('fetches the files once a queued rename job is done, even if no poll saw it running', async () => {
|
||||
const mockFetchFiles = jest.fn();
|
||||
const api = require('../api');
|
||||
api.renamePath.mockResolvedValue('move-1234');
|
||||
// The job is still queued at the first check, and done at the second.
|
||||
api.fetchFileWorkerJobStatus.mockResolvedValueOnce('pending').mockResolvedValue('complete');
|
||||
useBrowseFiles.mockReturnValue({
|
||||
browseFiles: mockBrowseFiles,
|
||||
openFolders: [],
|
||||
setOpenFolders: jest.fn(),
|
||||
fetchFiles: mockFetchFiles,
|
||||
});
|
||||
renderFileBrowser(<FileBrowser/>);
|
||||
|
||||
await renameTestdir();
|
||||
expect(api.renamePath).toHaveBeenCalledWith('testdir/', 'newdir');
|
||||
// Nothing is fetched while the rename is only queued.
|
||||
expect(mockFetchFiles).not.toHaveBeenCalled();
|
||||
|
||||
// The file worker's status never left idle; the job is what says the rename is done.
|
||||
await waitFor(() => expect(mockFetchFiles).toHaveBeenCalledTimes(1), {timeout: 3000});
|
||||
expect(api.fetchFileWorkerJobStatus).toHaveBeenCalledWith('move-1234');
|
||||
expect(api.fetchFileWorkerJobStatus).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('fetches the files right away after a rename that was not queued', async () => {
|
||||
const mockFetchFiles = jest.fn();
|
||||
const api = require('../api');
|
||||
api.renamePath.mockResolvedValue(null);
|
||||
useBrowseFiles.mockReturnValue({
|
||||
browseFiles: mockBrowseFiles,
|
||||
openFolders: [],
|
||||
setOpenFolders: jest.fn(),
|
||||
fetchFiles: mockFetchFiles,
|
||||
});
|
||||
renderFileBrowser(<FileBrowser/>);
|
||||
|
||||
await renameTestdir();
|
||||
expect(mockFetchFiles).toHaveBeenCalledTimes(1);
|
||||
expect(api.fetchFileWorkerJobStatus).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('Delete functionality', () => {
|
||||
it('removes deleted directory from openFolders', async () => {
|
||||
const mockSetOpenFolders = jest.fn();
|
||||
|
|
|
|||
|
|
@ -915,7 +915,12 @@ export function FilesSearchView({
|
|||
</>
|
||||
}
|
||||
|
||||
function getPhaseLabel(status) {
|
||||
function getPhaseLabel(status, renameFrom) {
|
||||
if (renameFrom && (status === 'planning' || status === 'moving')) {
|
||||
// A directory rename is a move of the directory's contents.
|
||||
const name = renameFrom.split('/').filter(Boolean).pop();
|
||||
return `Rename ${name}: ${status === 'planning' ? 'Planning' : 'Moving'}`;
|
||||
}
|
||||
switch (status) {
|
||||
case 'counting':
|
||||
return 'Counting';
|
||||
|
|
@ -1037,8 +1042,8 @@ export function RefreshSteps({progress}) {
|
|||
</div>
|
||||
}
|
||||
|
||||
function RefreshProgressBar({status, operation_total, operation_processed, operation_percent, error}) {
|
||||
const phaseLabel = getPhaseLabel(status);
|
||||
function RefreshProgressBar({status, operation_total, operation_processed, operation_percent, error, rename_from}) {
|
||||
const phaseLabel = getPhaseLabel(status, rename_from);
|
||||
|
||||
if (!phaseLabel) {
|
||||
return null;
|
||||
|
|
@ -1064,7 +1069,7 @@ export function FilesRefreshProgress() {
|
|||
return null;
|
||||
}
|
||||
|
||||
const {status, operation_total, operation_processed, operation_percent, error} = progress;
|
||||
const {status, operation_total, operation_processed, operation_percent, error, rename_from} = progress;
|
||||
|
||||
// Show Steps component AND progress bar for global refresh operations
|
||||
if (isGlobalRefresh(progress)) {
|
||||
|
|
@ -1080,13 +1085,14 @@ export function FilesRefreshProgress() {
|
|||
</>;
|
||||
}
|
||||
|
||||
// Show just progress bar for other operations (moves, reorganize, targeted refresh)
|
||||
// Show just progress bar for other operations (moves, renames, reorganize, targeted refresh)
|
||||
return <RefreshProgressBar
|
||||
status={status}
|
||||
operation_total={operation_total}
|
||||
operation_processed={operation_processed}
|
||||
operation_percent={operation_percent}
|
||||
error={error}
|
||||
rename_from={rename_from}
|
||||
/>;
|
||||
}
|
||||
|
||||
|
|
|
|||
44
app/src/components/FilesRefreshProgress.test.js
Normal file
44
app/src/components/FilesRefreshProgress.test.js
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import React from 'react';
|
||||
import {screen} from '@testing-library/react';
|
||||
import {FilesRefreshProgress} from './Files';
|
||||
import {renderWithProviders} from '../test-utils';
|
||||
import {fileWorkerStatusFixture} from '../test-fixtures';
|
||||
import {FileWorkerStatusContext} from '../contexts/FileWorkerStatusContext';
|
||||
|
||||
const renderProgress = (status) => renderWithProviders(
|
||||
<FileWorkerStatusContext.Provider value={fileWorkerStatusFixture({status})}>
|
||||
<FilesRefreshProgress/>
|
||||
</FileWorkerStatusContext.Provider>
|
||||
);
|
||||
|
||||
const moving = {
|
||||
status: 'moving',
|
||||
task_type: 'move',
|
||||
destination: '/media/wrolpi/3d models/Tools',
|
||||
operation_total: 109,
|
||||
operation_processed: 40,
|
||||
operation_percent: 36,
|
||||
error: null,
|
||||
};
|
||||
|
||||
describe('FilesRefreshProgress', () => {
|
||||
it('labels a move', () => {
|
||||
renderProgress({...moving, rename_from: null});
|
||||
expect(screen.getByText('Move: Moving (40 / 109)')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('labels a directory rename with the renamed directory', () => {
|
||||
renderProgress({...moving, rename_from: '/media/wrolpi/3d models/tools'});
|
||||
expect(screen.getByText('Rename tools: Moving (40 / 109)')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('labels a directory rename that is planning', () => {
|
||||
renderProgress({...moving, status: 'planning', operation_total: 0, rename_from: '/media/wrolpi/3d models/tools'});
|
||||
expect(screen.getByText('Rename tools: Planning')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows nothing while idle', () => {
|
||||
renderProgress({...moving, status: 'idle'});
|
||||
expect(screen.queryByText(/Move|Rename/)).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
|
@ -1083,14 +1083,22 @@ export const useSearchFiles = (defaultLimit = 48, emptySearch = false, model) =>
|
|||
export const useBrowseFiles = () => {
|
||||
const [browseFiles, setBrowseFiles] = useState(null);
|
||||
const [openFolders, setOpenFolders] = useAllQuery('folders');
|
||||
// Only the latest fetch may set the files; an older one that settles late would show files that have since
|
||||
// been moved or renamed.
|
||||
const latestFetchRef = React.useRef(0);
|
||||
|
||||
const fetchFiles = async () => {
|
||||
const fetchId = ++latestFetchRef.current;
|
||||
try {
|
||||
const files = await getFiles(openFolders);
|
||||
setBrowseFiles(files);
|
||||
if (fetchId === latestFetchRef.current) {
|
||||
setBrowseFiles(files);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
setBrowseFiles(undefined); // Display error.
|
||||
if (fetchId === latestFetchRef.current) {
|
||||
setBrowseFiles(undefined); // Display error.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,11 +2,11 @@ import React from 'react';
|
|||
import {act, renderHook} from '@testing-library/react';
|
||||
import {render, screen} from '../test-utils';
|
||||
import {
|
||||
usePages, useDriveTemperature, useDriveHealth, useSearchChannels, useSearchRecentFiles, useSettings,
|
||||
useStatistics, useStatus, useVideoExtras,
|
||||
useBrowseFiles, usePages, useDriveTemperature, useDriveHealth, useSearchChannels, useSearchRecentFiles,
|
||||
useSettings, useStatistics, useStatus, useVideoExtras,
|
||||
} from './customHooks';
|
||||
import {
|
||||
ApiDownError, filesSearch, getSettings, getStatistics, getStatus, getVideoComments, getVideoDescription,
|
||||
ApiDownError, filesSearch, getFiles, getSettings, getStatistics, getStatus, getVideoComments, getVideoDescription,
|
||||
searchChannels,
|
||||
} from '../api';
|
||||
import {getControllerStats} from '../api/controller';
|
||||
|
|
@ -50,6 +50,7 @@ jest.mock('../api', () => ({
|
|||
getStatus: jest.fn(),
|
||||
getSettings: jest.fn(),
|
||||
filesSearch: jest.fn(),
|
||||
getFiles: jest.fn(),
|
||||
}));
|
||||
|
||||
jest.mock('../api/controller', () => ({
|
||||
|
|
@ -736,3 +737,35 @@ describe('useSearchRecentFiles', () => {
|
|||
expect(result.current.searchFiles).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('useBrowseFiles', () => {
|
||||
it('ignores a fetch that settles after a newer one', async () => {
|
||||
// The mount's fetch, then a fetch from before a rename, then one from after it.
|
||||
let resolveBeforeRename;
|
||||
getFiles
|
||||
.mockResolvedValueOnce([{path: 'mounted/'}])
|
||||
.mockReturnValueOnce(new Promise(resolve => {
|
||||
resolveBeforeRename = resolve;
|
||||
}))
|
||||
.mockResolvedValueOnce([{path: 'renamed/'}]);
|
||||
|
||||
const {result} = renderHook(() => useBrowseFiles(), {wrapper: makeWrapper()});
|
||||
await act(async () => {});
|
||||
expect(result.current.browseFiles).toEqual([{path: 'mounted/'}]);
|
||||
|
||||
let beforeRename;
|
||||
await act(async () => {
|
||||
beforeRename = result.current.fetchFiles();
|
||||
await result.current.fetchFiles();
|
||||
});
|
||||
expect(result.current.browseFiles).toEqual([{path: 'renamed/'}]);
|
||||
|
||||
// The older fetch settles last; the newer files stay.
|
||||
await act(async () => {
|
||||
resolveBeforeRename([{path: 'old-name/'}]);
|
||||
await beforeRename;
|
||||
});
|
||||
expect(result.current.browseFiles).toEqual([{path: 'renamed/'}]);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue