diff options
Diffstat (limited to 'python')
| -rw-r--r-- | python/vyos/utils/misc.py | 29 | ||||
| -rw-r--r-- | python/vyos/wanloadbalance.py | 2 |
2 files changed, 30 insertions, 1 deletions
diff --git a/python/vyos/utils/misc.py b/python/vyos/utils/misc.py index c7f7e7343..20e3de746 100644 --- a/python/vyos/utils/misc.py +++ b/python/vyos/utils/misc.py @@ -12,6 +12,9 @@ # # You should have received a copy of the GNU Lesser General Public # License along with this library. If not, see <http://www.gnu.org/licenses/>. +import time + +from typing import Callable, Any def begin(*args): """ @@ -64,3 +67,29 @@ def install_into_config(conf, config_paths, override_prompt=True): if count > 0: print(f'{count} value(s) installed. Use "compare" to see the pending changes, and "commit" to apply.') + +def wait_for( + func: Callable[..., Any], + *args, + interval: float = 1.0, + timeout: float = 5.0, + **kwargs +) -> bool: + """ + Repeatedly calls `func()` until it returns True or the timeout expires. + + Args: + func: A function with no arguments that returns a truthy value when ready. + interval: Seconds to wait between calls (default: 1.0). + timeout: Maximum time to wait in seconds (default: 5.0). + + Returns: + True if the function returned True within the timeout, otherwise False. + """ + start = time.monotonic() + while True: + if func(*args, **kwargs): + return True + if (time.monotonic() - start) >= timeout: + return False + time.sleep(interval) diff --git a/python/vyos/wanloadbalance.py b/python/vyos/wanloadbalance.py index afe005731..05ae6b536 100644 --- a/python/vyos/wanloadbalance.py +++ b/python/vyos/wanloadbalance.py @@ -154,7 +154,7 @@ def wlb_weight_interfaces(rule_conf, health_state): for ifname, weight in sorted(interfaces, key=lambda i: i[1]): # build weight ranges end = start + weight - 1 out.append((ifname, f'{start}-{end}' if end > start else start)) - start = weight + start += weight return out, total_weight |
