|
| 1 | +# Copyright (c) 2015-2016 ACSONE SA/NV (<http://acsone.eu>) |
| 2 | +# Copyright 2015-2016 Camptocamp SA |
| 3 | +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html) |
| 4 | + |
| 5 | +from odoo import _ |
| 6 | + |
| 7 | +from ....queue_job.jobrunner.channels import Channel, ChannelManager, split_strip |
| 8 | +from ..job import PAUSE_CHANNEL |
| 9 | + |
| 10 | + |
| 11 | +class ChannelPause(Channel): |
| 12 | + def __str__(self): |
| 13 | + default_capacity = "0" if self.name == PAUSE_CHANNEL else "∞" |
| 14 | + capacity = default_capacity if not self.capacity else str(self.capacity) |
| 15 | + return "%s(C:%s,Q:%d,R:%d,F:%d)" % ( |
| 16 | + self.fullname, |
| 17 | + capacity, |
| 18 | + len(self._queue), |
| 19 | + len(self._running), |
| 20 | + len(self._failed), |
| 21 | + ) |
| 22 | + |
| 23 | + def has_capacity(self): |
| 24 | + """This method has been copied entirely from the parent class.""" |
| 25 | + if self.sequential and self._failed: |
| 26 | + # a sequential queue blocks on failed jobs |
| 27 | + return False |
| 28 | + # MODIFY: the original logic was: `if not self.capacity:` |
| 29 | + if not self.capacity and self.fullname != PAUSE_CHANNEL: |
| 30 | + # unlimited capacity |
| 31 | + return True |
| 32 | + return len(self._running) < self.capacity |
| 33 | + |
| 34 | + |
| 35 | +class ChannelManagerPause(ChannelManager): |
| 36 | + @classmethod |
| 37 | + def parse_simple_config(cls, config_string): |
| 38 | + """This method has been copied entirely from the parent class.""" |
| 39 | + res = [] |
| 40 | + config_string = config_string.replace("\n", ",") |
| 41 | + for channel_config_string in split_strip(config_string, ","): |
| 42 | + if not channel_config_string: |
| 43 | + # ignore empty entries (commented lines, trailing commas) |
| 44 | + continue |
| 45 | + config = {} |
| 46 | + config_items = split_strip(channel_config_string, ":") |
| 47 | + name = config_items[0] |
| 48 | + if not name: |
| 49 | + raise ValueError( |
| 50 | + _("Invalid channel config %s: missing channel name", config_string) |
| 51 | + ) |
| 52 | + config["name"] = name |
| 53 | + if len(config_items) > 1: |
| 54 | + capacity = config_items[1] |
| 55 | + try: |
| 56 | + config["capacity"] = int(capacity) |
| 57 | + # MODIFY: Add the `if` logic. |
| 58 | + if name == PAUSE_CHANNEL and config["capacity"] != 0: |
| 59 | + raise Exception( |
| 60 | + _("Channel 'pause' must be capacity equal to zero") |
| 61 | + ) |
| 62 | + except Exception as ex: |
| 63 | + raise ValueError( |
| 64 | + _( |
| 65 | + f"Invalid channel config {config_string}: " |
| 66 | + f"invalid capacity {capacity}" |
| 67 | + ) |
| 68 | + ) from ex |
| 69 | + for config_item in config_items[2:]: |
| 70 | + kv = split_strip(config_item, "=") |
| 71 | + if len(kv) == 1: |
| 72 | + k, v = kv[0], True |
| 73 | + elif len(kv) == 2: |
| 74 | + k, v = kv |
| 75 | + else: |
| 76 | + raise ValueError( |
| 77 | + _( |
| 78 | + f"Invalid channel config {config_string}: " |
| 79 | + f"incorrect config item {config_item}", |
| 80 | + ) |
| 81 | + ) |
| 82 | + if k in config: |
| 83 | + raise ValueError( |
| 84 | + _( |
| 85 | + f"Invalid channel config {config_string}: " |
| 86 | + f"duplicate key {k}", |
| 87 | + ) |
| 88 | + ) |
| 89 | + config[k] = v |
| 90 | + else: |
| 91 | + # MODIFY: the original logic was `config["capacity"] = 1` |
| 92 | + config["capacity"] = 0 if name == PAUSE_CHANNEL else 1 |
| 93 | + res.append(config) |
| 94 | + return res |
0 commit comments