From 2b5b693c2ea232bfd362bcb8bdd8b52b56bae1ee Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Fri, 15 Dec 2023 08:35:24 +0100 Subject: [PATCH] Refactored timer manager --- .../system-maintenance-service-freezer.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/roles/system-maintenance-service-freezer/files/system-maintenance-service-freezer.py b/roles/system-maintenance-service-freezer/files/system-maintenance-service-freezer.py index 2be74c10..f3efc2d9 100644 --- a/roles/system-maintenance-service-freezer/files/system-maintenance-service-freezer.py +++ b/roles/system-maintenance-service-freezer/files/system-maintenance-service-freezer.py @@ -64,14 +64,16 @@ def manage_timer(service, action): raise ValueError("Invalid action specified for manage_timer") timer_name = f"{service}.timer" - subprocess.run(['systemctl', action, timer_name]) - if action == 'start': - subprocess.run(['systemctl', 'enable', timer_name]) - elif action == 'stop': - subprocess.run(['systemctl', 'disable', timer_name]) - - print(f"{timer_name} {action}ed and {'enabled' if action == 'start' else 'disabled'}.") - + try: + subprocess.run(['systemctl', action, timer_name], check=True) + if action == 'start': + subprocess.run(['systemctl', 'enable', timer_name], check=True) + elif action == 'stop': + subprocess.run(['systemctl', 'disable', timer_name], check=True) + print(f"{timer_name} {action}ed and {'enabled' if action == 'start' else 'disabled'}.") + except subprocess.CalledProcessError as e: + print(f"Error managing timer {timer_name}: {e}") + exit(1) def stop_timer(service): """