From 36ee5a24aa0be6f867a71cf98424a2e112ea3780 Mon Sep 17 00:00:00 2001 From: Justintime50 <39606064+Justintime50@users.noreply.github.com> Date: Sat, 6 Mar 2021 15:47:33 -0700 Subject: [PATCH] Adding retry logic to contianer_healthcheck --- docs/README.md | 2 +- harvey/stage.py | 23 ++++++++++++----------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/docs/README.md b/docs/README.md index 9076e8b..e1a98a6 100644 --- a/docs/README.md +++ b/docs/README.md @@ -26,7 +26,7 @@ The following example will run a full pipeline (tests, build and deploy), tag it } ``` -* Optional: `compose` value can be passed to specify the `docker-compose` file to use **if** building from a docker-compose file and hitting the `/pipelines/start/compose` endpoint (eg: `-f docker-compose.yml -f docker-compose-prod.yml`). +* Optional: `compose` value can be passed to specify the `docker-compose` file to use **if** building from a docker-compose file and hitting the `/pipelines/start/compose` endpoint (eg: `docker-compose.yml -f docker-compose-prod.yml`). * Optional: `dockerfile` value can be passed to specify the name of the Dockerfile to build from (including path if not in root directory). **Possible Pipeline Values** diff --git a/harvey/stage.py b/harvey/stage.py index 70247ec..1439025 100644 --- a/harvey/stage.py +++ b/harvey/stage.py @@ -240,25 +240,26 @@ def build_deploy_compose(cls, config, webhook, output): return final_output @classmethod - def run_container_healthcheck(cls, webhook): + def run_container_healthcheck(cls, webhook, retry_attempt=0): """Run a healthcheck to ensure the container is running and not in a transitory state. Not to be confused with the Docker Healthcheck functionality which is different """ - print('Running container healthcheck...') - time.sleep(5) + print('Running container healthcheck...') # TODO: Attach project name to this line + + # If we cannot inspect a container, it may not be up and running yet, retry container = Container.inspect_container(Global.docker_project_name(webhook)) container_json = container.json() - state = container_json['State'] - if ( - state['Restarting'] - and state['Dead'] - and state['Paused'] - ) is False and state['Running'] is True: + state = container_json.get('State') + if not state and retry_attempt <= 5: + retry_attempt += 1 + time.sleep(5) + cls.run_container_healthcheck(webhook, retry_attempt) + elif state['Running'] is True: healthcheck = True output = 'Project healthcheck succeeded!' - print(output) else: healthcheck = False output = 'Project healthcheck failed.' - print(output) + + print(output) return healthcheck, output