Skip to content

Commit

Permalink
improved health checks
Browse files Browse the repository at this point in the history
  • Loading branch information
sholdee committed Jun 28, 2024
1 parent 65a3abc commit bcbdcd3
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions adguard_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,19 +150,19 @@ def __init__(self, log_file_path, metrics_collector):
self.log_file_path = log_file_path
self.metrics_collector = metrics_collector
self.last_position = 0
self.last_update_time = time.time()
self.is_initialized = False
self.start_time = time.time()
self.last_inode = None
self.health_status = True
self.max_wait_time = 300 # Maximum wait time in seconds

def wait_for_log_file(self):
max_wait_time = 300 # Maximum wait time in seconds
wait_interval = 5 # Interval between checks in seconds
start_time = time.time()
while not os.path.exists(self.log_file_path):
elapsed_time = time.time() - start_time
if elapsed_time >= max_wait_time:
logger.error(f"Log file did not appear within {max_wait_time} seconds.")
if elapsed_time >= self.max_wait_time:
logger.error(f"Log file did not appear within {self.max_wait_time} seconds.")
return False
logger.info(f"Waiting for log file to appear... ({int(elapsed_time)} seconds elapsed)")
time.sleep(wait_interval)
Expand All @@ -185,7 +185,6 @@ def initial_load(self):
except orjson.JSONDecodeError:
logger.error(f"Error decoding JSON: {line}")
self.last_position = log_file.tell()
self.last_update_time = time.time()
self.is_initialized = True
logger.info(f"Initial load complete. Processed up to position {self.last_position}")
else:
Expand Down Expand Up @@ -229,25 +228,25 @@ def process_new_lines(self):
except orjson.JSONDecodeError:
logger.error(f"Error decoding JSON: {line}")
self.last_position = log_file.tell()
self.last_update_time = time.time()
self.is_initialized = True
self.health_status = True
break # Successfully processed, exit the retry loop
except FileNotFoundError:
if attempt < max_retries - 1:
logger.warning(f"Log file not found. Retrying in {retry_delay} seconds...")
time.sleep(retry_delay)
else:
logger.error("Max retries reached. Unable to process log file.")
self.health_status = False
except Exception as e:
logger.error(f"Error processing log file: {e}")
self.health_status = False
break # Exit the retry loop for other types of exceptions

def is_ready(self):
return self.is_initialized or time.time() - self.start_time < 300
return self.is_initialized or time.time() - self.start_time < self.max_wait_time

def is_healthy(self):
return (self.is_initialized or time.time() - self.start_time < 300) and \
(not os.path.exists(self.log_file_path) or time.time() - self.last_update_time < update_interval * 30)
return self.health_status

class HealthServer:
def __init__(self, log_handler):
Expand Down

0 comments on commit bcbdcd3

Please sign in to comment.