Skip to content

Commit

Permalink
Fixes cc-archive#487(reflected changes mentioned in PR review)
Browse files Browse the repository at this point in the history
  • Loading branch information
madewithkode committed May 14, 2020
1 parent dba4eca commit ee92b11
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
20 changes: 15 additions & 5 deletions cccatalog-api/cccatalog/api/views/site_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down
14 changes: 14 additions & 0 deletions ingestion_server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ee92b11

Please sign in to comment.