XD-DENG commented on a change in pull request #4309: [AIRFLOW-3504] 
Extend/refine the functionality of "/health" endpoint
URL: https://github.com/apache/airflow/pull/4309#discussion_r245502264
 
 

 ##########
 File path: airflow/www/blueprints.py
 ##########
 @@ -32,6 +37,26 @@ def index():
 
 @routes.route('/health')
 def health():
-    """ We can add an array of tests here to check the server's health """
-    content = Markup(markdown.markdown("The server is healthy!"))
-    return content
+    session = settings.Session()
+    DM = models.DagModel
+    payload = {}
+
+    latest_scheduler_run = None
+    payload['metadatabase'] = {'status': 'healthy'}
+    try:
+        latest_scheduler_run = 
session.query(func.max(DM.last_scheduler_run)).scalar()
+    except Exception as _:
+        payload['metadatabase']['status'] = 'unhealthy'
+
+    if not latest_scheduler_run:
+        scheduler_status = 'unhealthy'
+    else:
+        if timezone.utcnow() - latest_scheduler_run <= timedelta(seconds=30):
+            scheduler_status = 'healthy'
+        else:
+            scheduler_status = 'unhealthy'
+
+    payload['scheduler'] = {'status': scheduler_status,
+                            'latest_scheduler_run': str(latest_scheduler_run)}
 
 Review comment:
   Thanks @feng-tao for reviewing.
   
   Yes, this converting was added purposely.
   
   ```python
   import json
   
   from airflow.www import utils as wwwutils
   from airflow.utils import timezone
   
   
   ts = timezone.utcnow()
   
   print(json.loads(wwwutils.json_response({"timestamp":ts}).data))
   print(json.loads(wwwutils.json_response({"timestamp":str(ts)}).data))
   ```
   
   **Result:**
   ```
   {'timestamp': '2019-01-06T09:39:49Z'}
   {'timestamp': '2019-01-06 09:39:49.894382+00:00'}
   ```
   
   The second format is more favourable to me. That was why I purposely chose 
explicit str conversion.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to