I'm trying to write a monitoring function in Python.
def monitor(filename):
"""
Read from a log file.
For each line check if log line time is later than base comparison time.
If so, update the base time and set error count to 0.
Check for errors in line. Increment error count if any are found.
Check the total error count. If it's greater than five, restart the logged
process.
Check if process has been restarted before. If so, kill the logged process.
"""
error_count = 0
base_time = datetime.datetime.min
log = read_log(filename)
restarted = False
for line in log:
line_time = parse_time(line)
if line_time > base_time:
base_time = increase_base_time(line_time)
error_count = 0
if find_error(line, base_time, line_time):
error_count += 1
if count >= 5:
# Restart process
error_count = 0
restarted = True
if restarted:
# Kill process
There are a lot of if-statement for doing checks. I thought about making this class based, but it really doesn't make sense to do it (functions don't need to share states and it is a rather small program). Is the number of if's acceptable? How could I refactor the code to make it flow a bit better?
Aucun commentaire:
Enregistrer un commentaire