From a40beac0f64c8c023b705248e9910c04973c5e28 Mon Sep 17 00:00:00 2001 From: lrnselfreliance <33263708+lrnselfreliance@users.noreply.github.com> Date: Fri, 30 Oct 2026 18:51:02 -0600 Subject: [PATCH] A delete returns once its FileGroups are removed, and models the library afterward (#903) lib.delete() ran the modelers, indexers, and refresh cleanup inline. Those cover the whole library (any unmodeled or unindexed FileGroup anywhere), so a single-file delete waited on unrelated work and could exceed the response timeout. The deleted FileGroups are still removed before the delete returns; the post-processing now runs in the FileWorker through a queued refresh of the same paths. Co-authored-by: Claude Opus 5.5 --- wrolpi/files/lib.py | 10 +++++++--- wrolpi/files/test/test_lib.py | 18 +++++++++++++++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/wrolpi/files/lib.py b/wrolpi/files/lib.py index b9d9b2c1..94804b2a 100644 --- a/wrolpi/files/lib.py +++ b/wrolpi/files/lib.py @@ -408,8 +408,8 @@ async def delete(*paths: Union[str, pathlib.Path], force: bool = False, post_pro If any files are tagged and force is False, returns a list of tagged FileGroups without deleting. If force is True, deletes even tagged files. - If post_processing is False, the synchronous DB cleanup still runs but global modelers/indexers - are skipped — use this when the caller will trigger modeling itself (e.g. the upload endpoint + The DB cleanup of the deleted FileGroups finishes before this returns; the global modelers/indexers are + queued in the FileWorker. If post_processing is False they are not queued at all — use this when the caller will trigger modeling itself (e.g. the upload endpoint follows up with `upsert_file`).""" media_directory = get_media_directory() paths = [media_directory / i for i in paths] @@ -458,7 +458,11 @@ async def delete(*paths: Union[str, pathlib.Path], force: bool = False, post_pro # This ensures deleted FileGroups are removed before the caller continues # (e.g., upload API recreating the file). from wrolpi.files.worker import file_worker - await file_worker.refresh_sync(list(paths), post_processing=post_processing) + await file_worker.refresh_sync(list(paths), post_processing=False) + if post_processing: + # The modelers and indexers cover the whole library, so the request must not wait on them. A queued + # refresh of these paths finds nothing left to change and then runs them in the FileWorker. + file_worker.queue_refresh(list(paths), send_events=False) if had_tagged: # `tags.yaml` references TagFiles that the refresh just removed; rewrite it. diff --git a/wrolpi/files/test/test_lib.py b/wrolpi/files/test/test_lib.py index 685abe33..639c33bc 100644 --- a/wrolpi/files/test/test_lib.py +++ b/wrolpi/files/test/test_lib.py @@ -15,7 +15,7 @@ from PIL import Image import wrolpi.common from modules.videos import Video from wrolpi.common import timer, get_wrolpi_config -from wrolpi.conftest import await_switches +from wrolpi.conftest import await_switches, await_file_worker import sqlalchemy from sqlalchemy.exc import IntegrityError @@ -59,6 +59,22 @@ async def test_delete_file(async_client, test_session, make_files_structure, tes await lib.delete('.') +@pytest.mark.asyncio +async def test_delete_post_processing_in_background(async_client, test_session, make_files_structure, refresh_files): + """A delete removes its FileGroups before returning, but does not wait on the library-wide modelers; the + FileWorker runs them afterward.""" + make_files_structure(['foo.txt', 'bar.txt']) + await refresh_files() + + with mock.patch('wrolpi.files.worker.apply_modelers') as apply_modelers: + await lib.delete('foo.txt') + apply_modelers.assert_not_called() + assert [i.primary_path.name for i in test_session.query(FileGroup)] == ['bar.txt'] + + await await_file_worker() + apply_modelers.assert_called_once() + + @pytest.mark.asyncio async def test_delete_file_multiple(async_client, test_session, make_files_structure, test_directory): """Multiple files can be deleted at once."""