Skip to content

Commit 6feef83

Browse files
committed
✨ [#229] Don't error out immediately if a zaak can't be deleted
Before, when processing the items of a list, if one raised an error we stopped there and marked the list as failed. Now we run through all the items and at the end raise an error if one or more failed.
1 parent 21eae7d commit 6feef83

File tree

4 files changed

+36
-28
lines changed

4 files changed

+36
-28
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class DeletionProcessingError(Exception):
2+
pass

backend/src/openarchiefbeheer/destruction/models.py

+28-16
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,14 @@ def has_short_review_process(self) -> bool:
180180
[zaaktype in config.zaaktypes_short_process for zaaktype in zaaktypes_urls]
181181
)
182182

183+
def has_failures(self) -> bool:
184+
return any(
185+
[
186+
status == InternalStatus.failed
187+
for status in self.items.values_list("processing_status", flat=True)
188+
]
189+
)
190+
183191

184192
class DestructionListItem(models.Model):
185193
destruction_list = models.ForeignKey(
@@ -254,27 +262,31 @@ def set_processing_status(self, status: InternalStatus) -> None:
254262
self.processing_status = status
255263
self.save()
256264

257-
def process_deletion(self) -> None:
258-
from .utils import mark_as_failed_on_error
265+
def _delete_zaak(self):
266+
try:
267+
zaak = Zaak.objects.get(url=self.zaak)
268+
except ObjectDoesNotExist as exc:
269+
logger.error(
270+
"Could not find zaak with URL %s. Aborting deletion.", self.zaak
271+
)
272+
raise exc
259273

260-
self.processing_status = InternalStatus.processing
261-
self.save()
274+
store = ResultStore(store=self)
275+
store.clear_traceback()
262276

263-
with mark_as_failed_on_error(self):
264-
try:
265-
zaak = Zaak.objects.get(url=self.zaak)
266-
except ObjectDoesNotExist as exc:
267-
logger.error(
268-
"Could not find zaak with URL %s. Aborting deletion.", self.zaak
269-
)
270-
raise exc
277+
delete_zaak_and_related_objects(zaak=zaak, result_store=store)
271278

272-
store = ResultStore(store=self)
273-
store.clear_traceback()
279+
zaak.delete()
274280

275-
delete_zaak_and_related_objects(zaak=zaak, result_store=store)
281+
def process_deletion(self) -> None:
282+
self.processing_status = InternalStatus.processing
283+
self.save()
276284

277-
zaak.delete()
285+
try:
286+
self._delete_zaak()
287+
except Exception:
288+
self.set_processing_status(InternalStatus.failed)
289+
return
278290

279291
self.processing_status = InternalStatus.succeeded
280292
self.save()

backend/src/openarchiefbeheer/destruction/tasks.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from openarchiefbeheer.celery import app
88

99
from .constants import InternalStatus, ListItemStatus, ListStatus
10+
from .exceptions import DeletionProcessingError
1011
from .models import DestructionList, DestructionListItem, ReviewResponse
1112
from .signals import deletion_failure
1213
from .utils import notify_assignees_successful_deletion
@@ -66,11 +67,11 @@ def delete_destruction_list(destruction_list: DestructionList) -> None:
6667
chunk_tasks = delete_destruction_list_item.chunks(
6768
items_pks, settings.ZAKEN_CHUNK_SIZE
6869
)
69-
notify_task = complete_and_notify.si(destruction_list.pk)
70+
complete_and_notify_task = complete_and_notify.si(destruction_list.pk)
7071

7172
task_chain = chain(
7273
chunk_tasks.group(),
73-
notify_task,
74+
complete_and_notify_task,
7475
link_error=handle_processing_error.si(destruction_list.pk),
7576
)
7677
task_chain.delay()
@@ -99,6 +100,9 @@ def delete_destruction_list_item(pk: int) -> None:
99100
@app.task
100101
def complete_and_notify(pk: int) -> None:
101102
destruction_list = DestructionList.objects.get(pk=pk)
103+
if destruction_list.has_failures():
104+
raise DeletionProcessingError()
105+
102106
destruction_list.processing_status = InternalStatus.succeeded
103107
destruction_list.save()
104108

backend/src/openarchiefbeheer/destruction/utils.py

-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from contextlib import contextmanager
21
from typing import Protocol
32

43
from django.conf import settings
@@ -118,15 +117,6 @@ class ObjectWithStatus(Protocol):
118117
def set_processing_status(self, status: InternalStatus) -> None: ...
119118

120119

121-
@contextmanager
122-
def mark_as_failed_on_error(object_with_status: ObjectWithStatus):
123-
try:
124-
yield
125-
except Exception as exc:
126-
object_with_status.set_processing_status(InternalStatus.failed)
127-
raise exc
128-
129-
130120
def process_new_assignees(
131121
destruction_list: DestructionList,
132122
assignees: list[dict],

0 commit comments

Comments
 (0)