Skip to content

Commit 3b234e1

Browse files
committed
[IMP] queue_job: Configure default subchannel capacity.
This adds a new `subcapacity` option to channels that allows the configuration of the default capacity for autocreated child channels. For example, environment `ODOO_QUEUE_JOB_CHANNELS=root:8:subcapacity=1` would set the capacity of an autocreated `root.sub` channel to 1.
1 parent 6f88b31 commit 3b234e1

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

queue_job/jobrunner/channels.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,9 @@ class Channel:
381381
without risking to overflow the system.
382382
"""
383383

384-
def __init__(self, name, parent, capacity=None, sequential=False, throttle=0):
384+
def __init__(
385+
self, name, parent, capacity=None, sequential=False, throttle=0, subcapacity=0
386+
):
385387
self.name = name
386388
self.parent = parent
387389
if self.parent:
@@ -391,9 +393,10 @@ def __init__(self, name, parent, capacity=None, sequential=False, throttle=0):
391393
self._running = set()
392394
self._failed = set()
393395
self._pause_until = 0 # utc seconds since the epoch
394-
self.capacity = capacity
396+
self.capacity = capacity or (parent and parent.subcapacity) or None
395397
self.throttle = throttle # seconds
396398
self.sequential = sequential
399+
self.subcapacity = subcapacity
397400

398401
@property
399402
def sequential(self):
@@ -410,11 +413,13 @@ def configure(self, config):
410413
411414
* capacity
412415
* sequential
416+
* subcapacity
413417
* throttle
414418
"""
415419
assert self.fullname.endswith(config["name"])
416420
self.capacity = config.get("capacity", None)
417421
self.sequential = bool(config.get("sequential", False))
422+
self.subcapacity = int(config.get("subcapacity", 0))
418423
self.throttle = int(config.get("throttle", 0))
419424
if self.sequential and self.capacity != 1:
420425
raise ValueError("A sequential channel must have a capacity of 1")
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,43 @@
11
# Copyright 2015-2016 Camptocamp SA
22
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html)
33

4+
from odoo.tests import BaseCase
5+
46
# pylint: disable=odoo-addons-relative-import
57
# we are testing, we want to test as we were an external consumer of the API
68
from odoo.addons.queue_job.jobrunner import channels
79

810
from .common import load_doctests
911

1012
load_tests = load_doctests(channels)
13+
14+
15+
class TestChannelManager(BaseCase):
16+
def test_subcapacity_default(self):
17+
cm = channels.ChannelManager()
18+
cm.simple_configure("root:4")
19+
root = cm.get_channel_by_name("root")
20+
self.assertEqual(root.capacity, 4)
21+
self.assertEqual(root.subcapacity, 0)
22+
child = cm.get_channel_by_name("child", autocreate=True)
23+
self.assertIs(child.capacity, None)
24+
self.assertEqual(child.subcapacity, 0)
25+
26+
def test_subcapacity(self):
27+
cm = channels.ChannelManager()
28+
cm.simple_configure("root:4:subcapacity=1,override:2")
29+
root = cm.get_channel_by_name("root")
30+
self.assertEqual(root.subcapacity, 1)
31+
child = cm.get_channel_by_name("override")
32+
self.assertEqual(child.capacity, 2)
33+
self.assertEqual(child.subcapacity, 0)
34+
child = cm.get_channel_by_name("child", autocreate=True)
35+
self.assertEqual(child.capacity, 1)
36+
self.assertEqual(child.subcapacity, 0)
37+
38+
def test_subcapacity_subchannel(self):
39+
cm = channels.ChannelManager()
40+
cm.simple_configure("root:4,sub:2:subcapacity=1")
41+
child = cm.get_channel_by_name("sub.child", autocreate=True)
42+
self.assertEqual(child.capacity, 1)
43+
self.assertEqual(child.subcapacity, 0)

0 commit comments

Comments
 (0)