From 96dc435cf640a2fe163f1fb0eaef6b37f7aa08fe Mon Sep 17 00:00:00 2001 From: gyptazy Date: Sat, 24 May 2025 09:52:01 +0200 Subject: [PATCH] feature: Add relaod (SIGHUP) function to ProxLB to reload the configuration. Fixes: #189 --- .changelogs/1.1.3/189_add_reload_function.yml | 2 ++ proxlb/main.py | 13 ++++++++++++ proxlb/utils/helper.py | 20 +++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 .changelogs/1.1.3/189_add_reload_function.yml diff --git a/.changelogs/1.1.3/189_add_reload_function.yml b/.changelogs/1.1.3/189_add_reload_function.yml new file mode 100644 index 0000000..f71de13 --- /dev/null +++ b/.changelogs/1.1.3/189_add_reload_function.yml @@ -0,0 +1,2 @@ +feature: + - Add relaod (SIGHUP) function to ProxLB to reload the configuration (by @gyptazy). [#189] diff --git a/proxlb/main.py b/proxlb/main.py index 4d1b51a..6d5eb56 100644 --- a/proxlb/main.py +++ b/proxlb/main.py @@ -13,6 +13,7 @@ __license__ = "GPL-3.0" import logging +import signal from utils.logger import SystemdLogger from utils.cli_parser import CliParser from utils.config_parser import ConfigParser @@ -32,6 +33,9 @@ def main(): # Initialize logging handler logger = SystemdLogger(level=logging.INFO) + # Signal handler for SIGHUP + signal.signal(signal.SIGHUP, Helper.handler_sighup) + # Parses arguments passed from the CLI cli_parser = CliParser() cli_args = cli_parser.parse_args() @@ -51,6 +55,15 @@ def main(): proxlb_config["proxmox_api"]["pass"] = "********" while True: + + # Validate if reload signal was sent during runtime + # and reload the ProxLB configuration and adjust log level + if Helper.proxlb_reload: + logger.info("Reloading ProxLB configuration.") + proxlb_config = config_parser.get_config() + logger.set_log_level(proxlb_config.get('service', {}).get('log_level', 'INFO')) + Helper.proxlb_reload = False + # Get all required objects from the Proxmox cluster meta = {"meta": proxlb_config} nodes = Nodes.get_nodes(proxmox_api, proxlb_config) diff --git a/proxlb/utils/helper.py b/proxlb/utils/helper.py index 292385e..1ab6bbc 100644 --- a/proxlb/utils/helper.py +++ b/proxlb/utils/helper.py @@ -40,6 +40,8 @@ class Helper: get_daemon_mode(proxlb_config: Dict[str, Any]) -> None: Checks if the daemon mode is active and handles the scheduling accordingly. """ + proxlb_reload = False + def __init__(self): """ Initializes the general Helper clas. @@ -162,3 +164,21 @@ class Helper: print(json.dumps(filtered_data, indent=4)) logger.debug("Finished: print_json.") + + @staticmethod + def handler_sighup(signum, frame): + """ + Signal handler for SIGHUP. + + This method is triggered when the process receives a SIGHUP signal. + It sets the `proxlb_reload` class variable to True to indicate that + configuration should be reloaded in the main loop. + + Args: + signum (int): The signal number (expected to be signal.SIGHUP). + frame (frame object): Current stack frame (unused but required by signal handler signature). + """ + logger.debug("Starting: handle_sighup.") + logger.debug("Got SIGHUP signal. Reloading...") + Helper.proxlb_reload = True + logger.debug("Starting: handle_sighup.")