changeset e436b206f484 in trytond:default
details: https://hg.tryton.org/trytond?cmd=changeset&node=e436b206f484
description:
        Add batch option to push queue tasks

        issue11129
        review381661002
diffstat:

 CHANGELOG                    |   1 +
 doc/topics/configuration.rst |   7 +++++++
 doc/topics/task_queue.rst    |   5 +++++
 trytond/ir/queue.py          |  43 +++++++++++++++++++++++++++++++------------
 4 files changed, 44 insertions(+), 12 deletions(-)

diffs (104 lines):

diff -r 9941357a9b7c -r e436b206f484 CHANGELOG
--- a/CHANGELOG Mon Mar 07 00:52:58 2022 +0100
+++ b/CHANGELOG Wed Mar 09 00:57:17 2022 +0100
@@ -1,3 +1,4 @@
+* Add batch option to push queue tasks
 * Support ref value for reference field
 * Enforce ref model in XML data
 * Do not resolve entities by default with lxml (issue11219)
diff -r 9941357a9b7c -r e436b206f484 doc/topics/configuration.rst
--- a/doc/topics/configuration.rst      Mon Mar 07 00:52:58 2022 +0100
+++ b/doc/topics/configuration.rst      Wed Mar 09 00:57:17 2022 +0100
@@ -311,6 +311,13 @@
 
 Default: ``30``
 
+batch_size
+~~~~~~~~~~
+
+The default number of the instances to process in a batch.
+
+Default: ``20``
+
 error
 -----
 
diff -r 9941357a9b7c -r e436b206f484 doc/topics/task_queue.rst
--- a/doc/topics/task_queue.rst Mon Mar 07 00:52:58 2022 +0100
+++ b/doc/topics/task_queue.rst Wed Mar 09 00:57:17 2022 +0100
@@ -34,6 +34,11 @@
       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.
diff -r 9941357a9b7c -r e436b206f484 trytond/ir/queue.py
--- a/trytond/ir/queue.py       Mon Mar 07 00:52:58 2022 +0100
+++ b/trytond/ir/queue.py       Wed Mar 09 00:57:17 2022 +0100
@@ -14,6 +14,7 @@
 
 has_worker = config.getboolean('queue', 'worker', default=False)
 clean_days = config.getint('queue', 'clean_days', default=30)
+batch_size = config.getint('queue', 'batch_size', default=20)
 
 
 class Queue(ModelSQL):
@@ -221,6 +222,7 @@
         if scheduled_at is not None:
             scheduled_at = now + scheduled_at
         expected_at = context.pop('queue_expected_at', None)
+        queue_batch = context.pop('queue_batch', None)
         context.pop('_check_access', None)
         context.pop('language', None)
         if expected_at is not None:
@@ -229,15 +231,32 @@
             instances = list(map(int, instances))
         except TypeError:
             instances = int(instances)
-        data = {
-            'model': self.__model.__name__,
-            'method': self.__name,
-            'user': transaction.user,
-            'context': context,
-            'instances': instances,
-            'args': args,
-            'kwargs': kwargs,
-            }
-        return self.__queue.push(
-            name, data,
-            scheduled_at=scheduled_at, expected_at=expected_at)
+
+        def _push(instances):
+            data = {
+                'model': self.__model.__name__,
+                'method': self.__name,
+                'user': transaction.user,
+                'context': context,
+                'instances': instances,
+                'args': args,
+                'kwargs': kwargs,
+                }
+            return self.__queue.push(
+                name, data,
+                scheduled_at=scheduled_at, expected_at=expected_at)
+
+        if isinstance(instances, list):
+            if has_worker and queue_batch:
+                if isinstance(queue_batch, int):
+                    count = queue_batch
+                else:
+                    count = batch_size
+            else:
+                count = len(instances)
+            task_ids = []
+            for sub_instances in grouped_slice(instances, count=count):
+                task_ids.append(_push(list(sub_instances)))
+            return task_ids
+        else:
+            return _push(instances)

Reply via email to