From 1721bd559a2cc5af9284022ac45b7b8bb4ce8a61 Mon Sep 17 00:00:00 2001 From: Trenton H <797416+stumpylog@users.noreply.github.com> Date: Sun, 15 Nov 2026 14:33:11 -0800 Subject: [PATCH] Fix: flush file moves before the scheduled workflow websocket notification --- src/documents/tasks.py | 8 +++-- src/documents/tests/test_file_moves.py | 41 ++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/documents/tasks.py b/src/documents/tasks.py index 923af0245c..72a973c895 100644 --- a/src/documents/tasks.py +++ b/src/documents/tasks.py @@ -698,14 +698,16 @@ def check_scheduled_workflows() -> None: workflow_to_run=workflow, document=document, ) + # Move this document's files now rather than at the end + # of the task, so a long run makes steady progress and + # the notification below carries the final filename and + # modified timestamp. + flush_pending_file_moves() # Scheduled workflows dont send document_updated signal, so send a websocket update here to ensure clients are updated send_websocket_document_updated( sender=None, document=document, ) - # Move this document's files now rather than at the end - # of the task, so a long run makes steady progress. - flush_pending_file_moves() def update_document_parent_tags(tag: Tag, new_parent: Tag) -> None: diff --git a/src/documents/tests/test_file_moves.py b/src/documents/tests/test_file_moves.py index 63ddcb7cd3..b2ed6145a4 100644 --- a/src/documents/tests/test_file_moves.py +++ b/src/documents/tests/test_file_moves.py @@ -1439,6 +1439,47 @@ class TestCeleryTaskScope: assert seen[first_pk] is False assert seen[second_pk] is True + def test_websocket_notification_sent_after_move( + self, + document: Document, + paperless_dirs: PaperlessDirs, + mocker: MockerFixture, + ) -> None: + """ + GIVEN: + - A document matched by a scheduled workflow that renames it + WHEN: + - check_scheduled_workflows runs + THEN: + - The websocket notification is sent after the file has moved and + the filename column is updated, so clients see the final state + """ + trigger = WorkflowTrigger.objects.create( + type=WorkflowTrigger.WorkflowTriggerType.SCHEDULED, + schedule_offset_days=0, + schedule_date_field=WorkflowTrigger.ScheduleDateField.ADDED, + ) + workflow = Workflow.objects.create(name="scheduled", order=0) + workflow.triggers.add(trigger) + workflow.actions.add(WorkflowAction.objects.create(assign_title="unused")) + + def rename(*, document: Document, **kwargs: object) -> None: + document.title = "renamed" + document.save() + + state: dict[str, object] = {} + + def notify(*, document: Document, **kwargs: object) -> None: + state["moved"] = (paperless_dirs.originals_dir / "renamed.pdf").is_file() + state["filename"] = Document.objects.get(pk=document.pk).filename + + mocker.patch("documents.tasks.run_workflows", side_effect=rename) + mocker.patch("documents.tasks.send_websocket_document_updated", notify) + + check_scheduled_workflows.apply() + + assert state == {"moved": True, "filename": "renamed.pdf"} + @pytest.mark.django_db class TestEmptyTrashLockOrdering: