From 5884d76ff41d73a2e6220b9cd84cf2037cdfac13 Mon Sep 17 00:00:00 2001 From: gyptazy Date: Fri, 18 Apr 2025 16:52:59 +0200 Subject: [PATCH] tecdebt: Adjust code style. --- proxlb/main.py | 2 +- proxlb/models/guests.py | 21 +++++++++++++-------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/proxlb/main.py b/proxlb/main.py index 5ec5868..4d1b51a 100644 --- a/proxlb/main.py +++ b/proxlb/main.py @@ -54,7 +54,7 @@ def main(): # Get all required objects from the Proxmox cluster meta = {"meta": proxlb_config} nodes = Nodes.get_nodes(proxmox_api, proxlb_config) - guests = Guests.get_guests(proxmox_api, nodes) + guests = Guests.get_guests(proxmox_api, nodes, meta) groups = Groups.get_groups(guests, nodes) # Merge obtained objects from the Proxmox cluster for further usage diff --git a/proxlb/models/guests.py b/proxlb/models/guests.py index 2e143b3..45601b9 100644 --- a/proxlb/models/guests.py +++ b/proxlb/models/guests.py @@ -35,7 +35,7 @@ class Guests: """ @staticmethod - def get_guests(proxmox_api: any, nodes: Dict[str, Any]) -> Dict[str, Any]: + def get_guests(proxmox_api: any, nodes: Dict[str, Any], meta: Dict[str, Any]) -> Dict[str, Any]: """ Get metrics of all guests in a Proxmox cluster. @@ -62,13 +62,18 @@ class Guests: # resource metrics for rebalancing to ensure that we do not overprovisiong the node. for guest in proxmox_api.nodes(node).qemu.get(): if guest['status'] == 'running': - retry_counter = 1 - while guest['cpu'] == 0 and retry_counter < 10: - guest = proxmox_api.nodes(node).qemu(guest['vmid']).status.current.get() - logger.debug(f"guest {guest['name']} is reporting { -guest['cpu']} cpu usage on retry {retry_counter}.") - time.sleep(1) - retry_counter += 1 + + # If the balancing method is set to cpu, we need to wait for the guest to report + # cpu usage. This is important for the balancing process to ensure that we do not + # wait for a guest for an infinite time. + if meta["meta"]["balancing"]["method"] == "cpu": + retry_counter = 0 + while guest['cpu'] == 0 and retry_counter < 10: + guest = proxmox_api.nodes(node).qemu(guest['vmid']).status.current.get() + logger.debug(f"Guest {guest['name']} (type VM) is reporting {guest['cpu']} cpu usage on retry {retry_counter}.") + retry_counter += 1 + time.sleep(1) + guests['guests'][guest['name']] = {} guests['guests'][guest['name']]['name'] = guest['name'] guests['guests'][guest['name']]['cpu_total'] = guest['cpus']