Skip to main content

A middleware for Dramatiq (for Django) that keeps track of task state only when you need it to.

Project description

dramatiq-taskstate

A middleware for Dramatiq (for Django) that keeps track of task state only when you need it to.

Quickstart

  1. Install dramatiq-taskstate via pip:

    pip install dramatiq-taskstate
    

    Or via the repo:

    pip install git+https://github.com/armandtvz/dramatiq-taskstate.git
    
  2. Add taskstate to your INSTALLED_APPS in your project settings.py file:

    INSTALLED_APPS = [
        'django_dramatiq',
        '...',
        '...',
        'taskstate',
    ]
    
  3. Run migrate:

    python manage.py migrate
    
  4. Include the middleware in your django_dramatiq configuration:

    DRAMATIQ_BROKER = {
        'BROKER': '...',
        'OPTIONS': {
            # ...
        },
        'MIDDLEWARE': [
            # ...
            'taskstate.middleware.StateMiddleware',
        ]
    }
    
  5. Add a for_state parameter to Dramatiq actors that need task state to be tracked. The middleware will ignore any tasks that don't have this argument. Also remember that all values in the for_state dictionary must be JSON serializable.

    @dramatiq.actor
    def my_actor(arg, for_state={}):
        pass
    
  6. Then, when sending a new task to Dramatiq: the for_state dictionary can contain any of the following keys:

    'for_state': {
        'user_pk': user.pk,
        'model_name': 'model',
        'app_name': 'app',
        'description': 'description',
    }
    
  7. Each time the Task object is updated a task_changed signal is dispatched which can be handled like this:

    from django.dispatch import receiver
    
    from taskstate.middleware import StateMiddleware
    from taskstate.signals import task_changed
    
    @receiver(task_changed, sender=StateMiddleware)
    def handle_task_changed(sender, task, **kwargs):
        pass
    

Management commands

The clear_tasks management command will delete all Task objects currently in the database irrespective of status.

python manage.py clear_tasks

Compatibility

  • Compatible with Python 3.8 and above.
  • Compatible with Django 3.2 and above.

Versioning

This project follows semantic versioning (SemVer).

License and code of conduct

Check the root of the repo for these files.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dramatiq-taskstate-0.1.tar.gz (17.7 kB view hashes)

Uploaded Source

Built Distribution

dramatiq_taskstate-0.1-py3-none-any.whl (19.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page