Commit bfe3d620 authored by Alain Takoudjou's avatar Alain Takoudjou

slapgrid.promise: Fix again get promise result, count execution instead of check time

parent ecdfff77
......@@ -221,7 +221,7 @@ class GenericPromise(object):
if match is not None:
if not only_failure or (only_failure and match.groups()[1] == 'ERROR'):
result_list.append({
'date': match.groups()[0],
'date': datetime.strptime(match.groups()[0], '%Y-%m-%d %H:%M:%S'),
'status': match.groups()[1],
'message': (match.groups()[2] + line_part).strip(),
})
......@@ -232,13 +232,26 @@ class GenericPromise(object):
return result_list
def getLastPromiseResultList(self, latest_minute=1, only_failure=False):
def __checkLogDateInNewPromise(self, log_date, previous_date):
if previous_date is None:
return True
diff = previous_date - log_date
if diff.seconds >= self.getConfig('promise-timeout'):
# The promise run in max 20 seconds so this was not in the same process
return True
return False
def getLastPromiseResultList(self, latest_minute=0, result_count=1,
only_failure=False):
"""
Return the latest log result of the promise starting from the most recent
@param last_minute: the number of minutes in the past. If last_minute is
1, it will return the log of the latest minute execution.
1, it will return the log of the latest minute execution. 0 => disabled
@param only_failure: only return the lines which contain failures.
@param result_count: maximum number of promise result to check, will not
exceed the `latest_minute`
@return Return a dict of logs. The format is
[{"date": "DATE", "status": "STATUS", "message": MESSAGE}, ]
"""
......@@ -254,10 +267,14 @@ class GenericPromise(object):
return []
regex = self.__getLogRegex()
date = datetime.now() - timedelta(minutes=latest_minute)
date_string = date.strftime('%Y-%m-%d %H:%M:%S')
max_date_string = ""
if latest_minute > 0:
date = datetime.now() - timedelta(minutes=latest_minute)
max_date_string = date.strftime('%Y-%m-%d %H:%M:%S')
line_list = []
previous_date = None
promise_count = 0
with open(self.__log_file, 'r') as f:
offset = 0
f.seek(0, 2)
......@@ -276,36 +293,46 @@ class GenericPromise(object):
if line != "":
result = regex.match(line)
if result is not None:
if result.groups()[0] <= date_string:
if max_date_string and result.groups()[0] <= max_date_string:
break
log_date = datetime.strptime(result.groups()[0], '%Y-%m-%d %H:%M:%S')
if self.__checkLogDateInNewPromise(log_date, previous_date):
promise_count += 1
previous_date = log_date
if promise_count > result_count:
break
if not only_failure or \
(only_failure and result.groups()[1] == 'ERROR'):
line_list.append({
'date': result.groups()[0],
'date': log_date,
'status': result.groups()[1],
'message': (result.groups()[2] + line_part).strip(),
})
else:
line_part += '\n' + line
line_part = '\n' + line + line_part
line = ""
continue
line = line_part = ""
return line_list
def defaultTest(self, latest_minute=2, failure_amount=1, is_anomaly=False):
def defaultTest(self, result_count=1, failure_amount=1, latest_minute=0,
is_anomaly=False):
"""
Test if the latest messages contain `failure_amount` failures.
@param result_count: maximum number of promise result to check, will not
exceed the `latest_minute`
@param latest_minute: test the result from now to the latest X minutes in
the past.
@param failure_amount: fail is this amount of failure is found in result
@param is_anomaly: Say if the result is an AnomalyResult of TestResult
"""
problem = False
problem = True
message = ""
module = TestResult if not is_anomaly else AnomalyResult
latest_result_list = self.getLastPromiseResultList(
result_count=result_count,
latest_minute=latest_minute,
only_failure=False
)
......@@ -321,7 +348,6 @@ class GenericPromise(object):
while i < result_size and failure_found < failure_amount:
if latest_result_list[i]['status'] == 'ERROR':
failure_found += 1
problem = True
i += 1
if failure_found != failure_amount:
......@@ -754,6 +780,7 @@ class PromiseLauncher(object):
'log-folder': self.log_folder,
'partition-folder': self.partition_folder,
'debug': self.debug,
'promise-timeout': self.promise_timeout,
'slapgrid-mode': self.slapgrid_mode,
'check-anomaly': self.check_anomaly,
'master-url': self.master_url,
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment