Skip to content

Commit 7c73c82

Browse files
committed
Don't retry on FileNotFound in celery, dramatiq
1 parent 2a3dbd6 commit 7c73c82

File tree

2 files changed

+9
-25
lines changed

2 files changed

+9
-25
lines changed

pictures/tasks.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,11 @@ def _process_picture(
3333
old = old or []
3434
storage = utils.reconstruct(*storage)
3535
if new:
36-
try:
37-
with storage.open(file_name) as fs:
38-
with Image.open(fs) as img:
39-
for picture in new:
40-
picture = utils.reconstruct(*picture)
41-
picture.save(img)
42-
except FileNotFoundError:
43-
# The file no longer exists (for example, because it was deleted or replaced).
44-
return
36+
with storage.open(file_name) as fs:
37+
with Image.open(fs) as img:
38+
for picture in new:
39+
picture = utils.reconstruct(*picture)
40+
picture.save(img)
4541

4642
for picture in old:
4743
picture = utils.reconstruct(*picture)
@@ -57,7 +53,9 @@ def _process_picture(
5753
pass
5854
else:
5955

60-
@dramatiq.actor(queue_name=conf.get_settings().QUEUE_NAME)
56+
@dramatiq.actor(
57+
queue_name=conf.get_settings().QUEUE_NAME, throws=(FileNotFoundError,)
58+
)
6159
def process_picture_with_dramatiq(
6260
storage: tuple[str, list, dict],
6361
file_name: str,
@@ -91,6 +89,7 @@ def process_picture( # noqa: F811
9189
@shared_task(
9290
ignore_results=True,
9391
retry_backoff=True,
92+
dont_autoretry_for=(FileNotFoundError,),
9493
)
9594
def process_picture_with_celery(
9695
storage: tuple[str, list, dict],

tests/test_tasks.py

-15
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,5 @@ def test_process_picture__file_cannot_be_reopened(image_upload_file):
2222
)
2323

2424

25-
@pytest.mark.django_db
26-
def test_process_picture__file_missing(image_upload_file):
27-
obj = SimpleModel.objects.create(picture=image_upload_file)
28-
setattr(
29-
obj.picture.file,
30-
"open",
31-
Mock(side_effect=FileNotFoundError("The file does not exist anymore.")),
32-
)
33-
tasks._process_picture(
34-
obj.picture.storage.deconstruct(),
35-
obj.picture.name,
36-
new=[i.deconstruct() for i in obj.picture.get_picture_files_list()],
37-
)
38-
39-
4025
def test_noop():
4126
tasks.noop() # does nothing

0 commit comments

Comments
 (0)