Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configurable soft and hard time limits for import and export tasks #107

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ By default a dry run of the import is initiated when the import object is create
IMPORT_DRY_RUN_FIRST_TIME = False


Preforming an import
Performing an import
--------------------

You will find an example django application that uses django-import-export-celery for importing data. There are instructions for running the example application in the example directory's README file. Once you have it running, you can perform an import with the following steps.
Expand Down Expand Up @@ -178,6 +178,21 @@ Define a custom storage backend by adding the `IMPORT_EXPORT_CELERY_STORAGE` to

IMPORT_EXPORT_CELERY_STORAGE = "storages.backends.s3boto3.S3Boto3Storage"

Customizing Task Time Limits
----------------------------

By default, there is no time limit on celery import/export tasks. This can be customized by setting the following variables in your Django settings file.

::

# set import time limits (in seconds)
IMPORT_EXPORT_CELERY_IMPORT_SOFT_TIME_LIMIT = 300 # 5 minutes
IMPORT_EXPORT_CELERY_IMPORT_HARD_TIME_LIMIT = 360 # 6 minutes

# set export time limits (in seconds)
IMPORT_EXPORT_CELERY_EXPORT_SOFT_TIME_LIMIT = 300 # 5 minutes
IMPORT_EXPORT_CELERY_EXPORT_HARD_TIME_LIMIT = 360 # 6 minutes

Customizing email template for export job completion email
----------------------------------------------------------

Expand Down
8 changes: 8 additions & 0 deletions example/project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,11 @@

EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
IMPORT_EXPORT_CELERY_STORAGE = "django.core.files.storage.FileSystemStorage"

# Default import time limits (in seconds)
IMPORT_EXPORT_CELERY_IMPORT_SOFT_TIME_LIMIT = 300 # 5 minutes
IMPORT_EXPORT_CELERY_IMPORT_HARD_TIME_LIMIT = 360 # 6 minutes

# Default export time limits (in seconds)
IMPORT_EXPORT_CELERY_EXPORT_SOFT_TIME_LIMIT = 300 # 5 minutes
IMPORT_EXPORT_CELERY_EXPORT_HARD_TIME_LIMIT = 360 # 6 minutes
12 changes: 10 additions & 2 deletions import_export_celery/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,11 @@ def before_import_row(self, row, **kwargs):
import_job.save()


@shared_task(bind=False)
@shared_task(
bind=False,
soft_time_limit=getattr(settings, "IMPORT_EXPORT_CELERY_IMPORT_SOFT_TIME_LIMIT", 0),
time_limit=getattr(settings, "IMPORT_EXPORT_CELERY_IMPORT_HARD_TIME_LIMIT", 0),
)
def run_import_job(pk, dry_run=True):
log.info(f"Importing {pk} dry-run {dry_run}")
import_job = models.ImportJob.objects.get(pk=pk)
Expand All @@ -201,7 +205,11 @@ def run_import_job(pk, dry_run=True):
return


@shared_task(bind=False)
@shared_task(
bind=False,
soft_time_limit=getattr(settings, "IMPORT_EXPORT_CELERY_EXPORT_SOFT_TIME_LIMIT", 0),
time_limit=getattr(settings, "IMPORT_EXPORT_CELERY_EXPORT_HARD_TIME_LIMIT", 0),
)
def run_export_job(pk):
log.info("Exporting %s" % pk)
export_job = models.ExportJob.objects.get(pk=pk)
Expand Down
Loading