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 <noreply@anthropic.com>
This commit is contained in:
lrnselfreliance 2026-10-30 18:51:02 -06:00 • committed by Roland
parent 44052741d3
commit a40beac0f6
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
2 changed files with 24 additions and 4 deletions

View file

@ -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.

View file

@ -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."""