|
| 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 ..job import (PAUSE_CHANNEL) |
| 6 | +from ....queue_job.jobrunner.channels import Channel, ChannelManager |
| 7 | + |
| 8 | + |
| 9 | +class ChannelPause(Channel): |
| 10 | + |
| 11 | + def __str__(self): |
| 12 | + default_capacity = "0" if self.name == PAUSE_CHANNEL else "∞" |
| 13 | + capacity = default_capacity if not self.capacity else str(self.capacity) |
| 14 | + return "%s(C:%s,Q:%d,R:%d,F:%d)" % ( |
| 15 | + self.fullname, |
| 16 | + capacity, |
| 17 | + len(self._queue), |
| 18 | + len(self._running), |
| 19 | + len(self._failed), |
| 20 | + ) |
| 21 | + |
| 22 | + def has_capacity(self): |
| 23 | + """This method has been copied entirely from the parent class 'Channel'. |
| 24 | + """ |
| 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 | +class ChannelManagerPause(ChannelManager): |
| 35 | + |
| 36 | + @classmethod |
| 37 | + def parse_simple_config(cls, config_string): |
| 38 | + """This method has been copied entirely from the parent class 'ChannelManager'. |
| 39 | + """ |
| 40 | + res = [] |
| 41 | + config_string = config_string.replace("\n", ",") |
| 42 | + for channel_config_string in split_strip(config_string, ","): |
| 43 | + if not channel_config_string: |
| 44 | + # ignore empty entries (commented lines, trailing commas) |
| 45 | + continue |
| 46 | + config = {} |
| 47 | + config_items = split_strip(channel_config_string, ":") |
| 48 | + name = config_items[0] |
| 49 | + if not name: |
| 50 | + raise ValueError( |
| 51 | + "Invalid channel config %s: missing channel name" % config_string |
| 52 | + ) |
| 53 | + config["name"] = name |
| 54 | + if len(config_items) > 1: |
| 55 | + capacity = config_items[1] |
| 56 | + try: |
| 57 | + config["capacity"] = int(capacity) |
| 58 | + # MODIFY: Add the `if` logic. |
| 59 | + if name == PAUSE_CHANNEL and config["capacity"] != 0: |
| 60 | + raise Exception( |
| 61 | + "Channel 'pause' must be capacity equal to zero" |
| 62 | + ) |
| 63 | + except Exception as ex: |
| 64 | + raise ValueError( |
| 65 | + "Invalid channel config %s: " |
| 66 | + "invalid capacity %s" % (config_string, capacity) |
| 67 | + ) from ex |
| 68 | + for config_item in config_items[2:]: |
| 69 | + kv = split_strip(config_item, "=") |
| 70 | + if len(kv) == 1: |
| 71 | + k, v = kv[0], True |
| 72 | + elif len(kv) == 2: |
| 73 | + k, v = kv |
| 74 | + else: |
| 75 | + raise ValueError( |
| 76 | + "Invalid channel config %s: " |
| 77 | + "incorrect config item %s" % (config_string, config_item) |
| 78 | + ) |
| 79 | + if k in config: |
| 80 | + raise ValueError( |
| 81 | + "Invalid channel config %s: " |
| 82 | + "duplicate key %s" % (config_string, k) |
| 83 | + ) |
| 84 | + config[k] = v |
| 85 | + else: |
| 86 | + # MODIFY: the original logic was `config["capacity"] = 1` |
| 87 | + config["capacity"] = 0 if name == PAUSE_CHANNEL else 1 |
| 88 | + res.append(config) |
| 89 | + return res |
0 commit comments