From cc560770ce7154d026e665dbacac0f41bf4f55f5 Mon Sep 17 00:00:00 2001 From: Sven Sager Date: Tue, 22 Apr 2025 13:42:33 +0200 Subject: [PATCH] feat(revpiconfig): Make unit config changes asynchronous Refactored unit enable/disable actions to run in separate threads, ensuring non-blocking operations. This enhances performance and avoids potential delays during systemd operations. --- .../system_config/revpi_config.py | 37 ++++++++++++------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/src/revpi_middleware/dbus_middleware1/system_config/revpi_config.py b/src/revpi_middleware/dbus_middleware1/system_config/revpi_config.py index 45d2d26..ec01aef 100644 --- a/src/revpi_middleware/dbus_middleware1/system_config/revpi_config.py +++ b/src/revpi_middleware/dbus_middleware1/system_config/revpi_config.py @@ -10,6 +10,7 @@ from glob import glob from logging import getLogger from os import X_OK, access from os.path import exists, join +from threading import Thread from typing import List, Optional from pydbus import SystemBus @@ -665,29 +666,39 @@ def simple_systemd(action: ConfigActions, unit: str): systemd_manager = systemd["org.freedesktop.systemd1.Manager"] if action is ConfigActions.ENABLE: - # Dbus call: UnmaskUnitFiles(in as files, in b runtime, out a(sss) changes - lst_change_unmask = systemd_manager.UnmaskUnitFiles([unit], False) - # Dbus call: EnableUnitFiles(in as files, in b runtime, in b force, - # out b carries_install_info, out a(sss) changes - lst_change_enable = systemd_manager.EnableUnitFiles([unit], False, False) - if lst_change_unmask or lst_change_enable: - # Reload systemd after modified unit property - systemd_manager.Reload() + def thread_unit_config(): + """Change configuration asynchronously.""" + # Dbus call: UnmaskUnitFiles(in as files, in b runtime, out a(sss) changes + lst_change_unmask = systemd_manager.UnmaskUnitFiles([unit], False) + + # Dbus call: EnableUnitFiles(in as files, in b runtime, in b force, + # out b carries_install_info, out a(sss) changes + lst_change_enable = systemd_manager.EnableUnitFiles([unit], False, False) + if lst_change_unmask or lst_change_enable: + # Reload systemd after modified unit property + systemd_manager.Reload() + + Thread(target=thread_unit_config, daemon=True).start() # Dbus call: StartUnit(in s name, in s mode, out o job systemd_manager.StartUnit(unit, "replace") elif action is ConfigActions.DISABLE: + def thread_unit_config(): + """Change configuration asynchronously.""" + # Dbus call: DisableUnitFiles (in as files, in b runtime, out a(sss) changes) + change = systemd_manager.DisableUnitFiles([unit], False) + if change: + # Reload systemd after modified unit property + systemd_manager.Reload() + + Thread(target=thread_unit_config, daemon=True).start() + # Dbus call: StopUnit(in s name,in s mode, out o job systemd_manager.StopUnit(unit, "replace") - # Dbus call: DisableUnitFiles (in as files, in b runtime, out a(sss) changes) - change = systemd_manager.DisableUnitFiles([unit], False) - if change: - # Reload systemd after modified unit property - systemd_manager.Reload() elif action is ConfigActions.STATUS: try: