Skip to content

Commit

Permalink
Use BACKGROUND_EXPORT_TASK_HANDLED_EXCEPTIONS to specify user handled…
Browse files Browse the repository at this point in the history
… exceptions
  • Loading branch information
Peter Kubov committed Mar 6, 2024
1 parent de49952 commit 62ffb0b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
9 changes: 9 additions & 0 deletions docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,15 @@ These configuration you can use with django-is-core in your django settings file
def perform_action_before_export_task(request, queryset, filename, **kwargs):
logger.info(f"Exporting result of the query ({queryset.query}) into the file '{filename}'.")
.. attribute:: IS_CORE_BACKGROUND_EXPORT_TASK_CALLBACK_HANDLED_EXCEPTIONS

A list of exceptions declared as explicitly handled by the library user that may occur during
the background export. These exceptions do abort background export and trigger export_failed signal.
However, the background export task finishes successfully without a failure.

# Django settings
IS_CORE_BACKGROUND_EXPORT_TASK_CALLBACK_HANDLED_EXCEPTIONS = ('your.file.Exception1', 'your.file.Exception2')

.. attribute:: IS_CORE_BACKGROUND_EXPORT_TASK_QUEUE

The celery queue name which will be used for the background export.
Expand Down
1 change: 1 addition & 0 deletions is_core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def _get_auth_login_view():
'BACKGROUND_EXPORT_TASK_SOFT_TIME_LIMIT': (60*60) - 5,
'BACKGROUND_EXPORT_TASK_UPDATE_REQUEST_FUNCTION': None,
'BACKGROUND_EXPORT_TASK_CALLBACK': None,
'BACKGROUND_EXPORT_TASK_CALLBACK_HANDLED_EXCEPTIONS': (),
'BACKGROUND_EXPORT_TASK_QUEUE': None,
'BACKGROUND_EXPORT_SERIALIZATION_LIMIT': 2000,
'BACKGROUND_EXPORT_STORAGE_CLASS': 'django.core.files.storage.DefaultStorage',
Expand Down
16 changes: 10 additions & 6 deletions is_core/contrib/background_export/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,16 @@ def background_serialization(self, exported_file_pk, rest_context, language, req

# Perform user-defined verification before saving exported file into the database.
if settings.BACKGROUND_EXPORT_TASK_CALLBACK:
import_string(settings.BACKGROUND_EXPORT_TASK_CALLBACK)(
request=request,
queryset=queryset,
filename=filename,
exported_file=exported_file,
)
try:
import_string(settings.BACKGROUND_EXPORT_TASK_CALLBACK)(
request=request,
queryset=queryset,
filename=filename,
exported_file=exported_file,
)
except tuple(import_string(exc) for exc in settings.BACKGROUND_EXPORT_TASK_CALLBACK_HANDLED_EXCEPTIONS) as exc:
export_failure.send(sender=self.__class__, exception=exc)
return

exported_file.file.save(filename, ContentFile(''))

Expand Down

0 comments on commit 62ffb0b

Please sign in to comment.