From aff5f1eb1f3db3c756113afca1a9ecd077a272ea Mon Sep 17 00:00:00 2001 From: lrnselfreliance <33263708+lrnselfreliance@users.noreply.github.com> Date: Sat, 7 Nov 2026 13:09:33 -0700 Subject: [PATCH] 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 * 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/ 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 --------- Co-authored-by: Claude Opus 5.5 --- app/cypress/e2e/files/footer-rename.cy.js | 31 ++-- app/src/api.js | 18 ++ app/src/components/FileBrowser.js | 60 ++++++- app/src/components/FileBrowser.test.js | 91 +++++++++- app/src/components/Files.js | 16 +- .../components/FilesRefreshProgress.test.js | 44 +++++ app/src/hooks/customHooks.js | 12 +- app/src/hooks/customHooks.test.js | 39 ++++- .../collections/test/test_playlists_config.py | 2 +- wrolpi/contexts.py | 1 + wrolpi/files/api.py | 33 +++- wrolpi/files/lib.py | 49 ++---- wrolpi/files/move.py | 55 +++++- wrolpi/files/test/test_api.py | 164 ++++++++++++++++-- wrolpi/files/test/test_lib.py | 3 +- wrolpi/files/worker.py | 10 +- 16 files changed, 532 insertions(+), 96 deletions(-) create mode 100644 app/src/components/FilesRefreshProgress.test.js diff --git a/app/cypress/e2e/files/footer-rename.cy.js b/app/cypress/e2e/files/footer-rename.cy.js index 843397cd..e9d8f199 100644 --- a/app/cypress/e2e/files/footer-rename.cy.js +++ b/app/cypress/e2e/files/footer-rename.cy.js @@ -108,22 +108,29 @@ import { selectRow('fb-alpha'); footerButton('Rename').click(); - drainQueuedRefresh(() => { + // A directory rename is a file-worker move job; the request returns once it is queued, and the job + // reports when it is done. No refresh is queued. + cy.serverNow().then((since) => { dialog().within(() => { cy.get('input').clear().type('fb-alpha-renamed'); cy.contains('button', 'Rename').click(); }); - - // A directory rename runs as a file-worker move job; the modal stays open until - // the job finishes, which takes longer than the default timeout. - cy.get('[role="dialog"]', {timeout: 30000}).should('not.exist'); - row('fb-alpha-renamed').should('be.visible'); - // The folder row's text is just the name, so anchoring excludes 'fb-alpha-renamed'. - cy.contains('tr', /fb-alpha$/).should('not.exist'); - - cy.task('fb:readdir', 'fb-alpha-renamed') - .should('deep.equal', ['fb-one.txt', 'fb-two.txt']); - cy.task('fb:exists', 'fb-alpha').should('be.false'); + dialog().should('not.exist'); + cy.waitForEvent({ + since, + event: 'file_move_completed', + message: 'Renamed fb-alpha to fb-alpha-renamed', + failureEvent: 'file_move_failed', + }); }); + + // The table is fetched again once the file worker is idle. + cy.contains('tr', 'fb-alpha-renamed', {timeout: 10000}).should('be.visible'); + // The folder row's text is just the name, so anchoring excludes 'fb-alpha-renamed'. + cy.contains('tr', /fb-alpha$/).should('not.exist'); + + cy.task('fb:readdir', 'fb-alpha-renamed') + .should('deep.equal', ['fb-one.txt', 'fb-two.txt']); + cy.task('fb:exists', 'fb-alpha').should('be.false'); }); }); diff --git a/app/src/api.js b/app/src/api.js index 527ad696..1ccf5275 100644 --- a/app/src/api.js +++ b/app/src/api.js @@ -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) { diff --git a/app/src/components/FileBrowser.js b/app/src/components/FileBrowser.js index cd267f67..e4cce687 100644 --- a/app/src/components/FileBrowser.js +++ b/app/src/components/FileBrowser.js @@ -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}