Skip to main content

django-db-queue-exports

An extension to django-db-queue for monitoring long running task statuses. The aim of this extension is to simplify the execution of long running tasks, and allow for polling of tasks statuses during execution.

Build Status pypi release

Supported and tested against:

  • Django 2.2
  • django-db-queue 1.3.0
  • Python 3.6, 3.7 and 3.8

What and why?

Have you ever been in a position where you need to run a report, or email several thousand users without blocking your main process? django-db-queue can handle this perfectly for you through the use of a seperate worker process and a queue of tasks. However, it's difficult to determine the state of your task. This package aims to address this. Through the use of a pre-configured view, url and generic task, you can use a single endpoint to create new exports and query their statuses.

Getting started

Installation

pip install django-db-queue-exports

Add django_dbq_exports to the installed apps, found inside your settings.py

INSTALLED_APPS = (
    ...
    'django_dbq_exports',
)

Add django_dbq_exports.tasks.export_task to django-dbq JOBS list in settings.py

JOBS = {
    ...
    'export': {
        'tasks': ['django_dbq_exports.tasks.export_task'],
    },
}

Configure the url, something like this:

urlpatterns = [
    ...
    url(r'^export/', include("django_dbq_exports.urls")),
]

Remember to run your migrations

python manage.py migrate

Usage

Describing your export

An export is a standard python function. It must take an export_params dictionary parameter. This can be utilised for any parameters required within your export. The export can also optionally return a string value which will be stored in Export.result_reference. This is best used for file paths or URLs when you need to download or access the results of the export. Here's an example task:

import random

def generate_example_report(export_params):
    output_file = 'myfile.csv'
    array_length = export_params.get("length", None)
    x = []

    for i in range(array_length if array_length else 99):
        x.append(random.randint(1, 10))

    x.sort()
    with open(output_file, 'w') as f:
        f.write(",".join(str(y) for y in x))

    return output_file

Configure your task in settings.py

EXPORTS = {
    "my_export": "my_project.tasks.generate_example_report",
}

Running the task

Simply POST to the pre-configured endpoint with the following json. The export_type should map to a configured key within the settings.EXPORTS dictionary.

{
    "export_type" : "my_export"
} 

With optional parameters to be received by your previously created export task

{
    "export_type" : "my_export",
    "export_params" : {
        "length": 256
    }
}

Querying the task status

GET the same endpoint with a url parameter = to the export id field returned from the POST request. Or GET the same endpoint with no parameters to return a list of all exports.

Creating a custom view

If you don't wish to use the built in views and urls to trigger exports, create your own! To trigger an export yourself simply create an export object like so:

Export.objects.create(export_type="my_export")

The newly created export object will handle the django-db-queue job creation.

Overriding priority

By default all exports will be created with a priority of 1. This is passed through to django-db-queue. If you wish to override this you can do so via the POST method.

{
    "export_type" : "my_export",
    "priority" : 3
} 

Or through the Export creation directly.

Export.objects.create(export_type="my_export", priority=3)

Code of conduct

For guidelines regarding the code of conduct when contributing to this repository please review https://www.dabapps.com/open-source/code-of-conduct/

Release files for django-db-queue-exports 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-db-queue-exports 0.0.3
File Size Uploaded
django-db-queue-exports-0.0.3.tar.gz 6.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-db-queue-exports 0.0.3
File Interpreter ABI Platform
django_db_queue_exports-0.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 32.9 kB

Release files / django-db-queue-exports-0.0.3.tar.gz

Download URL django-db-queue-exports-0.0.3.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
ce96ae0e7de882e98ae28127abb6fb68c860f38d7460b58de61f8d4ebd7a3156
BLAKE2b-256 checksum
How to use checksums
3dd1fd1453303fccd3cacde47be69fbaa1bdf71e5678f4d60851a517f8253473
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.7.7

Release files / django_db_queue_exports-0.0.3-py3-none-any.whl

Download URL django_db_queue_exports-0.0.3-py3-none-any.whl
Size 26.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9717701b84460a84ef102045624fe2ec6b68ca551b9bdea42b50cb345e1af4e9
BLAKE2b-256 checksum
How to use checksums
6be803e4c10480b4b6be38e9b62bba3adc85203c36b78957783a3c21cccbf1c2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.7.7

Release history Release notifications | RSS feed

This release

0.0.3 This release

2 release files

0.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page