diff --git a/.travis.yml b/.travis.yml index eada2f9e..a772273f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,7 +23,7 @@ before_script: - sudo service postgresql stop - while sudo lsof -Pi :5432 -sTCP:LISTEN -t; do sleep 1; done - docker-compose up -d - - bash -c 'while [[ "$(curl --insecure -s -o /dev/null -w ''%{http_code}'' http://localhost:8000/healthcheck)" != "200" ]]; do sleep 10 && docker logs cccatalog-api_web_1; done' + - bash -c 'while [[ "$(curl --insecure -s -o /dev/null http://localhost:8000/healthcheck | jq '.error')" != "IndexMissing" ]] || [[ "$(curl --insecure -s -o /dev/null -w ''%{http_code}'' http://localhost:8000/healthcheck)" != "200" ]]; do sleep 10 && docker logs cccatalog-api_web_1; done' - ./load_sample_data.sh - bash -c 'while [[ "$(curl --insecure -s -o /dev/null -w ''%{http_code}'' http://localhost:8000/v1/images?q=test)" != "200" ]]; do sleep 5; done' script: diff --git a/cccatalog-api/cccatalog/api/views/site_views.py b/cccatalog-api/cccatalog/api/views/site_views.py index c2ca426e..8c934ca9 100644 --- a/cccatalog-api/cccatalog/api/views/site_views.py +++ b/cccatalog-api/cccatalog/api/views/site_views.py @@ -26,13 +26,14 @@ FILTER = 'filter_content' URL = 'domain_name' HEALTH_CACHE_TTL = 10 +ALLOWED_INDEX_STATUSES = ['green','yellow'] @method_decorator(cache_page(HEALTH_CACHE_TTL), name='get') class HealthCheck(APIView): """ Returns a `200 OK` response if the server is running and `image` - index exists and a `500 Internal Server Error` if either of the condition fails. + index is healthy and a `500 Internal Server Error` if either of the condition fails. This endpoint is used in production to ensure that the server should receive traffic. If no response is provided, the server is deregistered from the @@ -42,12 +43,21 @@ class HealthCheck(APIView): def get(self, request, format=None): es_conn = es - image_index_exists = es_conn.indices.exists(index=["image"]) + image_index_exists = es_conn.indices.exists(index=['image']) if image_index_exists: - return Response('', status=200) + health_check = es_conn.cluster.health(index='image') + index_status = health_check.get("status") + if index_status not in ALLOWED_INDEX_STATUSES: + log.error('The health check failed because the status of the image index is unhealthy.') + response_data = {"error": "IndexUnhealthy", "detail": "The status of the image index is unhealthy."} + return Response(response_data, status=500) + else: + return Response(health_check, status=200) else: - log.error('The health check failed because the cluster image index is either unhealthy or non-existent') - return Response('', status=500) + response_data = {"error": "IndexMissing", "detail": "The image index does not exist. Index data to Elasticsearch."} + log.error('The health check failed because the cluster image index is non-existent') + return Response(response_data, status=500) + class AboutImageResponse(serializers.Serializer): diff --git a/ingestion_server/Dockerfile b/ingestion_server/Dockerfile index 104fed96..abde9e42 100644 --- a/ingestion_server/Dockerfile +++ b/ingestion_server/Dockerfile @@ -26,3 +26,17 @@ RUN pipenv install --deploy --system --dev USER supervisord EXPOSE 8001 CMD ["supervisord", "-c", "/ingestion_server/config/supervisord.conf"] + +# Install the jq tool to filter JSON data +ENV JQ_VERSION='1.5' + +RUN wget --no-check-certificate https://raw.githubusercontent.com/stedolan/jq/master/sig/jq-release.key -O /tmp/jq-release.key && \ + wget --no-check-certificate https://raw.githubusercontent.com/stedolan/jq/master/sig/v${JQ_VERSION}/jq-linux64.asc -O /tmp/jq-linux64.asc && \ + wget --no-check-certificate https://github.com/stedolan/jq/releases/download/jq-${JQ_VERSION}/jq-linux64 -O /tmp/jq-linux64 && \ + gpg --import /tmp/jq-release.key && \ + gpg --verify /tmp/jq-linux64.asc /tmp/jq-linux64 && \ + cp /tmp/jq-linux64 /usr/bin/jq && \ + chmod +x /usr/bin/jq && \ + rm -f /tmp/jq-release.key && \ + rm -f /tmp/jq-linux64.asc && \ + rm -f /tmp/jq-linux64 \ No newline at end of file