diff --git a/docker-compose_dist.yml b/docker-compose_dist.yml index df7f152..779123d 100644 --- a/docker-compose_dist.yml +++ b/docker-compose_dist.yml @@ -54,7 +54,10 @@ services: # Hosts separated by comma ('http://', 'https://' prefixes are mandatory) # Example: CSRF_TRUSTED_ORIGINS: 'http://127.0.0.1', 'http://127.0.0.1,https://127.0.0.1:8000', etc CSRF_TRUSTED_ORIGINS: 'http://modulector:8000,http://www.modulector:8000' - ALLOWED_HOSTS: 'modulector' + + # Allowed host must include the ones set in the NGINX modulector.conf file. Localhost is included for + # health-checks + ALLOWED_HOSTS: 'web,modulector,localhost' volumes: postgres_data: diff --git a/tools/healthcheck/check.py b/tools/healthcheck/check.py index ec88c2e..a250f1b 100644 --- a/tools/healthcheck/check.py +++ b/tools/healthcheck/check.py @@ -1,47 +1,54 @@ -import requests,sys, os +import requests, sys, os + def check(): exit_code = os.EX_OK - health_url = os.getenv("HEALTH_URL","http://localhost:8000/drugs/") + health_url = os.getenv("HEALTH_URL", "http://localhost:8000/drugs/") try: r = requests.get(health_url) - if r.status_code != 200 : + if r.status_code != 200: raise Exception("The probe has failed") set_tries("0") - except: + except Exception as ex: + print(ex) notify() exit_code = os.EX_SOFTWARE sys.exit(exit_code) + def notify(): - url = os.getenv("HEALTH_ALERT_URL","") - if url!="": + url = os.getenv("HEALTH_ALERT_URL", "") + if url != "": tries = get_tries() - tries = tries+1 + tries = tries + 1 headers = {'Content-Type': 'application/json'} - message="Modulector: Health check failed {} times.".format(tries) - if tries==3: + message = "Modulector: Health check failed {} times.".format(tries) + if tries == 3: message = message + " The container has been marked as unhealthy." set_tries(0) - data = { "content": message } + data = {"content": message} try: - r = requests.post(url,headers=headers, json=data) - except: + requests.post(url, headers=headers, json=data) + except Exception as ex: + print(ex) pass set_tries(str(tries)) + def get_tries(): - tries=0 + tries = 0 try: - with open("tries.txt","r") as f: - tries=f.read() + with open("tries.txt", "r") as f: + tries = f.read() except FileNotFoundError: set_tries(0) return int(tries) + def set_tries(tries): - with open("tries.txt","w") as f: + with open("tries.txt", "w") as f: f.write(str(tries)) -if __name__=="__main__": + +if __name__ == "__main__": check()