Repository: weni-ai/bothub-engine

Issue:
Fix health checker /ping/ - infinite looping

Improve check_database_connection function
We can improve this code like that:

```python
def check_database_connection(**kwargs):
        for conn in connections.all():
            try:
                conn.cursor()
                return True
            except OperationalError:
                return False
        return False
```

reported by @eltonplima in #158 
Improve check_database_connection function
We can improve this code like that:

```python
def check_database_connection(**kwargs):
        for conn in connections.all():
            try:
                conn.cursor()
                return True
            except OperationalError:
                return False
        return False
```

reported by @eltonplima in #158 


Discussion:

Resolving patch:
diff --git a/bothub/health/checks.py b/bothub/health/checks.py
--- a/bothub/health/checks.py
+++ b/bothub/health/checks.py
@@ -1,23 +1,37 @@
+import logging
+
+from rest_framework import status
+
+
+logger = logging.getLogger('bothub.health.checks')
+
+CHECK_ACCESSIBLE_API_URL = '/api/repositories/'
+
+
 def check_database_connection(**kwargs):
     from django.db import connections
     from django.db.utils import OperationalError
-    db_conn = connections['default']
-    if not db_conn:
-        return False
-    try:
-        db_conn.cursor()
-        return True
-    except OperationalError as e:
+    if len(connections.all()) is 0:
         return False
+    logger.info('found {} database connection'.format(len(connections.all())))
+    for i, conn in enumerate(connections.all(), 1):
+        try:
+            conn.cursor()
+            logger.info('#{} db connection OKAY'.format(i))
+        except OperationalError as e:
+            logger.warning('#{} db connection ERROR'.format(i))
+            return False
+    return True
 
 
 def check_accessible_api(request, **kwargs):
-    import requests
-    HTTP_HOST = request.META.get('HTTP_HOST')
-    repositories_url = 'http://{}/api/repositories/'.format(HTTP_HOST)
-    request = requests.get(repositories_url)
-    try:
-        request.raise_for_status()
+    from django.test import Client
+    logger.info('making request to {}'.format(CHECK_ACCESSIBLE_API_URL))
+    client = Client()
+    response = client.get(CHECK_ACCESSIBLE_API_URL)
+    logger.info('{} status code: {}'.format(
+        CHECK_ACCESSIBLE_API_URL,
+        response.status_code))
+    if response.status_code is status.HTTP_200_OK:
         return True
-    except requests.HTTPError as e:
-        return False
+    return False
diff --git a/bothub/settings.py b/bothub/settings.py
--- a/bothub/settings.py
+++ b/bothub/settings.py
@@ -2,6 +2,7 @@
 import dj_database_url
 
 from decouple import config
+from django.utils.log import DEFAULT_LOGGING
 
 
 # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
@@ -191,7 +192,7 @@
 
 BOTHUB_NLP_BASE_URL = config(
     'BOTHUB_NLP_BASE_URL',
-    default='http://localhost:8001/')
+    default='http://localhost:2657/')
 
 
 # CSRF
@@ -204,3 +205,21 @@
     'CSRF_COOKIE_SECURE',
     default=False,
     cast=bool)
+
+
+# Logging
+
+LOGGING = DEFAULT_LOGGING
+LOGGING['formatters']['bothub.health'] = {
+    'format': '[bothub.health] {message}',
+    'style': '{',
+}
+LOGGING['handlers']['bothub.health'] = {
+    'level': 'DEBUG',
+    'class': 'logging.StreamHandler',
+    'formatter': 'bothub.health',
+}
+LOGGING['loggers']['bothub.health.checks'] = {
+    'handlers': ['bothub.health'],
+    'level': 'DEBUG',
+}


Test patch: