Skip to content

Commit 654f17e

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 654f17e

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

queue_job/jobrunner/channels.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,15 @@ 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,
386+
name,
387+
parent,
388+
capacity=None,
389+
sequential=False,
390+
throttle=0,
391+
subcapacity=None,
392+
):
385393
self.name = name
386394
self.parent = parent
387395
if self.parent:
@@ -391,9 +399,10 @@ def __init__(self, name, parent, capacity=None, sequential=False, throttle=0):
391399
self._running = set()
392400
self._failed = set()
393401
self._pause_until = 0 # utc seconds since the epoch
394-
self.capacity = capacity
402+
self.capacity = capacity or (parent and parent.subcapacity)
395403
self.throttle = throttle # seconds
396404
self.sequential = sequential
405+
self.subcapacity = subcapacity
397406

398407
@property
399408
def sequential(self):
@@ -410,11 +419,13 @@ def configure(self, config):
410419
411420
* capacity
412421
* sequential
422+
* subcapacity
413423
* throttle
414424
"""
415425
assert self.fullname.endswith(config["name"])
416426
self.capacity = config.get("capacity", None)
417427
self.sequential = bool(config.get("sequential", False))
428+
self.subcapacity = config.get("subcapacity", None)
418429
self.throttle = int(config.get("throttle", 0))
419430
if self.sequential and self.capacity != 1:
420431
raise ValueError("A sequential channel must have a capacity of 1")
@@ -874,7 +885,16 @@ def parse_simple_config(cls, config_string):
874885
f"Invalid channel config {config_string}: "
875886
f"duplicate key {k}"
876887
)
877-
config[k] = v
888+
if k == "subcapacity":
889+
try:
890+
config[k] = int(v)
891+
except Exception as ex:
892+
raise ValueError(
893+
f"Invalid channel config {config_string}: "
894+
f"invalid subcapacity {v}"
895+
) from ex
896+
else:
897+
config[k] = v
878898
else:
879899
config["capacity"] = 1
880900
res.append(config)
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.assertIsNone(root.subcapacity)
22+
child = cm.get_channel_by_name("child", autocreate=True)
23+
self.assertIs(child.capacity, None)
24+
self.assertIsNone(child.subcapacity)
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.assertIsNone(child.subcapacity)
34+
child = cm.get_channel_by_name("child", autocreate=True)
35+
self.assertEqual(child.capacity, 1)
36+
self.assertIsNone(child.subcapacity)
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.assertIsNone(child.subcapacity)

0 commit comments

Comments
 (0)