Inspector

> Updated the `check_container_health(container_name)` function. Included to check if container is running and any other exceptions. If it finds any of those the functions will return messages that are not "Healthy" which will trigger the  error count.

> Added comments to various functions
This commit is contained in:
Eugene Amos 2023-12-07 23:14:15 -08:00
parent e46230de0b
commit 07b323010e

View File

@ -24,6 +24,7 @@ if not os.path.exists(logs_folder):
def get_log_file_path(): def get_log_file_path():
return os.path.join(logs_folder, 'inspector_qbitt.log') return os.path.join(logs_folder, 'inspector_qbitt.log')
# Rotate the log files at 12am daily
def rotate_log_files(log_file): def rotate_log_files(log_file):
now = datetime.datetime.now() now = datetime.datetime.now()
timestamp = now.strftime('%Y-%m-%d') timestamp = now.strftime('%Y-%m-%d')
@ -35,17 +36,35 @@ def rotate_log_files(log_file):
os.rename(log_file, rotated_log_path) os.rename(log_file, rotated_log_path)
# Formating time
def format_remaining_time(remaining_time): def format_remaining_time(remaining_time):
minutes, seconds = divmod(remaining_time.seconds, 60) minutes, seconds = divmod(remaining_time.seconds, 60)
return f'{minutes} minutes {seconds} seconds' return f'{minutes} minutes {seconds} seconds'
# Checking the health of the container
def check_container_health(container_name): def check_container_health(container_name):
client = docker.from_env() client = docker.from_env()
container = client.containers.get(container_name) try:
return container.attrs['State']['Health']['Status'].upper() container = client.containers.get(container_name)
# Check if container is running
if container.status != 'running':
return 'NOT RUNNING'
health_data = container.attrs['State'].get('Health')
if health_data is None:
return 'NO HEALTH CHECK'
health_status = health_data.get('Status', 'UNKNOWN')
return health_status.upper()
except docker.errors.NotFound:
return 'CONTAINER NOT FOUND'
except Exception as e:
return f'ERROR: {str(e)}'
# Restaring the container
def restart_container(container_name): def restart_container(container_name):
client = docker.from_env() client = docker.from_env()
container = client.containers.get(container_name) container = client.containers.get(container_name)