Refactored code

This commit is contained in:
Kevin Veen-Birkenbach 2023-12-15 08:27:41 +01:00
parent 84d2111af0
commit 618e10d94a

View File

@ -55,6 +55,24 @@ def check_any_service_active(services):
""" """
return any(check_service_active(service) for service in services) return any(check_service_active(service) for service in services)
def manage_timer(service, action):
"""
Manage a systemd timer for a service.
action can be 'start' or 'stop'.
"""
if action not in ['start', 'stop']:
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'}.")
def stop_timer(service): def stop_timer(service):
""" """
Stop and disable a systemd timer for a service if it exists. Stop and disable a systemd timer for a service if it exists.
@ -62,12 +80,9 @@ def stop_timer(service):
if service == f"{FREEZER_SERVICES_PREFIX}defrost": if service == f"{FREEZER_SERVICES_PREFIX}defrost":
print(f"Ignoring {service}. It's the initializer of freezer.") print(f"Ignoring {service}. It's the initializer of freezer.")
if service_file_exists(service, "timer"): if service_file_exists(service, "timer"):
timer_name = f"{service}.timer" manage_timer(service, 'stop')
subprocess.run(['systemctl', 'stop', timer_name])
subprocess.run(['systemctl', 'disable', timer_name])
print(f"{timer_name} stopped and disabled.")
else: else:
print("No timer to stop for service.") print(f"Timer {service}.timer does not exist.")
def filter_services(services, ignored_services): def filter_services(services, ignored_services):
""" """
@ -107,12 +122,6 @@ def freeze(filtered_services, timeout_sec):
attempt = wait_for_all_services_to_stop(filtered_services, max_attempts, attempt) attempt = wait_for_all_services_to_stop(filtered_services, max_attempts, attempt)
print("All required services have stopped.") print("All required services have stopped.")
def start_timer(service):
timer_name = f"{service}.timer"
subprocess.run(['systemctl', 'start', timer_name])
subprocess.run(['systemctl', 'enable', timer_name])
print(f"{timer_name} started and enabled.")
def get_max_attempts(timeout_sec): def get_max_attempts(timeout_sec):
return timeout_sec // BREAK_TIME_SECONDS return timeout_sec // BREAK_TIME_SECONDS
@ -120,7 +129,7 @@ def defrost(filtered_services,timeout_sec):
""" """
Defrost services by starting and enabling their timers. Defrost services by starting and enabling their timers.
""" """
running_service = f"{FREEZER_SERVICES_PREFIX}{action}" running_service = f"{FREEZER_SERVICES_PREFIX}defrost"
attempt = 0 attempt = 0
max_attempts = get_max_attempts(timeout_sec) max_attempts = get_max_attempts(timeout_sec)
try: try:
@ -128,13 +137,13 @@ def defrost(filtered_services,timeout_sec):
except AttemptException as e: except AttemptException as e:
print(e) print(e)
print("Defrosting was not possible. The execution of other services took to long.") print("Defrosting was not possible. The execution of other services took to long.")
start_timer(running_service) manage_timer(running_service, "stop")
exit(0) exit(0)
for service in filtered_services + [running_service]: for service in filtered_services + [running_service]:
print(f"Unfreezing: {service}") print(f"Unfreezing: {service}")
if service_file_exists(service, "timer"): if service_file_exists(service, "timer"):
start_timer(service) manage_timer(service, "start")
else: else:
print("No timer to activate for service.") print("No timer to activate for service.")
print("All required services are started.") print("All required services are started.")