File: injecting_metadata.rst

package info (click to toggle)
python-django-celery-results 2.5.1-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 652 kB
  • sloc: python: 2,185; makefile: 312; sh: 7; sql: 2
file content (42 lines) | stat: -rw-r--r-- 2,020 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
Injecting metadata
===============


To save arbitrary data on the field TaskResult.meta, the Celery Task Request must be manipulated as such:

    .. code-block:: python

        from celery import Celery

        app = Celery('hello', broker='amqp://guest@localhost//')

        @app.task(bind=True)
        def hello(task_instance):
            task_instance.request.meta = {'some_key': 'some_value'}
            task_instance.update_state(
                state='PROGRESS',
                meta='Task current result'
            )
            # If TaskResult is queried from DB at this momento it will yield
            # TaskResult(
            #     result='Task current result',
            #     meta={'some_key': 'some_value'}  # some discrepancies apply as I didn't document the json parse and children data
            # )
            return 'hello world'

        # After task is completed, if TaskResult is queried from DB at this momento it will yield
        # TaskResult(
        #     result='hello world',
        #     meta={'some_key': 'some_value'}  # some discrepancies apply as I didn't document the json parse and children data
        # )

This way, the value of ``task_instance.request.meta`` will be stored on ``TaskResult.meta``.

Note that the `meta` arg in the method `update_state` is not really a metadata and it's not stored on ``TaskResult.meta``.
This arg is used to save the CURRENT result of the task. So it's stored on ``TaskResult.result``.

It works this way because while a task is executing, the `TaskResult` is used really as current task state; holding information, temporarily, until the task completes.
Subsequent calls to `update_state` will update the same `TaskResult`, overwriting what was there previously.
Upon completion of the task, the results of the task are stored in the same TaskResult, overwriting the previous state of the task.
So the return from the function is stored in ``TaskResult.result`` and ``TaskResult.status`` is set to 'SUCCESS' (or 'FAILURE').