From 792f515d3376c8fc0d5a623a4a2364fe044d4dbc Mon Sep 17 00:00:00 2001 From: "Nicholas H.Tollervey" Date: Mon, 2 Nov 2020 11:46:01 +0000 Subject: [PATCH] Add timeout of 1 second to log search and catch associated exceptions. --- controlpanel/api/elasticsearch.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/controlpanel/api/elasticsearch.py b/controlpanel/api/elasticsearch.py index 168080acc..bb8ae4f01 100644 --- a/controlpanel/api/elasticsearch.py +++ b/controlpanel/api/elasticsearch.py @@ -1,3 +1,4 @@ +import logging from dateutil.parser import parse from django.conf import settings from elasticsearch import Elasticsearch @@ -5,6 +6,9 @@ from elasticsearch_dsl.query import Range +log = logging.getLogger(__name__) + + def bucket_hits_aggregation(bucket_name, num_days=None): conn = Elasticsearch(hosts=settings.ELASTICSEARCH['hosts']) @@ -29,6 +33,7 @@ def bucket_hits_aggregation(bucket_name, num_days=None): def app_logs(app, num_hours=None): # Disable logs for noisy app ("covid19-early-release") # to prevent its App details page to timeout + log.info("Get app logs for: " + str(app.id)) if app.id == 171: return [] @@ -36,7 +41,11 @@ def app_logs(app, num_hours=None): num_hours = 1 conn = Elasticsearch(hosts=settings.ELASTICSEARCH['hosts']) - s = Search(using=conn, index=settings.ELASTICSEARCH['indices']['app-logs']) + # Create Search object. Timeout after 1 second. Will retry several times. + s = Search( + using=conn, + index=settings.ELASTICSEARCH['indices']['app-logs'] + ).params(request_timeout=1) # limit fields returned s = s.source(['@timestamp', 'message']) @@ -59,7 +68,13 @@ def app_logs(app, num_hours=None): s = s.params(preserve_order=True) - for entry in s.scan(): - entry['timestamp'] = parse(entry['@timestamp']) - yield entry + try: + logs = list(s.scan()) + log.info("Got {} log entries.".format(len(logs))) + for entry in logs: + entry['timestamp'] = parse(entry['@timestamp']) + except Exception as ex: + log.error(ex) + logs = [] + return logs