wrolpi/wrolpi/files/test/test_worker_move.py
Roland 85f22b61ca
files: a failed directory rename that hung for five minutes
A directory rename waits inline on a move job. When that move failed
(stale FileGroup rows colliding on primary_path), handle_move reverted
the files and sent a failure event but left the job pending. wait_for_job
only understands 'complete', so POST /api/files/rename spun until its
300s timeout and the Rename modal never closed.

The same sequence could kill file processing for the life of the process.
perpetual_signal treated any CancelledError as terminal and logged
nothing. Separately, perpetual_tasks_started is a shared Event that
survives the worker that set it: docker auto-reloads and worker crashes
left every later after_server_start as a no-op. Queued refreshes sat
untouched while worker_status reported idle. Restarting the API was the
only recovery.

Mark failed jobs failed and raise immediately from wait_for_job. Catch
unexpected cancellation and reschedule unless the app is actually
stopping. Track the perpetual-loop owner PID and retake the claim when
that process is gone; every Sanic worker still runs the file-worker
pump. Harden transfer_queue against the cross-process Empty race, and
stop request processes stealing from the shared public queue.
2026-08-19 18:57:17 -06:00

676 lines
23 KiB
Python

"""Tests for the FileWorker move task."""
import contextlib
import pytest
from wrolpi.conftest import production_like_sessions, write_lock_held_briefly
from wrolpi.files import worker as worker_module
from wrolpi.files.lib import _move_file_group_files
from wrolpi.files.models import FileGroup, Directory
from wrolpi.files.worker import file_worker, FileTask, FileTaskType, FileWorkerJobFailed, build_move_plan_bulk
@pytest.mark.asyncio
async def test_file_worker_queue_move(async_client, test_session, test_directory, make_files_structure):
"""FileWorker accepts and queues move tasks."""
file1, = make_files_structure(['source/file1.txt'])
dest = test_directory / 'destination'
dest.mkdir()
# Queue a move task
file_worker.queue_move(dest, [file1.parent])
# Verify task is in the public queue
assert file_worker.public_queue.qsize() == 1
@pytest.mark.asyncio
async def test_file_worker_move_single_file(async_client, test_session, test_directory, make_files_structure):
"""FileWorker moves a single file to destination."""
file1, = make_files_structure(['source/file1.txt'])
fg = FileGroup.from_paths(test_session, file1)
test_session.commit()
original_id = fg.id
dest = test_directory / 'destination'
dest.mkdir()
task = FileTask(FileTaskType.move, [file1], destination=dest)
file_worker.private_queue.put_nowait(task)
await file_worker.process_queue()
# Verify file was moved
assert (dest / 'file1.txt').exists()
assert not file1.exists()
# Verify FileGroup was updated
test_session.expire_all()
fg = test_session.query(FileGroup).filter(FileGroup.id == original_id).one()
assert str(dest) in str(fg.directory)
assert 'file1.txt' in str(fg.primary_path)
@pytest.mark.asyncio
async def test_file_worker_move_directory(async_client, test_session, test_directory, make_files_structure):
"""FileWorker moves an entire directory to destination."""
files = make_files_structure([
'source/file1.txt',
'source/file2.txt',
'source/subdir/file3.txt',
])
# Create FileGroups
for f in files:
FileGroup.from_paths(test_session, f)
test_session.commit()
source_dir = test_directory / 'source'
dest = test_directory / 'destination'
dest.mkdir()
task = FileTask(FileTaskType.move, [source_dir], destination=dest)
file_worker.private_queue.put_nowait(task)
await file_worker.process_queue()
# Verify files were moved
assert (dest / 'source' / 'file1.txt').exists()
assert (dest / 'source' / 'file2.txt').exists()
assert (dest / 'source' / 'subdir' / 'file3.txt').exists()
# Verify original files are gone
assert not (source_dir / 'file1.txt').exists()
# Verify FileGroups were updated
test_session.expire_all()
file_groups = test_session.query(FileGroup).all()
assert len(file_groups) == 3
for fg in file_groups:
assert str(dest) in str(fg.directory)
@pytest.mark.asyncio
async def test_file_worker_move_status_updates(async_client, test_session, test_directory, make_files_structure):
"""FileWorker updates status during move operation."""
files = make_files_structure([f'source/file{i}.txt' for i in range(5)])
for f in files:
FileGroup.from_paths(test_session, f)
test_session.commit()
dest = test_directory / 'destination'
dest.mkdir()
task = FileTask(FileTaskType.move, [test_directory / 'source'], destination=dest)
file_worker.private_queue.put_nowait(task)
await file_worker.process_queue()
# After completion, status should be reset to idle
status = file_worker.status
assert status['status'] == 'idle'
assert status['destination'] is None
@pytest.mark.asyncio
async def test_file_worker_move_preserves_tags(
async_client, test_session, test_directory, make_files_structure, tag_factory, await_switches
):
"""FileWorker preserves tags when moving files."""
file1, = make_files_structure(['source/file1.txt'])
fg = FileGroup.from_paths(test_session, file1)
test_session.commit()
tag = await tag_factory()
fg.add_tag(test_session, tag.id)
test_session.commit()
await await_switches()
original_id = fg.id
dest = test_directory / 'destination'
dest.mkdir()
task = FileTask(FileTaskType.move, [file1], destination=dest)
file_worker.private_queue.put_nowait(task)
await file_worker.process_queue()
# Verify tag is preserved
test_session.expire_all()
fg = test_session.query(FileGroup).filter(FileGroup.id == original_id).one()
assert len(fg.tag_files) == 1
assert fg.tag_files[0].tag_id == tag.id
@pytest.mark.asyncio
async def test_file_worker_move_with_shared_stem_files(
async_client, test_session, test_directory, make_files_structure, video_file_factory
):
"""FileWorker moves all files sharing a stem together."""
docs_dir = test_directory / 'source'
docs_dir.mkdir(parents=True, exist_ok=True)
video_path = video_file_factory(docs_dir / 'video1.mp4')
info_path = docs_dir / 'video1.info.json'
info_path.write_text('{"title": "test"}')
fg = FileGroup.from_paths(test_session, video_path)
fg.files.append({'path': 'video1.info.json', 'mimetype': 'application/json'})
test_session.commit()
dest = test_directory / 'destination'
dest.mkdir()
task = FileTask(FileTaskType.move, [video_path], destination=dest)
file_worker.private_queue.put_nowait(task)
await file_worker.process_queue()
# Verify both files were moved
assert (dest / 'video1.mp4').exists()
assert (dest / 'video1.info.json').exists()
assert not video_path.exists()
assert not info_path.exists()
@pytest.mark.asyncio
async def test_file_worker_move_error_file_exists(
async_client, test_session, test_directory, make_files_structure
):
"""FileWorker handles error when destination file already exists."""
source_file, dest_file = make_files_structure([
'source/file1.txt',
'destination/file1.txt',
])
fg = FileGroup.from_paths(test_session, source_file)
test_session.commit()
dest = test_directory / 'destination'
task = FileTask(FileTaskType.move, [source_file], destination=dest)
file_worker.private_queue.put_nowait(task)
await file_worker.process_queue()
# Status should be reset to idle (error handled gracefully)
status = file_worker.status
assert status['status'] == 'idle'
# Source file should still exist (move failed)
assert source_file.exists()
@pytest.mark.asyncio
async def test_file_worker_move_invalid_source(async_client, test_session, test_directory):
"""FileWorker handles invalid source paths gracefully."""
nonexistent = test_directory / 'nonexistent'
dest = test_directory / 'destination'
dest.mkdir()
task = FileTask(FileTaskType.move, [nonexistent], destination=dest)
file_worker.private_queue.put_nowait(task)
await file_worker.process_queue()
# Status should be reset to idle
status = file_worker.status
assert status['status'] == 'idle'
@pytest.mark.asyncio
async def test_file_worker_move_bulk_files(async_client, test_session, test_directory, make_files_structure):
"""FileWorker handles bulk moves efficiently with chunking."""
# Create 150+ files to test chunking (MOVE_CHUNK_SIZE = 100)
file_count = 150
files = make_files_structure([f'source/file{i}.txt' for i in range(file_count)])
for f in files:
FileGroup.from_paths(test_session, f)
test_session.commit()
dest = test_directory / 'destination'
dest.mkdir()
task = FileTask(FileTaskType.move, [test_directory / 'source'], destination=dest)
file_worker.private_queue.put_nowait(task)
await file_worker.process_queue()
# Verify all files moved
test_session.expire_all()
moved_count = test_session.query(FileGroup).filter(
FileGroup.directory.like(f'{dest}%')
).count()
assert moved_count == file_count
# Verify physical files exist
assert (dest / 'source' / 'file0.txt').exists()
assert (dest / 'source' / f'file{file_count - 1}.txt').exists()
@pytest.mark.asyncio
async def test_file_worker_move_creates_destination_directories(
async_client, test_session, test_directory, make_files_structure
):
"""FileWorker creates destination directories as needed."""
file1, = make_files_structure(['source/file1.txt'])
fg = FileGroup.from_paths(test_session, file1)
test_session.commit()
# Destination doesn't exist yet
dest = test_directory / 'new' / 'nested' / 'destination'
assert not dest.exists()
task = FileTask(FileTaskType.move, [file1], destination=dest)
file_worker.private_queue.put_nowait(task)
await file_worker.process_queue()
# Verify destination was created and file moved
assert dest.exists()
assert (dest / 'file1.txt').exists()
@pytest.mark.asyncio
async def test_file_worker_move_multiple_sources(
async_client, test_session, test_directory, make_files_structure
):
"""FileWorker moves multiple source files to one destination."""
file1, file2 = make_files_structure([
'source1/file1.txt',
'source2/file2.txt',
])
fg1 = FileGroup.from_paths(test_session, file1)
fg2 = FileGroup.from_paths(test_session, file2)
test_session.commit()
dest = test_directory / 'destination'
dest.mkdir()
task = FileTask(FileTaskType.move, [file1, file2], destination=dest)
file_worker.private_queue.put_nowait(task)
await file_worker.process_queue()
# Verify both files moved
assert (dest / 'file1.txt').exists()
assert (dest / 'file2.txt').exists()
assert not file1.exists()
assert not file2.exists()
@pytest.mark.asyncio
async def test_file_worker_move_already_moved_files(
async_client, test_session, test_directory, make_files_structure
):
"""FileWorker handles files that were already moved on filesystem.
When a user manually moves files and then tells the worker to move them,
the worker should detect the files are already at the destination and
simply update the database records.
"""
import shutil
# Create file and FileGroup at original location
file1, = make_files_structure(['source/file1.txt'])
fg = FileGroup.from_paths(test_session, file1)
test_session.commit()
original_id = fg.id
# User manually moves file on filesystem (outside of WROLPi)
dest = test_directory / 'destination'
dest.mkdir()
shutil.move(str(file1), str(dest / 'file1.txt'))
# Verify: source gone, dest exists
assert not file1.exists()
assert (dest / 'file1.txt').exists()
# Tell worker to move (from old location to new)
task = FileTask(FileTaskType.move, [file1], destination=dest)
file_worker.private_queue.put_nowait(task)
await file_worker.process_queue()
# Verify FileGroup was updated to new location
test_session.expire_all()
fg = test_session.query(FileGroup).filter(FileGroup.id == original_id).one()
assert str(dest) in str(fg.directory)
assert 'file1.txt' in str(fg.primary_path)
# Status should be idle (success)
assert file_worker.status['status'] == 'idle'
# ============================================================================
# Tests for missing scenarios from files.lib.move
# ============================================================================
@pytest.mark.asyncio
async def test_file_worker_move_rollback_on_failure(
async_client, test_session, test_directory, make_files_structure, monkeypatch
):
"""FileWorker rolls back moved files when an error occurs mid-move."""
files = make_files_structure([
'source/file1.txt',
'source/file2.txt',
'source/file3.txt',
])
for f in files:
FileGroup.from_paths(test_session, f)
test_session.commit()
dest = test_directory / 'destination'
dest.mkdir()
# Patch _move_file_group_files to fail after first file
call_count = 0
original_move = _move_file_group_files
def failing_move(fg, new_path):
nonlocal call_count
call_count += 1
if call_count > 1:
raise IOError("Simulated disk failure")
return original_move(fg, new_path)
monkeypatch.setattr('wrolpi.files.worker._move_file_group_files', failing_move)
task = FileTask(FileTaskType.move, [test_directory / 'source'], destination=dest)
file_worker.private_queue.put_nowait(task)
await file_worker.process_queue()
# All source files should still exist (rolled back)
assert files[0].exists(), f"file1.txt should exist after rollback"
assert files[1].exists(), f"file2.txt should exist after rollback"
assert files[2].exists(), f"file3.txt should exist after rollback"
# Destination should be empty or not exist
dest_source = dest / 'source'
assert not dest_source.exists() or not list(dest_source.iterdir()), \
"Destination should be empty after rollback"
@pytest.mark.asyncio
async def test_handle_move_marks_job_failed_on_exception(
async_client, test_session, test_directory, make_files_structure, monkeypatch
):
"""A failed move must mark its tracked job 'failed' so wait_for_job can return.
Regression: handle_move's except path sent a failure event and reverted files
but never touched the job status, leaving it 'pending' forever.
"""
source_file, = make_files_structure(['source/file1.txt'])
FileGroup.from_paths(test_session, source_file)
test_session.commit()
dest = test_directory / 'destination'
dest.mkdir()
def failing_move(fg, new_path):
raise IOError('Simulated disk failure')
monkeypatch.setattr('wrolpi.files.worker._move_file_group_files', failing_move)
job_id = 'move-will-fail'
task = FileTask(FileTaskType.move, [source_file], destination=dest, job_id=job_id)
file_worker._set_job_status(job_id, 'pending')
file_worker.private_queue.put_nowait(task)
await file_worker.process_queue()
assert file_worker.get_job_status(job_id) == 'failed'
assert 'Simulated disk failure' in (file_worker.get_job_error(job_id) or '')
assert source_file.exists(), 'Source should be reverted'
@pytest.mark.asyncio
async def test_wait_for_job_fails_promptly_on_stale_destination_filegroup(
async_client, test_session, test_directory, make_files_structure
):
"""A move that collides with a stale destination FileGroup must fail promptly.
Reproduction from the incident: rename dir-a -> dir-b, delete dir-b on disk
without a refresh, recreate dir-a/one.txt, rename again. The move plan
INSERTs a FileGroup for the new source then UPDATEs primary_path onto the
stale destination row and hits UNIQUE constraint. The waiter must raise
FileWorkerJobFailed immediately, not spin until the 300s timeout.
"""
source, = make_files_structure({'dir-a/one.txt': 'hello'})
FileGroup.from_paths(test_session, source)
test_session.commit()
dest_b = test_directory / 'dir-b'
first_job = file_worker.queue_move(dest_b, [source])
await file_worker.wait_for_job(first_job, timeout=10)
assert (dest_b / 'one.txt').exists()
# Dest gone on disk; DB still holds dir-b/one.txt. Recreate the old source.
# The first move leaves the empty source directory in place.
import shutil
shutil.rmtree(dest_b)
source.parent.mkdir(exist_ok=True)
source.write_text('hello')
second_job = file_worker.queue_move(dest_b, [source])
with pytest.raises(FileWorkerJobFailed):
await file_worker.wait_for_job(second_job, timeout=5)
assert file_worker.get_job_status(second_job) == 'failed'
assert source.exists(), 'Failed move should revert the source file'
@pytest.mark.asyncio
async def test_file_worker_move_unindexed_files(
async_client, test_session, test_directory, make_files_structure
):
"""FileWorker creates FileGroups for files not yet in database."""
# Create files but don't add them all to DB
files = make_files_structure([
'source/indexed.txt',
'source/unindexed.txt',
])
# Only index one file
FileGroup.from_paths(test_session, files[0])
test_session.commit()
dest = test_directory / 'destination'
dest.mkdir()
task = FileTask(FileTaskType.move, [test_directory / 'source'], destination=dest)
file_worker.private_queue.put_nowait(task)
await file_worker.process_queue()
# Both files should be moved
assert (dest / 'source' / 'indexed.txt').exists()
assert (dest / 'source' / 'unindexed.txt').exists()
# Both should have FileGroups now
test_session.expire_all()
file_groups = test_session.query(FileGroup).all()
assert len(file_groups) == 2
@pytest.mark.asyncio
async def test_file_worker_move_preserves_existing_destination_on_failure(
async_client, test_session, test_directory, make_files_structure, monkeypatch
):
"""FileWorker preserves existing destination directory on failure."""
source_file, existing_file = make_files_structure([
'source/file1.txt',
'destination/existing.txt', # Pre-existing file
])
FileGroup.from_paths(test_session, source_file)
test_session.commit()
dest = test_directory / 'destination'
# Patch to fail
def failing_move(fg, new_path):
raise IOError("Simulated failure")
monkeypatch.setattr('wrolpi.files.worker._move_file_group_files', failing_move)
task = FileTask(FileTaskType.move, [source_file], destination=dest)
file_worker.private_queue.put_nowait(task)
await file_worker.process_queue()
# Destination should still exist with original file
assert dest.exists(), "Existing destination should be preserved"
assert existing_file.exists(), "Existing file should be preserved"
@pytest.mark.asyncio
async def test_file_worker_move_removes_created_destination_on_failure(
async_client, test_session, test_directory, make_files_structure, monkeypatch
):
"""FileWorker removes destination directory it created when move fails."""
source_file, = make_files_structure(['source/file1.txt'])
FileGroup.from_paths(test_session, source_file)
test_session.commit()
dest = test_directory / 'new_destination'
assert not dest.exists() # Doesn't exist yet
# Patch to fail
def failing_move(fg, new_path):
raise IOError("Simulated failure")
monkeypatch.setattr('wrolpi.files.worker._move_file_group_files', failing_move)
task = FileTask(FileTaskType.move, [source_file], destination=dest)
file_worker.private_queue.put_nowait(task)
await file_worker.process_queue()
# Destination should NOT exist (was created by move and should be cleaned up)
assert not dest.exists(), "Created destination should be removed on failure"
@pytest.mark.asyncio
async def test_file_worker_move_cleans_directory_records(
async_client, test_session, test_directory, make_files_structure
):
"""FileWorker removes Directory records for directories that no longer exist.
Directories that still exist (even if empty) keep their DB records.
"""
files = make_files_structure([
'source/subdir/file1.txt',
])
# Create directory records
source_dir = test_directory / 'source'
subdir = source_dir / 'subdir'
test_session.add(Directory(path=str(source_dir), name='source'))
test_session.add(Directory(path=str(subdir), name='subdir'))
FileGroup.from_paths(test_session, files[0])
test_session.commit()
dest = test_directory / 'destination'
dest.mkdir()
task = FileTask(FileTaskType.move, [source_dir], destination=dest)
file_worker.private_queue.put_nowait(task)
await file_worker.process_queue()
test_session.expire_all()
# subdir no longer exists - its DB record should be deleted
subdir_record = test_session.query(Directory).filter(
Directory.path == str(subdir)
).one_or_none()
assert subdir_record is None, "subdir Directory record should be deleted (directory no longer exists)"
assert not subdir.exists(), "subdir should not exist on disk"
# source still exists (empty) - its DB record should remain
source_record = test_session.query(Directory).filter(
Directory.path == str(source_dir)
).one_or_none()
assert source_record is not None, "source Directory record should remain (directory still exists)"
assert source_dir.exists(), "source should still exist on disk (empty)"
@pytest.mark.asyncio
async def test_build_move_plan_waits_for_concurrent_writer(
async_client, test_session, test_directory, make_files_structure
):
"""`build_move_plan_bulk` must not fail instantly when another connection is writing.
Regression test for the tag-a-channel move that died with `database is locked` while
INSERTing a FileGroup for an unindexed file. The plan's transaction begins *deferred*
(a read), then upgrades to a writer at that INSERT; SQLite refuses a lock upgrade
immediately, skipping busy_timeout, so any concurrent writer aborts the whole move.
Beginning the transaction as a writer lets busy_timeout absorb the contention instead.
"""
# An unindexed file, so planning must INSERT a FileGroup (the write that failed in production).
file1, = make_files_structure(['source/file1.txt'])
dest = test_directory / 'destination'
dest.mkdir()
db_file = test_session.get_bind().url.database
with production_like_sessions(test_session):
with write_lock_held_briefly(db_file):
plan, _ = await build_move_plan_bulk([file1], dest)
assert plan == {file1: dest / 'file1.txt'}
@pytest.mark.asyncio
async def test_file_worker_move_survives_concurrent_writer(
async_client, test_session, test_directory, make_files_structure, monkeypatch
):
"""Executing a move must survive a concurrent writer holding the write lock.
`_execute_move_chunks` reads FileGroups/Directories and then writes them, so its transaction
must begin as a writer too - SQLite refuses the mid-transaction lock upgrade instantly and the
move aborts. The lock is taken only *after* planning finishes, so this fails if the execute
site alone regresses; planning cannot absorb the contention on the execute site's behalf.
"""
file1, = make_files_structure(['source/file1.txt'])
FileGroup.from_paths(test_session, file1)
test_session.commit()
dest = test_directory / 'destination'
dest.mkdir()
db_file = test_session.get_bind().url.database
task = FileTask(FileTaskType.move, [file1], destination=dest)
file_worker.private_queue.put_nowait(task)
with contextlib.ExitStack() as stack:
maker = stack.enter_context(production_like_sessions(test_session))
real_build_move_plan_bulk = worker_module.build_move_plan_bulk
async def build_plan_then_hold_write_lock(*args, **kwargs):
plan = await real_build_move_plan_bulk(*args, **kwargs)
# Planning is done; the contention now falls entirely on the execute phase.
stack.enter_context(write_lock_held_briefly(db_file))
return plan
monkeypatch.setattr(worker_module, 'build_move_plan_bulk', build_plan_then_hold_write_lock)
await file_worker.process_queue()
assert (dest / 'file1.txt').is_file(), 'file was not moved'
assert not file1.exists(), 'file was left at its old path'
session = maker()
fg = session.query(FileGroup).one()
assert fg.primary_path == dest / 'file1.txt', 'FileGroup was not updated to the new path'