File: task_queue.rst

package info (click to toggle)
tryton-server 7.0.43-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,760 kB
  • sloc: python: 53,744; xml: 5,194; sh: 803; sql: 217; makefile: 28
file content (71 lines) | stat: -rw-r--r-- 2,229 bytes parent folder | download | duplicates (2)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
.. _topics-task-queue:

==========
Task Queue
==========

Tryton provides a way to run asynchronously some tasks. You must activate the
worker in the ``queue`` section of the :ref:`configuration
<topics-configuration>` and :ref:`run the worker manager
<topics-start-server>` otherwise the tasks will be run at the end of the
transaction.

A task is the parameters that defines how to call a method from a
:class:`~trytond.model.Model`.
This include the :attr:`~trytond.transaction.Transaction.context`, the
:attr:`~trytond.transaction.Transaction.user` and the arguments.
The first argument of the method must be an instance or a list of instances of
:class:`~trytond.model.Model`.
This other arguments must be JSON-ifiable.

A task is pushed into the ``queue`` by calling the desired method on the
:attr:`~trytond.model.Model.__queue__`.
This stores in the queue all the current parameters of the call and it will be
execute by a worker or at the end of the transaction if no worker is
configured.
The following :attr:`~trytond.transaction.Transaction.context` keys are used as
parameters for the queue:

``queue_name``
   The name of the queue.
   Default value is ``default``.

``queue_scheduled_at``
   A ``datetime.timedelta`` to add to current time to define when the task
   should be started.
   Default value is ``None`` which means directly.

``queue_expected_at``
   A ``datetime.timedelta`` to add to current time to define when the task
   should be finished.
   Default value is ``None`` which means as soon as possible.

``queue_batch``
   An ``integer`` to divide the instances by batch of this size.
   If the value is ``true`` then the size is the value defined by the
   configuration ``queue`` of ``batch_size``.
   Default is ``None`` which means no division.

.. warning::

    There is no access right verification during the execution of the task.

Example:

.. highlight:: python

::

    from trytond.model import Model

    class MyModel(Model):
        "My Model"
        __name__ = 'my_model'

        @classmethod
        def launch(cls, records):
            for record in records:
                cls.__queue__.process(record, 42)

        def process(self, value):
            self.value = value