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.
This commit is contained in:
2025-04-22 13:42:33 +02:00
parent 4df903783c
commit cc560770ce

View File

@@ -10,6 +10,7 @@ from glob import glob
from logging import getLogger from logging import getLogger
from os import X_OK, access from os import X_OK, access
from os.path import exists, join from os.path import exists, join
from threading import Thread
from typing import List, Optional from typing import List, Optional
from pydbus import SystemBus from pydbus import SystemBus
@@ -665,6 +666,9 @@ def simple_systemd(action: ConfigActions, unit: str):
systemd_manager = systemd["org.freedesktop.systemd1.Manager"] systemd_manager = systemd["org.freedesktop.systemd1.Manager"]
if action is ConfigActions.ENABLE: if action is ConfigActions.ENABLE:
def thread_unit_config():
"""Change configuration asynchronously."""
# Dbus call: UnmaskUnitFiles(in as files, in b runtime, out a(sss) changes # Dbus call: UnmaskUnitFiles(in as files, in b runtime, out a(sss) changes
lst_change_unmask = systemd_manager.UnmaskUnitFiles([unit], False) lst_change_unmask = systemd_manager.UnmaskUnitFiles([unit], False)
@@ -675,20 +679,27 @@ def simple_systemd(action: ConfigActions, unit: str):
# Reload systemd after modified unit property # Reload systemd after modified unit property
systemd_manager.Reload() systemd_manager.Reload()
Thread(target=thread_unit_config, daemon=True).start()
# Dbus call: StartUnit(in s name, in s mode, out o job # Dbus call: StartUnit(in s name, in s mode, out o job
systemd_manager.StartUnit(unit, "replace") systemd_manager.StartUnit(unit, "replace")
elif action is ConfigActions.DISABLE: elif action is ConfigActions.DISABLE:
# Dbus call: StopUnit(in s name,in s mode, out o job def thread_unit_config():
systemd_manager.StopUnit(unit, "replace") """Change configuration asynchronously."""
# Dbus call: DisableUnitFiles (in as files, in b runtime, out a(sss) changes) # Dbus call: DisableUnitFiles (in as files, in b runtime, out a(sss) changes)
change = systemd_manager.DisableUnitFiles([unit], False) change = systemd_manager.DisableUnitFiles([unit], False)
if change: if change:
# Reload systemd after modified unit property # Reload systemd after modified unit property
systemd_manager.Reload() 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")
elif action is ConfigActions.STATUS: elif action is ConfigActions.STATUS:
try: try:
unit_path = systemd_manager.LoadUnit(unit) unit_path = systemd_manager.LoadUnit(unit)