зеркало из
1
0
Форкнуть 0
A celery task class whose execution is delayed until after the request finishes, using request_started and request_finished signals from django.
Перейти к файлу
Mathieu Pillard 4f1e33ec77
Merge pull request #30 from mozilla/update-supported-versions
Update supported Python/Django versions, use modern CircleCI images
2022-03-10 14:29:24 +01:00
.circleci Keep more explicit django version names 2022-03-10 14:27:24 +01:00
post_request_task Replace task decorator with shared_task 2021-01-29 11:17:15 +07:00
.gitignore Initial commit 2016-01-25 12:26:32 +01:00
AUTHORS Initial import for v0.0.1. 2016-01-25 14:39:34 +01:00
CODE_OF_CONDUCT.md Add Mozilla Code of Conduct file 2019-03-29 10:09:38 -07:00
LICENSE Initial commit 2016-01-25 12:26:32 +01:00
MANIFEST.in Initial import for v0.0.1. 2016-01-25 14:39:34 +01:00
Makefile Update Makefile, readme and travis config to reflect new testing environment and compat list. 2016-10-11 19:26:29 +02:00
README.rst Add circle ci badge 2021-07-23 16:29:35 +02:00
runtests.py Miscellaneous cleanups and test improvments 2016-01-28 18:04:06 +01:00
setup.py Stop supporting celery 4 2022-03-10 13:15:40 +01:00
tox.ini Just in case, make sure we really install Django 3.2.x when we mean to 2022-03-10 14:28:05 +01:00

README.rst

django-post-request-task
========================

.. image:: https://circleci.com/gh/mozilla/django-post-request-task.svg?style=svg
    :target: https://circleci.com/gh/mozilla/django-post-request-task

A celery task class whose execution is delayed until after the request
finishes, using ``request_started`` and ``request_finished`` signals from django
and thread locals.

This is useful if your views are wrapped in transactions (as they should if
you're making database modifications in them), as you can end up triggering a
celery task too soon before the transaction has been committed (or even trigger
a task when the corresponding transaction has been rolled back).

By listening to the `request_started` and `request_finished` django signals, we
can safely trigger a task after all transactions created from ``@atomic`` or
``ATOMIC_REQUESTS`` have been committed.

Usage
-----

.. code-block:: python

    from celery import Celery
    from post_request_task.task import PostRequestTask


    app = Celery('myapp', task_cls=PostRequestTask)

    @app.task
    def my_task():
        # If .delay() is called on this task inside a django request-response
        # cycle it will be called once the request is finished, and not before.
        pass


Or, if you are using the decorator directly:

.. code-block:: python

    from post_request_task.task import shared_task

    @shared_task
    def my_task():
        pass


That's it. If the task is called from outside the django request-response
cycle, then it will be triggered normally.

As a bonus feature, if the same task is called with the same argument several
times during a request-response cycle, it will only be queued up once.


Running tests
-------------

.. code-block:: sh

    $ make testenv
    $ make test

By default, tests are run with whatever django version is installed. If you want to run tests for other versions
use tox:


.. code-block:: sh

    $ make testenv
    $ tox -e 3.7-2.0.x # or any other environment defined in our tox.ini