From 2a3dbd6559ddf27517e895618d7ea6a98dc356e8 Mon Sep 17 00:00:00 2001 From: Rust Saiargaliev Date: Wed, 5 Feb 2025 15:38:41 +0100 Subject: [PATCH 1/2] Do not retry processing when there is no picture --- pictures/tasks.py | 14 +++++++++----- tests/test_tasks.py | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/pictures/tasks.py b/pictures/tasks.py index 48d0223..73b0539 100644 --- a/pictures/tasks.py +++ b/pictures/tasks.py @@ -33,11 +33,15 @@ def _process_picture( old = old or [] storage = utils.reconstruct(*storage) if new: - with storage.open(file_name) as fs: - with Image.open(fs) as img: - for picture in new: - picture = utils.reconstruct(*picture) - picture.save(img) + try: + with storage.open(file_name) as fs: + with Image.open(fs) as img: + for picture in new: + picture = utils.reconstruct(*picture) + picture.save(img) + except FileNotFoundError: + # The file no longer exists (for example, because it was deleted or replaced). + return for picture in old: picture = utils.reconstruct(*picture) diff --git a/tests/test_tasks.py b/tests/test_tasks.py index a3b655a..a8a4005 100644 --- a/tests/test_tasks.py +++ b/tests/test_tasks.py @@ -22,5 +22,20 @@ def test_process_picture__file_cannot_be_reopened(image_upload_file): ) +@pytest.mark.django_db +def test_process_picture__file_missing(image_upload_file): + obj = SimpleModel.objects.create(picture=image_upload_file) + setattr( + obj.picture.file, + "open", + Mock(side_effect=FileNotFoundError("The file does not exist anymore.")), + ) + tasks._process_picture( + obj.picture.storage.deconstruct(), + obj.picture.name, + new=[i.deconstruct() for i in obj.picture.get_picture_files_list()], + ) + + def test_noop(): tasks.noop() # does nothing From 7c73c82f297750d7fa0989ec19aec29245780676 Mon Sep 17 00:00:00 2001 From: Rust Saiargaliev Date: Tue, 11 Feb 2025 10:18:38 +0100 Subject: [PATCH 2/2] Don't retry on FileNotFound in celery, dramatiq --- pictures/tasks.py | 19 +++++++++---------- tests/test_tasks.py | 15 --------------- 2 files changed, 9 insertions(+), 25 deletions(-) diff --git a/pictures/tasks.py b/pictures/tasks.py index 73b0539..9d708f9 100644 --- a/pictures/tasks.py +++ b/pictures/tasks.py @@ -33,15 +33,11 @@ def _process_picture( old = old or [] storage = utils.reconstruct(*storage) if new: - try: - with storage.open(file_name) as fs: - with Image.open(fs) as img: - for picture in new: - picture = utils.reconstruct(*picture) - picture.save(img) - except FileNotFoundError: - # The file no longer exists (for example, because it was deleted or replaced). - return + with storage.open(file_name) as fs: + with Image.open(fs) as img: + for picture in new: + picture = utils.reconstruct(*picture) + picture.save(img) for picture in old: picture = utils.reconstruct(*picture) @@ -57,7 +53,9 @@ def _process_picture( pass else: - @dramatiq.actor(queue_name=conf.get_settings().QUEUE_NAME) + @dramatiq.actor( + queue_name=conf.get_settings().QUEUE_NAME, throws=(FileNotFoundError,) + ) def process_picture_with_dramatiq( storage: tuple[str, list, dict], file_name: str, @@ -91,6 +89,7 @@ def process_picture( # noqa: F811 @shared_task( ignore_results=True, retry_backoff=True, + dont_autoretry_for=(FileNotFoundError,), ) def process_picture_with_celery( storage: tuple[str, list, dict], diff --git a/tests/test_tasks.py b/tests/test_tasks.py index a8a4005..a3b655a 100644 --- a/tests/test_tasks.py +++ b/tests/test_tasks.py @@ -22,20 +22,5 @@ def test_process_picture__file_cannot_be_reopened(image_upload_file): ) -@pytest.mark.django_db -def test_process_picture__file_missing(image_upload_file): - obj = SimpleModel.objects.create(picture=image_upload_file) - setattr( - obj.picture.file, - "open", - Mock(side_effect=FileNotFoundError("The file does not exist anymore.")), - ) - tasks._process_picture( - obj.picture.storage.deconstruct(), - obj.picture.name, - new=[i.deconstruct() for i in obj.picture.get_picture_files_list()], - ) - - def test_noop(): tasks.noop() # does nothing