# Adding Healthchecks to Apps ## What is healthcheck? a **healthcheck** is simply an endpoint that your app supports that another service can call to check if your app is active/running or not. For example, to check if signals is running, haproxy might make a call to `/signals/healthcheck` and check the response code for a 200. If the response code is anything but a 200, haproxy can take various actions like - - using a backup server - showing an error message ## How to add a healthcheck In general, you have to do 2 things to configure a healthcheck for your app, i.e. - ### 1. Setup the healthcheck endpoint in your app #### UI ##### Deploying using pm2 If you're deploy method is using `pm2`, you don't have to do anything in your app as the pm2 server takes care of responding to the healthcheck api. ##### Static Deployments The default `/healthcheck` route for your app will return a blank screen but with a 200 so no additional configuration should be necessary. #### Backend ##### Using cookiecutter v1 Most endpoints currently have been written using the cookiecutter v1. To add the healthcheck endpoint here, simply - 1. Create a healthcheck function In your src folder, add a function called `healthcheck` to one of your files. For example, if you have a file called `src/prescriptions.py`, add a function similar to the one below - ```python= ... def healthcheck(): # call some functions that your app is reliant on. If these fail, the function will raise an error and the API will return a non 200 code. results = get_prescription_data('imatinib') return True ... ``` 3. Find your api.ini. This should normally just be in the root directory for your app. It might look something like this ``` [get] [post] compare_signals.fetch_dynamic_adjacency_query compare_signals.compare_dynamic_adjacency_queries [cron] ``` 2. Add the healthcheck endpoint as a GET request ``` [get] prescriptions.healthcheck [post] compare_signals.fetch_dynamic_adjacency_query compare_signals.compare_dynamic_adjacency_queries [cron] ``` ### 2. Configure the healthcheck in `nferx_Devops` 1. Find `ansible/apps_v2.json` in `nferx_Devops` 2. Find your app config block. For example, the `prescriptions` app block looks like ```json { "appname": "prescriptions", "template": "uiwithpm2_v1", "port": 4227, "repo": "nfer", "path": "nfer/nferxapps/prescriptions", "branch": "master", "appsredirect": true, }, ``` 3. Add the healthcheck endpoint to the block ```json { ... "healthcheck": "/prescriptions/healthcheck" } ``` 4. Make sure the owner/alert emails are set. The `owner` email is the app developers email. The alert email is used to notify the appropriate party via pager duty of the app failure. You can use `application@nference.pagerduty.com` for the alert email. ```json { ... "healthcheck": "/prescriptions/healthcheck", "owner": "developer@nference.net", "alert": "application@nference.pagerduty.com" } ```